/
fetch_profilecollections.go
77 lines (65 loc) 路 2.31 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
package fetch
import (
"crawshaw.io/sqlite"
"github.com/go-xorm/builder"
"github.com/itchio/butler/butlerd"
"github.com/itchio/butler/database/models"
"github.com/itchio/butler/endpoints/fetch/lazyfetch"
"github.com/itchio/butler/endpoints/fetch/pager"
"github.com/itchio/hades"
)
func FetchProfileCollections(rc *butlerd.RequestContext, params butlerd.FetchProfileCollectionsParams) (*butlerd.FetchProfileCollectionsResult, error) {
profile, client := rc.ProfileClient(params.ProfileID)
ft := models.FetchTargetForProfileCollections(profile.ID)
res := &butlerd.FetchProfileCollectionsResult{}
lazyfetch.Do(rc, ft, params, res, func(targets lazyfetch.Targets) {
collRes, err := client.ListProfileCollections()
models.Must(err)
profile.ProfileCollections = nil
for i, c := range collRes.Collections {
targets.Add(models.FetchTargetForCollection(c.ID))
profile.ProfileCollections = append(profile.ProfileCollections, &models.ProfileCollection{
// Other fields are set when saving the association
Collection: c,
Position: int64(i),
})
}
rc.WithConn(func(conn *sqlite.Conn) {
models.MustSave(conn, profile,
hades.AssocReplace("ProfileCollections",
hades.Assoc("Collection"),
),
)
})
})
rc.WithConn(func(conn *sqlite.Conn) {
var cond builder.Cond = builder.Eq{"profile_id": profile.ID}
joinCollections := false
search := hades.Search{}
switch params.SortBy {
case "default", "":
search = search.OrderBy("position " + pager.Ordering("ASC", params.Reverse))
case "updatedAt":
search = search.OrderBy("collections.updated_at " + pager.Ordering("DESC", params.Reverse))
joinCollections = true
case "title":
search = search.OrderBy("collections.title " + pager.Ordering("ASC", params.Reverse))
joinCollections = true
}
if params.Search != "" {
cond = builder.And(cond, builder.Like{"collections.title", params.Search})
joinCollections = true
}
if joinCollections {
search = search.Join("collections", "collections.id = profile_collections.collection_id")
}
var items []*models.ProfileCollection
pg := pager.New(params)
res.NextCursor = pg.Fetch(conn, &items, cond, search)
models.MustPreload(conn, items, hades.Assoc("Collection"))
for _, item := range items {
res.Items = append(res.Items, item.Collection)
}
})
return res, nil
}