Browse Source

recommending

master
arnaucode 7 years ago
parent
commit
fe78d1acaa
4 changed files with 27 additions and 24 deletions
  1. +8
    -7
      mongodb.go
  2. +7
    -9
      recommendations.go
  3. +11
    -8
      serverRoutes.go
  4. +1
    -0
      tests.sh

+ 8
- 7
mongodb.go

@ -141,11 +141,12 @@ func getAllItems() ([]ItemModel, error) {
result := []ItemModel{} result := []ItemModel{}
iter := itemCollection.Find(nil).Limit(100).Iter() iter := itemCollection.Find(nil).Limit(100).Iter()
err := iter.All(&result) err := iter.All(&result)
if err != nil {
//user not exist
return result, err
} else {
//user exist
return result, err
}
return result, err
}
func getItemsNotActed(actedItems []string) ([]ItemModel, error) {
result := []ItemModel{}
iter := itemCollection.Find(bson.M{"id": bson.M{"$nin": actedItems}}).Limit(100).Iter()
err := iter.All(&result)
return result, err
} }

+ 7
- 9
recommendations.go

@ -6,18 +6,16 @@ import (
"github.com/fatih/color" "github.com/fatih/color"
) )
func getRecommendations(userid string, nrec int) {
fmt.Println(userid)
fmt.Println(nrec)
func getRecommendations(userid string, nrec int) []ItemModel {
user, err := getUserById(userid) user, err := getUserById(userid)
check(err) check(err)
color.Blue("user: ")
fmt.Println(user)
items, err := getAllItems()
items, err := getItemsNotActed(user.Actions)
check(err) check(err)
color.Blue("all items: ")
//select nrec items from the items array
color.Blue("recommended items: ")
fmt.Println(items) fmt.Println(items)
return items
} }

+ 11
- 8
serverRoutes.go

@ -65,9 +65,12 @@ func Recommendations(w http.ResponseWriter, r *http.Request) {
check(err) check(err)
//now, get recommendations //now, get recommendations
getRecommendations(userid, nrec)
items := getRecommendations(userid, nrec)
//convert []items struct to json
jItems, err := json.Marshal(items)
check(err)
fmt.Fprintln(w, "recommendations")
fmt.Fprintln(w, string(jItems))
} }
func SelectItem(w http.ResponseWriter, r *http.Request) { func SelectItem(w http.ResponseWriter, r *http.Request) {
ipFilter(w, r) ipFilter(w, r)
@ -80,6 +83,12 @@ func SelectItem(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "item "+itemid+" not found") fmt.Fprintln(w, "item "+itemid+" not found")
} }
//find user
user, err := getUserById(userid)
if err != nil {
fmt.Fprintln(w, "user "+userid+" not found")
}
//increase TActed in item //increase TActed in item
item.TActed = item.TActed + 1 item.TActed = item.TActed + 1
@ -88,12 +97,6 @@ func SelectItem(w http.ResponseWriter, r *http.Request) {
check(err) check(err)
fmt.Println(item) fmt.Println(item)
//find user
user, err := getUserById(userid)
if err != nil {
fmt.Fprintln(w, "user "+userid+" not found")
}
//add item to []Actions of user //add item to []Actions of user
user.Actions = append(user.Actions, itemid) user.Actions = append(user.Actions, itemid)

+ 1
- 0
tests.sh

@ -19,6 +19,7 @@ echo "[selecting item by user]"
echo http http://127.0.0.1:3056/selectItem/user1/item1 echo http http://127.0.0.1:3056/selectItem/user1/item1
http http://127.0.0.1:3056/selectItem/user1/item1 http http://127.0.0.1:3056/selectItem/user1/item1
echo "------------------"
echo "[Getting recommendations for user]" echo "[Getting recommendations for user]"
echo http http://127.0.0.1:3056/user1/3 echo http http://127.0.0.1:3056/user1/3

Loading…
Cancel
Save