diff --git a/mongodb.go b/mongodb.go index 26b3384..a6db390 100644 --- a/mongodb.go +++ b/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 + } +} diff --git a/recommendations.go b/recommendations.go new file mode 100644 index 0000000..67548d2 --- /dev/null +++ b/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) +} diff --git a/serverRoutes.go b/serverRoutes.go index a95c4ac..22da524 100644 --- a/serverRoutes.go +++ b/serverRoutes.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "net/http" + "strconv" "github.com/gorilla/mux" ) @@ -52,11 +53,11 @@ func NewUser(w http.ResponseWriter, r *http.Request) { func Recommendations(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) userid := vars["userid"] - nrec := vars["nrec"] - fmt.Println(userid) - fmt.Println(nrec) + nrec, err := strconv.Atoi(vars["nrec"]) + check(err) //now, get recommendations + getRecommendations(userid, nrec) fmt.Fprintln(w, "recommendations") }