This repository has been archived by the owner on Aug 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
user_library_sql.go
348 lines (313 loc) · 8.6 KB
/
user_library_sql.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package minutes
import (
"fmt"
"time"
"github.com/jinzhu/gorm"
)
// SqlUserLibrary is a read-write user-specific library
type SqlUserLibrary struct {
db *gorm.DB
}
// NewSqlUserLibrary accepts the database directory name and
// returns a new SqlUserLibrary instance
func NewSqlUserLibrary(db *gorm.DB) (*SqlUserLibrary, error) {
if db.HasTable(UserShow{}) == false {
if err := db.CreateTable(UserShow{}).Error; err != nil {
return nil, err
}
}
if db.HasTable(UserSeason{}) == false {
if err := db.CreateTable(UserSeason{}).Error; err != nil {
return nil, err
}
}
if db.HasTable(UserEpisode{}) == false {
if err := db.CreateTable(UserEpisode{}).Error; err != nil {
return nil, err
}
}
if db.HasTable(UserFile{}) == false {
if err := db.CreateTable(UserFile{}).Error; err != nil {
return nil, err
}
}
return &SqlUserLibrary{
db: db,
}, nil
}
// UpsertShow adds a new show
// or errors with ErrNotImplemented, or ErrInternalServer
func (squl *SqlUserLibrary) UpsertShow(show *UserShow) error {
ush := UserShow{}
err := squl.db.Where("ID = ?", show.ID).Find(&ush).Error
if err == gorm.ErrRecordNotFound {
if err := squl.db.Create(show).Error; err != nil {
return ErrInternalServer
}
return nil
}
if err != nil {
return ErrInternalServer
}
if err := squl.db.Model(&ush).Update(show).Error; err != nil {
return ErrInternalServer
}
return nil
}
// UpsertSeason adds or updates a season
// or errors with ErrNotImplemented, or ErrInternalServer, or ErrMissingShow
func (squl *SqlUserLibrary) UpsertSeason(season *UserSeason) error {
useas := UserSeason{}
err := squl.db.Where(
"show_id = ? AND number = ?",
season.ShowID,
season.Number,
).Find(&useas).Error
if err == gorm.ErrRecordNotFound {
if err := squl.db.Create(season).Error; err != nil {
return ErrInternalServer
}
return nil
}
if err != nil {
return ErrInternalServer
}
if err := squl.db.Model(&useas).Update(season).Error; err != nil {
return ErrInternalServer
}
return nil
}
// UpsertEpisode adds or updates a episode
// or errors with ErrNotImplemented, or ErrInternalServer, ErrMissingShow
// or ErrMissingSeason
func (squl *SqlUserLibrary) UpsertEpisode(episode *UserEpisode) error {
usep := UserEpisode{}
err := squl.db.Where(
"show_id = ? AND season = ? AND number = ?",
episode.ShowID, episode.Season, episode.Number,
).Find(&usep).Error
if err == gorm.ErrRecordNotFound {
if err := squl.db.Create(episode).Error; err != nil {
return ErrInternalServer
}
if err := squl.upsertFiles(episode); err != nil {
return ErrInternalServer
}
return nil
}
if err != nil {
return ErrInternalServer
}
if err = squl.db.Model(&usep).Updates(episode).Error; err != nil {
return ErrInternalServer
}
if err := squl.upsertFiles(episode); err != nil {
return ErrInternalServer
}
return nil
}
// GetShow returns a UserShow
// or errors with ErrNotFound, or ErrInternalServer
func (squl *SqlUserLibrary) GetShow(showID string) (*UserShow, error) {
ush := UserShow{}
err := squl.db.Where("ID = ?", showID).Find(&ush).Error
if err == gorm.ErrRecordNotFound {
return nil, ErrNotFound
}
if err != nil {
return nil, ErrInternalServer
}
return &ush, nil
}
// GetShows returns all Shows
// or errors with ErrNotImplemented, or ErrInternalServer
func (squl *SqlUserLibrary) GetShows() ([]*UserShow, error) {
ushs := []*UserShow{}
err := squl.db.Find(&ushs).Error
if err != nil {
return nil, ErrInternalServer
}
return ushs, nil
}
// GetSeasons returns all Seasons for a show
// or errors with ErrNotFound, or ErrInternalServer
func (squl *SqlUserLibrary) GetSeasons(showID string) ([]*UserSeason, error) {
useas := []*UserSeason{}
err := squl.db.Where("show_id = ?", showID).Find(&useas).Error
if err == gorm.ErrRecordNotFound {
return nil, ErrNotFound
}
if err != nil {
return nil, ErrInternalServer
}
return useas, nil
}
// GetSeason returns a UserSeason given a UserShow's ID and a UserSeason number
// or errors with ErrNotFound, ErrMissingShow, or ErrInternalServer
func (squl *SqlUserLibrary) GetSeason(showID string, seasonNumber int) (*UserSeason, error) {
useas := UserSeason{}
err := squl.db.Where(
"show_id = ? AND number = ?",
showID, seasonNumber,
).Find(&useas).Error
if err == gorm.ErrRecordNotFound {
return nil, ErrNotFound
}
if err != nil {
return nil, ErrInternalServer
}
return &useas, nil
}
// GetEpisodes returns all Episodes for a show and season number
// or errors with ErrNotFound, or ErrInternalServer
func (squl *SqlUserLibrary) GetEpisodes(showID string, seasonNumber int) ([]*UserEpisode, error) {
useps := []*UserEpisode{}
err := squl.db.Where(
"show_id = ? AND season = ?",
showID, seasonNumber,
).Find(&useps).Error
if err == gorm.ErrRecordNotFound {
return nil, ErrNotFound
}
if err != nil {
return nil, ErrInternalServer
}
for i, usep := range useps {
fis, err := squl.GetFiles(usep.ShowID, usep.Season, usep.Number)
if err != nil { // TODO Handle error
continue
}
if fis != nil {
useps[i].Files = fis
}
}
return useps, nil
}
// GetEpisode returns a UserEpisode given a UserShow's ID a UserSeason number
// and UserEpisode's number
// or errors with ErrNotFound, ErrMissingShow, or ErrInternalServer
func (squl *SqlUserLibrary) GetEpisode(showID string, seasonNumber, episodeNumber int) (*UserEpisode, error) {
usep := UserEpisode{}
err := squl.db.Where("show_id = ? AND season = ? AND number = ?",
showID, seasonNumber, episodeNumber,
).Find(&usep).Error
if err == gorm.ErrRecordNotFound {
return nil, ErrNotFound
}
if err != nil {
return nil, ErrInternalServer
}
fis, err := squl.GetFiles(usep.ShowID, usep.Season, usep.Number)
if err == nil && fis != nil { // TODO Handle error
usep.Files = fis
}
return &usep, nil
}
// GetFiles returns all UserFiles given a ShowID, Season, and Episode
func (squl *SqlUserLibrary) GetFiles(showID string, seasonNumber, episodeNumber int) ([]*UserFile, error) {
usfis := []*UserFile{}
err := squl.db.Where("show_id = ? AND season = ? AND episode = ?",
showID, seasonNumber, episodeNumber,
).Find(&usfis).Error
if err != nil {
return nil, ErrInternalServer
}
return usfis, nil
}
// QueryShowsByTitle returns all Shows that match a partial title ordered
// by their probability
// or errors with ErrInternalServer
func (squl *SqlUserLibrary) QueryShowsByTitle(title string) ([]*UserShow, error) {
ushs := []*UserShow{}
err := squl.db.Where(
"title LIKE ?",
fmt.Sprintf("%%%s%%", title),
).Find(&ushs).Error
if err == gorm.ErrRecordNotFound {
return nil, ErrNotFound
}
if err != nil {
return nil, ErrInternalServer
}
return ushs, nil
}
// QueryEpisodesForFinder
func (squl *SqlUserLibrary) QueryEpisodesForFinder() ([]*UserEpisode, error) {
usfs := []*UserFile{}
useps := []*UserEpisode{}
err := squl.db.Where(
"status = ? AND retry_time < ? and infohash = ''",
"pending",
time.Now().UTC().Unix(),
).Find(&usfs).Error
if err == gorm.ErrRecordNotFound {
return nil, ErrNotFound
}
if err != nil {
return nil, ErrInternalServer
}
for _, file := range usfs {
usep := UserEpisode{}
err := squl.db.Where(
"show_id = ? AND season = ? AND number = ?",
file.ShowID, file.Season, file.Episode,
).Find(&usep).Error
if err != nil {
return nil, ErrInternalServer
}
usep.Files = append(usep.Files, file)
useps = append(useps, &usep)
}
return useps, nil
}
// QueryEpisodesForDownloader
func (squl *SqlUserLibrary) QueryEpisodesForDownloader() ([]*UserEpisode, error) {
usfs := []*UserFile{}
useps := []*UserEpisode{}
err := squl.db.Where(
"status = ? AND retry_time < ? and infohash <> ''",
"found",
time.Now().UTC().Unix(),
).Find(&usfs).Error
if err == gorm.ErrRecordNotFound {
return nil, ErrNotFound
}
if err != nil {
return nil, ErrInternalServer
}
for _, file := range usfs {
usep := UserEpisode{}
err := squl.db.Where(
"show_id = ? AND season = ? AND number = ?",
file.ShowID, file.Season, file.Episode,
).Find(&usep).Error
if err != nil {
return nil, ErrInternalServer
}
usep.Files = append(usep.Files, file)
useps = append(useps, &usep)
}
return useps, nil
}
func (squl *SqlUserLibrary) upsertFiles(episode *UserEpisode) error {
squl.db.Where(
"show_id = ? AND season = ? AND episode = ?",
episode.ShowID, episode.Season, episode.Number,
).Delete(UserFile{})
for _, file := range episode.Files {
file.ShowID = episode.ShowID
file.Season = episode.Season
file.Episode = episode.Number
if err := squl.db.Create(file).Error; err != nil {
return ErrInternalServer
}
}
return nil
}
func (squl *SqlUserLibrary) Close() error {
err := squl.db.Close().Error
if err != nil {
return ErrInternalServer
}
return nil
}