mirror of
https://github.com/arnaucube/goRecommender.git
synced 2026-02-07 11:36:41 +01:00
starting recommendations
This commit is contained in:
24
mongodb.go
24
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
recommendations.go
Normal file
23
recommendations.go
Normal file
@@ -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,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"]
|
nrec, err := strconv.Atoi(vars["nrec"])
|
||||||
fmt.Println(userid)
|
check(err)
|
||||||
fmt.Println(nrec)
|
|
||||||
|
|
||||||
//now, get recommendations
|
//now, get recommendations
|
||||||
|
getRecommendations(userid, nrec)
|
||||||
|
|
||||||
fmt.Fprintln(w, "recommendations")
|
fmt.Fprintln(w, "recommendations")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user