diff options
Diffstat (limited to 'internal/handlers')
-rw-r--r-- | internal/handlers/elements.go | 10 | ||||
-rw-r--r-- | internal/handlers/helpers.go | 36 | ||||
-rw-r--r-- | internal/handlers/main.go | 18 |
3 files changed, 53 insertions, 11 deletions
diff --git a/internal/handlers/elements.go b/internal/handlers/elements.go index 7fcd9ce..65a1c2d 100644 --- a/internal/handlers/elements.go +++ b/internal/handlers/elements.go @@ -1,7 +1,6 @@ package handlers import ( - "apjournal/internal/models" "html/template" "net/http" ) @@ -28,17 +27,12 @@ func (h *Handlers) ShowRecommended(w http.ResponseWriter, r *http.Request) { var username string // TODO: getusername // TODO: get recommendations for user - plusA, err := h.repo.DBActionRecommend(username, models.ActionTypePlus) + acts, err := h.fetchRecommendations(username, 5) if err != nil { abortWithError(w, err.Error()) return } - minusA, err := h.repo.DBActionRecommend(username, models.ActionTypeMinus) - if err != nil { - abortWithError(w, err.Error()) - return - } - h.log.Debug("got actions", "plus#", len(plusA), "minus#", len(minusA)) + h.log.Debug("got actions", "acts#", len(acts)) tmpl, err := template.ParseGlob("components/*.html") if err != nil { abortWithError(w, err.Error()) diff --git a/internal/handlers/helpers.go b/internal/handlers/helpers.go new file mode 100644 index 0000000..bc705af --- /dev/null +++ b/internal/handlers/helpers.go @@ -0,0 +1,36 @@ +package handlers + +import "apjournal/internal/models" + +func (h *Handlers) fetchRecommendations( + username string, limit int, +) ([]models.Action, error) { + actionsRes := []models.Action{} + acts, err := h.repo.DBActionRecommend(username) + if err != nil { + return nil, err + } + plusCounter := 0 + minusCounter := 0 + for _, act := range acts { + if act.Type == models.ActionTypePlus { + if plusCounter >= limit { + continue + } + actionsRes = append(actionsRes, act) + plusCounter++ + continue + } + if minusCounter >= limit { + continue + } + actionsRes = append(actionsRes, act) + minusCounter++ + if len(actionsRes) > limit*2 { + break + } + } + h.log.Debug("actions rec debug", "db_acts#", len(acts), + "res_acts#", len(actionsRes)) + return actionsRes, nil +} diff --git a/internal/handlers/main.go b/internal/handlers/main.go index e470b49..3bb194c 100644 --- a/internal/handlers/main.go +++ b/internal/handlers/main.go @@ -15,6 +15,8 @@ import ( "github.com/jmoiron/sqlx" ) +var defUS = models.UserScore{} + // Handlers structure type Handlers struct { cfg config.Config @@ -50,21 +52,26 @@ func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) { abortWithError(w, err.Error()) return } + // get recommendations + defUS.Recommendations, err = h.fetchRecommendations("", 5) + if err != nil { + panic(err) + } usernameRaw := r.Context().Value("username") h.log.Info("got mainpage request", "username", usernameRaw) if usernameRaw == nil { - tmpl.ExecuteTemplate(w, "main", nil) + tmpl.ExecuteTemplate(w, "main", defUS) return } username := usernameRaw.(string) if username == "" { - tmpl.ExecuteTemplate(w, "main", nil) + tmpl.ExecuteTemplate(w, "main", defUS) return } userScore, err := h.repo.DBUserScoreGet(username) if err != nil { h.log.Warn("got db err", "err", err) - tmpl.ExecuteTemplate(w, "main", nil) + tmpl.ExecuteTemplate(w, "main", defUS) return } userScore.Actions, err = h.repo.DBActionList(username) @@ -72,6 +79,11 @@ func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) { abortWithError(w, err.Error()) return } + userScore.Recommendations, err = h.fetchRecommendations(username, 5) + if err != nil { + abortWithError(w, err.Error()) + return + } tmpl.ExecuteTemplate(w, "main", userScore) } |