From 01f9a9f5d71450a0c80195058245fdebe88796bd Mon Sep 17 00:00:00 2001 From: GrailFinder Date: Wed, 5 Jun 2024 08:22:36 +0300 Subject: Feat: recommendations for anon and user --- internal/handlers/helpers.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 internal/handlers/helpers.go (limited to 'internal/handlers/helpers.go') 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 +} -- cgit v1.2.3