/
fetch_profilecollections.go
130 lines (111 loc) · 3.19 KB
/
fetch_profilecollections.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 fetch
import (
"github.com/itchio/butler/butlerd"
"github.com/itchio/butler/butlerd/messages"
"github.com/itchio/butler/database/hades"
"github.com/itchio/butler/database/models"
"github.com/itchio/go-itchio"
"github.com/pkg/errors"
)
func FetchProfileCollections(rc *butlerd.RequestContext, params *butlerd.FetchProfileCollectionsParams) (*butlerd.FetchProfileCollectionsResult, error) {
profile, client := rc.ProfileClient(params.ProfileID)
c := HadesContext(rc)
sendDBCollections := func() error {
err := c.Preload(rc.DB(), &hades.PreloadParams{
Record: profile,
Fields: []hades.PreloadField{
{Name: "ProfileCollections", OrderBy: `"position" ASC`},
{Name: "ProfileCollections.Collection"},
},
})
if err != nil {
return errors.WithStack(err)
}
profileCollections := profile.ProfileCollections
var collectionIDs []int64
collectionsByIDs := make(map[int64]*itchio.Collection)
for _, pc := range profileCollections {
c := pc.Collection
collectionIDs = append(collectionIDs, c.ID)
collectionsByIDs[c.ID] = c
}
var rows []struct {
itchio.CollectionGame
itchio.Game
}
err = rc.DB().Raw(`
SELECT collection_games.*, games.*
FROM collections
JOIN collection_games ON collection_games.collection_id = collections.id
JOIN games ON games.id = collection_games.game_id
WHERE collections.id IN (?)
AND collection_games.game_id IN (
SELECT game_id
FROM collection_games
WHERE collection_games.collection_id = collections.id
ORDER BY "position" ASC
LIMIT 8
)
`, collectionIDs).Scan(&rows).Error
if err != nil {
return errors.WithStack(err)
}
for _, row := range rows {
c := collectionsByIDs[row.CollectionGame.CollectionID]
cg := row.CollectionGame
game := row.Game
cg.Game = &game
c.CollectionGames = append(c.CollectionGames, &cg)
}
if len(profileCollections) > 0 {
yn := &butlerd.FetchProfileCollectionsYieldNotification{}
yn.Offset = 0
yn.Total = int64(len(profileCollections))
for _, pc := range profileCollections {
yn.Items = append(yn.Items, pc.Collection)
}
err = messages.FetchProfileCollectionsYield.Notify(rc, yn)
if err != nil {
return errors.WithStack(err)
}
}
return nil
}
err := sendDBCollections()
if err != nil {
return nil, errors.WithStack(err)
}
collRes, err := client.ListMyCollections()
if err != nil {
return nil, errors.WithStack(err)
}
profile.ProfileCollections = nil
for i, c := range collRes.Collections {
for j, g := range c.Games {
c.CollectionGames = append(c.CollectionGames, &itchio.CollectionGame{
Position: int64(j),
Game: g,
})
}
c.Games = nil
profile.ProfileCollections = append(profile.ProfileCollections, &models.ProfileCollection{
// Other fields are set when saving the association
Collection: c,
Position: int64(i),
})
}
err = c.Save(rc.DB(), &hades.SaveParams{
Record: profile,
Assocs: []string{"ProfileCollections"},
PartialJoins: []string{"CollectionGames"},
})
if err != nil {
return nil, errors.WithStack(err)
}
err = sendDBCollections()
if err != nil {
return nil, errors.WithStack(err)
}
res := &butlerd.FetchProfileCollectionsResult{}
return res, nil
}