-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
search_torrent_content.go
98 lines (90 loc) · 2.33 KB
/
search_torrent_content.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
package search
import (
"context"
"github.com/bitmagnet-io/bitmagnet/internal/database/dao"
"github.com/bitmagnet-io/bitmagnet/internal/database/query"
"github.com/bitmagnet-io/bitmagnet/internal/model"
"gorm.io/gen/field"
"gorm.io/gorm/clause"
)
type TorrentContentResultItem struct {
query.ResultItem
model.TorrentContent
}
type TorrentContentResult = query.GenericResult[TorrentContentResultItem]
type TorrentContentSearch interface {
TorrentContent(ctx context.Context, options ...query.Option) (TorrentContentResult, error)
}
func (s search) TorrentContent(ctx context.Context, options ...query.Option) (TorrentContentResult, error) {
return query.GenericQuery[TorrentContentResultItem](
ctx,
s.q,
query.Options(append([]query.Option{query.SelectAll()}, options...)...),
model.TableNameTorrentContent,
func(ctx context.Context, q *dao.Query) query.SubQuery {
return query.GenericSubQuery[dao.ITorrentContentDo]{
SubQuery: q.TorrentContent.WithContext(ctx).ReadDB(),
}
},
)
}
func TorrentContentDefaultOption() query.Option {
return query.Options(
query.DefaultOption(),
TorrentContentDefaultHydrate(),
TorrentContentCoreJoins(),
query.OrderBy(
clause.OrderByColumn{
Column: clause.Column{
Table: clause.CurrentTable,
Name: "published_at",
},
Desc: true,
},
clause.OrderByColumn{
Column: clause.Column{
Table: clause.CurrentTable,
Name: "updated_at",
},
Desc: true,
},
clause.OrderByColumn{
Column: clause.Column{
Table: clause.CurrentTable,
Name: "id",
},
},
),
)
}
func TorrentContentCoreJoins() query.Option {
return query.Options(
query.Join(func(q *dao.Query) []query.TableJoin {
return []query.TableJoin{
{
Table: q.Torrent,
On: []field.Expr{
q.TorrentContent.InfoHash.EqCol(q.Torrent.InfoHash),
},
Type: query.TableJoinTypeInner,
},
{
Table: q.Content,
On: []field.Expr{
q.TorrentContent.ContentType.EqCol(q.Content.Type),
q.TorrentContent.ContentSource.EqCol(q.Content.Source),
q.TorrentContent.ContentID.EqCol(q.Content.ID),
},
Type: query.TableJoinTypeLeft,
},
}
}),
ContentCoreJoins(),
)
}
func TorrentContentDefaultHydrate() query.Option {
return query.Options(
HydrateTorrentContentTorrent(),
HydrateTorrentContentContent(),
)
}