summaryrefslogtreecommitdiff
path: root/internal/handlers/helpers.go
blob: bc705afc2fb06738874bf5e4f1238d44499703c4 (plain)
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
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
}