Browse Source

starting recommendations

master
arnaucode 7 years ago
parent
commit
e6737eb383
3 changed files with 51 additions and 3 deletions
  1. +24
    -0
      mongodb.go
  2. +23
    -0
      recommendations.go
  3. +4
    -3
      serverRoutes.go

+ 24
- 0
mongodb.go

@ -98,3 +98,27 @@ func saveUser(c *mgo.Collection, user UserModel) {
} }
} }
func getUserById(id string) (UserModel, error) {
result := UserModel{}
err := userCollection.Find(bson.M{"id": id}).One(&result)
if err != nil {
//user not exist
return result, err
} else {
//user exist
return result, err
}
}
func getAllItems() ([]ItemModel, error) {
result := []ItemModel{}
iter := itemCollection.Find(nil).Limit(100).Iter()
err := iter.All(&result)
if err != nil {
//user not exist
return result, err
} else {
//user exist
return result, err
}
}

+ 23
- 0
recommendations.go

@ -0,0 +1,23 @@
package main
import (
"fmt"
"github.com/fatih/color"
)
func getRecommendations(userid string, nrec int) {
fmt.Println(userid)
fmt.Println(nrec)
user, err := getUserById(userid)
check(err)
color.Blue("user: ")
fmt.Println(user)
items, err := getAllItems()
check(err)
color.Blue("all items: ")
fmt.Println(items)
}

+ 4
- 3
serverRoutes.go

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strconv"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -52,11 +53,11 @@ func NewUser(w http.ResponseWriter, r *http.Request) {
func Recommendations(w http.ResponseWriter, r *http.Request) { func Recommendations(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
userid := vars["userid"] userid := vars["userid"]
nrec := vars["nrec"]
fmt.Println(userid)
fmt.Println(nrec)
nrec, err := strconv.Atoi(vars["nrec"])
check(err)
//now, get recommendations //now, get recommendations
getRecommendations(userid, nrec)
fmt.Fprintln(w, "recommendations") fmt.Fprintln(w, "recommendations")
} }

Loading…
Cancel
Save