-
Notifications
You must be signed in to change notification settings - Fork 0
/
recommendations.controller.go
55 lines (43 loc) · 1.59 KB
/
recommendations.controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package controllers
import (
"net/http"
"strconv"
"github.com/anirudhgray/balkan-assignment/models"
"github.com/anirudhgray/balkan-assignment/utils/recommender"
"github.com/gin-gonic/gin"
)
func GenerateRecommendations(c *gin.Context) {
currentUser, _ := c.Get("user")
user := currentUser.(*models.User)
userID := user.ID
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
likes, dislikes, err := recommender.GetUserLikesDislikes(userID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err})
return
}
otherUsers, err := recommender.GetUsersWithSimilarInteractions(likes, dislikes, userID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
similarities, err := recommender.CalculateSimilaritiesWithOtherUsers(userID, otherUsers, likes, dislikes)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
unreviewed, err := recommender.GetUnreviewedBooks(userID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
recprobs := recommender.CalculateRecommendationProbabilities(userID, unreviewed, similarities)
recommendedBooks := recommender.GetRecommendedBooksSortedAndPaginated(recprobs, page, 20)
var message string
if len(recommendedBooks) > 0 {
message = "Here are some recommendations for you."
} else {
message = "Could not get any recommendations for you. Maybe try looking at our catalog for now? Check out the recs section on the readme for more info on this."
}
c.JSON(http.StatusOK, gin.H{"message": message, "recommendations": recommendedBooks})
}