/
fetch_caves.go
70 lines (60 loc) 路 2.12 KB
/
fetch_caves.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
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/pager"
"github.com/itchio/hades"
)
func FetchCaves(rc *butlerd.RequestContext, params butlerd.FetchCavesParams) (*butlerd.FetchCavesResult, error) {
res := &butlerd.FetchCavesResult{}
rc.WithConn(func(conn *sqlite.Conn) {
var cond = builder.NewCond()
joinGames := false
search := hades.Search{}
switch params.SortBy {
case "title":
ordering := pager.Ordering("ASC", params.Reverse)
search = search.OrderBy("games.title " + ordering)
joinGames = true
case "playTime":
ordering := pager.Ordering("DESC", params.Reverse)
search = search.OrderBy("caves.seconds_run " + ordering)
case "installedAt":
ordering := pager.Ordering("DESC", params.Reverse)
search = search.OrderBy("caves.installed_at " + ordering)
case "installedSize":
ordering := pager.Ordering("DESC", params.Reverse)
search = search.OrderBy("caves.installed_size " + ordering)
case "lastTouched", "":
ordering := pager.Ordering("DESC", params.Reverse)
search = search.OrderBy("coalesce(caves.last_touched_at, caves.installed_at) " + ordering)
}
if params.Filters.Classification != "" {
cond = builder.And(cond, builder.Eq{"games.classification": params.Filters.Classification})
joinGames = true
}
if params.Filters.InstallLocationID != "" {
cond = builder.And(cond, builder.Eq{"caves.install_location_id": params.Filters.InstallLocationID})
}
if params.Filters.GameID != 0 {
cond = builder.And(cond, builder.Eq{"caves.game_id": params.Filters.GameID})
}
if params.Search != "" {
cond = builder.And(cond, builder.Like{"games.title", params.Search})
joinGames = true
}
if joinGames {
search = search.Join("games", "games.id = caves.game_id")
}
var items []*models.Cave
pg := pager.New(params)
res.NextCursor = pg.Fetch(conn, &items, cond, search)
models.PreloadCaves(conn, items)
for _, cave := range items {
res.Items = append(res.Items, FormatCave(conn, cave))
}
})
return res, nil
}