/
achievement-queries.go
130 lines (117 loc) · 4.44 KB
/
achievement-queries.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package sqlc
import (
"context"
"encoding/json"
"github.com/bcc-code/bcc-media-platform/backend/common"
"github.com/bcc-code/bcc-media-platform/backend/loaders"
"github.com/google/uuid"
"github.com/samber/lo"
"gopkg.in/guregu/null.v4"
)
// GetAchievements retrieves achievements from database
func (q *Queries) GetAchievements(ctx context.Context, ids []uuid.UUID) ([]common.Achievement, error) {
rows, err := q.getAchievements(ctx, ids)
if err != nil {
return nil, err
}
return lo.Map(rows, func(i getAchievementsRow, _ int) common.Achievement {
var images = common.LocaleMap[null.String]{}
_ = json.Unmarshal(i.Images.RawMessage, &images)
var res = common.LocaleMap[null.String]{}
for key, img := range images {
if img.Valid {
res[key] = null.StringFrom(q.filenameToImageURL(img.String))
}
}
return common.Achievement{
ID: i.ID,
GroupID: i.GroupID,
Title: toLocaleString(i.Title.RawMessage, i.OriginalTitle),
Description: toLocaleString(i.Description.RawMessage, i.OriginalDescription.String),
Conditions: unmarshalTo[[]common.AchievementCondition](i.Conditions.RawMessage),
Images: res,
}
}), nil
}
// CollectionQueries contains collection specific queries
type CollectionQueries struct {
collection string
*Queries
}
// Collection retrieves specific methods for the given collection
func (q *Queries) Collection(collection string) CollectionQueries {
return CollectionQueries{
collection: collection,
Queries: q,
}
}
// GetAchievementsForActions retrieves achievementIDs for actions
func (cq *CollectionQueries) GetAchievementsForActions(ctx context.Context, actions []string) ([]loaders.Relation[uuid.UUID, string], error) {
rows, err := cq.getAchievementsForActions(ctx, getAchievementsForActionsParams{
Column1: cq.collection,
Column2: actions,
})
if err != nil {
return nil, err
}
return lo.Map(rows, func(i getAchievementsForActionsRow, _ int) loaders.Relation[uuid.UUID, string] {
return relation[uuid.UUID, string](i)
}), nil
}
// GetAchievementsForGroups retrieves achievementIDs for actions
func (q *Queries) GetAchievementsForGroups(ctx context.Context, groupIDs []uuid.UUID) ([]loaders.Relation[uuid.UUID, uuid.UUID], error) {
rows, err := q.getAchievementsForGroups(ctx, groupIDs)
if err != nil {
return nil, err
}
return lo.Map(rows, func(i getAchievementsForGroupsRow, _ int) loaders.Relation[uuid.UUID, uuid.UUID] {
return relation[uuid.UUID, uuid.UUID](i)
}), nil
}
// GetAchievementGroups returns achievement-groups
func (q *Queries) GetAchievementGroups(ctx context.Context, ids []uuid.UUID) ([]common.AchievementGroup, error) {
rows, err := q.getAchievementGroups(ctx, ids)
if err != nil {
return nil, err
}
return lo.Map(rows, func(i getAchievementGroupsRow, _ int) common.AchievementGroup {
return common.AchievementGroup{
ID: i.ID,
Title: toLocaleString(i.Title.RawMessage, i.OriginalTitle.String),
}
}), nil
}
// GetAchievementsForProfiles returns achievements achieved for profiles
func (q *Queries) GetAchievementsForProfiles(ctx context.Context, ids []uuid.UUID) ([]loaders.Relation[uuid.UUID, uuid.UUID], error) {
rows, err := q.getAchievedAchievementsForProfiles(ctx, ids)
if err != nil {
return nil, err
}
return lo.Map(rows, func(i getAchievedAchievementsForProfilesRow, _ int) loaders.Relation[uuid.UUID, uuid.UUID] {
return relation[uuid.UUID, uuid.UUID](i)
}), nil
}
// GetUnconfirmedAchievementsForProfiles returns achievements achieved for profiles
func (q *Queries) GetUnconfirmedAchievementsForProfiles(ctx context.Context, ids []uuid.UUID) ([]loaders.Relation[uuid.UUID, uuid.UUID], error) {
rows, err := q.getUnconfirmedAchievedAchievementsForProfiles(ctx, ids)
if err != nil {
return nil, err
}
return lo.Map(rows, func(i getUnconfirmedAchievedAchievementsForProfilesRow, _ int) loaders.Relation[uuid.UUID, uuid.UUID] {
return relation[uuid.UUID, uuid.UUID](i)
}), nil
}
// GetAchievementsAchievedAt returns when achievements has been achieved
func (pq *ProfileQueries) GetAchievementsAchievedAt(ctx context.Context, ids []uuid.UUID) ([]common.Achieved, error) {
rows, err := pq.queries.achievementAchievedAt(ctx, achievementAchievedAtParams{ProfileID: pq.profileID, Column2: ids})
if err != nil {
return nil, err
}
return lo.Map(rows, func(i achievementAchievedAtRow, _ int) common.Achieved {
return common.Achieved{
ID: i.AchievementID,
AchievedAt: i.AchievedAt,
ConfirmedAt: i.ConfirmedAt,
}
}), nil
}