-
Notifications
You must be signed in to change notification settings - Fork 54
/
fetch_collection.go
46 lines (36 loc) 路 1.2 KB
/
fetch_collection.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
package fetch
import (
"crawshaw.io/sqlite"
"github.com/itchio/butler/butlerd"
"github.com/itchio/butler/database/models"
"github.com/itchio/butler/endpoints/fetch/lazyfetch"
itchio "github.com/itchio/go-itchio"
"github.com/pkg/errors"
)
func FetchCollection(rc *butlerd.RequestContext, params butlerd.FetchCollectionParams) (*butlerd.FetchCollectionResult, error) {
if params.CollectionID == 0 {
return nil, errors.New("collectionId must be non-zero")
}
ft := models.FetchTargetForCollection(params.CollectionID)
res := &butlerd.FetchCollectionResult{}
lazyfetch.Do(rc, ft, params, res, func(targets lazyfetch.Targets) {
_, client := rc.ProfileClient(params.ProfileID)
collRes, err := client.GetCollection(itchio.GetCollectionParams{
CollectionID: params.CollectionID,
})
models.Must(err)
collection := collRes.Collection
collection.CollectionGames = nil
rc.WithConn(func(conn *sqlite.Conn) {
models.MustSave(conn, collRes.Collection)
})
})
rc.WithConn(func(conn *sqlite.Conn) {
res.Collection = models.CollectionByID(conn, params.CollectionID)
})
if res.Collection == nil && !params.Fresh {
params.Fresh = true
return FetchCollection(rc, params)
}
return res, nil
}