Yimaru-BackEnd/internal/services/recommendation/service.go

65 lines
1.4 KiB
Go

package recommendation
import (
"Yimaru-Backend/internal/repository"
"context"
"sort"
)
// type RecommendationService interface {
// GetRecommendations(ctx context.Context, userID string) ([]string, error)
// }
type service struct {
repo repository.RecommendationRepository
}
func NewService(repo repository.RecommendationRepository) RecommendationService {
return &service{repo: repo}
}
func (s *service) GetRecommendations(ctx context.Context, userID string) ([]string, error) {
interactions, err := s.repo.GetUserVirtualGameInteractions(ctx, userID)
if err != nil {
return nil, err
}
recommendationMap := map[string]int{}
for _, interaction := range interactions {
score := 1
switch interaction.InteractionType {
case "bet":
score = 5
case "view":
score = 1
}
recommendationMap[interaction.GameID] += score
}
// Example: return top 3 games based on score
topGames := sortGamesByScore(recommendationMap)
return topGames, nil
}
func sortGamesByScore(scoreMap map[string]int) []string {
type kv struct {
Key string
Value int
}
var sorted []kv
for k, v := range scoreMap {
sorted = append(sorted, kv{k, v})
}
sort.Slice(sorted, func(i, j int) bool {
return sorted[i].Value > sorted[j].Value
})
var result []string
for i := 0; i < len(sorted) && i < 3; i++ {
result = append(result, sorted[i].Key)
}
return result
}