/
release.go
202 lines (169 loc) · 5.18 KB
/
release.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package models
import (
"time"
"github.com/kamva/mgm/v3"
"github.com/kamva/mgm/v3/operator"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Release struct {
Document `bson:",inline"` // include mgm.DefaultModel
//ID primitive.ObjectID `json:"_id" bson:"_id,omitempty"`
//CreatedAt time.Time `json:"created_at" bson:"created_at"`
//UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
Type string `json:"type" bson:"type"`
Source string `json:"source" bson:"source"`
Raw string `json:"raw" bson:"raw"`
Title string `json:"title" bson:"title"`
Description string `json:"description" bson:"description"`
Size string `json:"size" bson:"size"`
View string `json:"view" bson:"view"`
Download string `json:"download" bson:"download"`
Infohash string `json:"infohash" bson:"infohash"`
Name string `json:"name" bson:"name"`
Season int `json:"season" bson:"season"`
Episode int `json:"episode" bson:"episode"`
Volume int `json:"volume" bson:"volume"`
Checksum string `json:"checksum" bson:"checksum"`
Group string `json:"group" bson:"group"`
Author string `json:"author" bson:"author"`
Verified bool `json:"verified" bson:"verified"`
Widescreen bool `json:"widescreen" bson:"widescreen"`
Uncensored bool `json:"uncensored" bson:"uncensored"`
Bluray bool `json:"bluray" bson:"bluray"`
Resolution string `json:"resolution" bson:"resolution"`
Encoding string `json:"encoding" bson:"encoding"`
Quality string `json:"quality" bson:"quality"`
Published time.Time `json:"published" bson:"published_at"`
}
func NewRelease() *Release {
return &Release{}
}
type ReleaseStore struct {
Client *mongo.Client
Database *mongo.Database
Collection *mgm.Collection
}
func NewReleaseStore(URI, db, name string) (*ReleaseStore, error) {
client, err := mgm.NewClient(CustomClientOptions(URI))
if err != nil {
return nil, err
}
database := client.Database(db)
collection := mgm.NewCollection(database, name)
store := &ReleaseStore{
Client: client,
Database: database,
Collection: collection,
}
return store, nil
}
func (s *ReleaseStore) FindByID(id primitive.ObjectID) (*Release, error) {
c := NewRelease()
err := s.Collection.FindByID(id, c)
if err != nil {
return nil, err
}
return c, nil
}
func (s *ReleaseStore) Find(id string) (*Release, error) {
oid, err := primitive.ObjectIDFromHex(id)
if err != nil {
return nil, err
}
return s.FindByID(oid)
}
func (s *ReleaseStore) Save(o *Release) error {
// TODO: if id is nil create otherwise, call update
return s.Collection.Create(o)
}
func (s *ReleaseStore) Update(o *Release) error {
return s.Collection.Update(o)
}
func (s *ReleaseStore) Delete(o *Release) error {
return s.Collection.Delete(o)
}
func (s *ReleaseStore) Query() *ReleaseQuery {
values := make(bson.M)
return &ReleaseQuery{
store: s,
values: values,
limit: 25,
skip: 0,
sort: bson.D{},
}
}
type ReleaseQuery struct {
store *ReleaseStore
values bson.M
limit int64
skip int64
sort bson.D
}
func (q *ReleaseQuery) addSort(field string, value int) *ReleaseQuery {
q.sort = append(q.sort, bson.E{Key: field, Value: value})
return q
}
func (q *ReleaseQuery) Asc(field string) *ReleaseQuery {
return q.addSort(field, 1)
}
func (q *ReleaseQuery) Desc(field string) *ReleaseQuery {
return q.addSort(field, -1)
}
func (q *ReleaseQuery) Limit(limit int) *ReleaseQuery {
q.limit = int64(limit)
return q
}
func (q *ReleaseQuery) Skip(skip int) *ReleaseQuery {
q.skip = int64(skip)
return q
}
func (q *ReleaseQuery) options() *options.FindOptions {
o := &options.FindOptions{}
o.SetLimit(q.limit)
o.SetSkip(q.skip)
o.SetSort(q.sort)
return o
}
func (q *ReleaseQuery) Run() ([]Release, error) {
result := make([]Release, 0)
err := q.store.Collection.SimpleFind(&result, q.values, q.options())
if err != nil {
return nil, err
}
return result, nil
}
func (q *ReleaseQuery) Where(key string, value interface{}) *ReleaseQuery {
q.values[key] = bson.M{operator.Eq: value}
return q
}
func (q *ReleaseQuery) In(key string, value interface{}) *ReleaseQuery {
q.values[key] = bson.M{operator.In: value}
return q
}
func (q *ReleaseQuery) NotIn(key string, value interface{}) *ReleaseQuery {
q.values[key] = bson.M{operator.Nin: value}
return q
}
func (q *ReleaseQuery) NotEqual(key string, value interface{}) *ReleaseQuery {
q.values[key] = bson.M{operator.Ne: value}
return q
}
func (q *ReleaseQuery) LessThan(key string, value interface{}) *ReleaseQuery {
q.values[key] = bson.M{operator.Lt: value}
return q
}
func (q *ReleaseQuery) LessThanEqual(key string, value interface{}) *ReleaseQuery {
q.values[key] = bson.M{operator.Lte: value}
return q
}
func (q *ReleaseQuery) GreaterThan(key string, value interface{}) *ReleaseQuery {
q.values[key] = bson.M{operator.Gt: value}
return q
}
func (q *ReleaseQuery) GreaterThanEqual(key string, value interface{}) *ReleaseQuery {
q.values[key] = bson.M{operator.Gte: value}
return q
}