forked from akhilrex/podgrab
/
dbfunctions.go
435 lines (375 loc) · 12.5 KB
/
dbfunctions.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package db
import (
"database/sql"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/ChunkLightTuna/podgrab/model"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
func GetPodcastByURL(url string, podcast *Podcast) error {
result := DB.Preload(clause.Associations).Where(&Podcast{URL: url}).First(&podcast)
return result.Error
}
func GetPodcastsByURLList(urls []string, podcasts *[]Podcast) error {
result := DB.Preload(clause.Associations).Where("url in ?", urls).First(&podcasts)
return result.Error
}
func GetAllPodcasts(podcasts *[]Podcast, sorting string) error {
if sorting == "" {
sorting = "created_at"
}
result := DB.Preload("Tags").Order(sorting).Find(&podcasts)
return result.Error
}
func GetAllPodcastItems(podcasts *[]PodcastItem) error {
result := DB.Preload("Podcast").Order("pub_date desc").Find(&podcasts)
return result.Error
}
func GetAllPodcastItemsWithoutSize() (*[]PodcastItem, error) {
var podcasts []PodcastItem
result := DB.Where("file_size<=?", 0).Order("pub_date desc").Find(&podcasts)
return &podcasts, result.Error
}
func getSortOrder(sorting model.EpisodeSort) string {
switch sorting {
case model.RELEASE_ASC:
return "pub_date asc"
case model.RELEASE_DESC:
return "pub_date desc"
case model.DURATION_ASC:
return "duration asc"
case model.DURATION_DESC:
return "duration desc"
default:
return "pub_date desc"
}
}
func GetPaginatedPodcastItemsNew(queryModel model.EpisodesFilter) (*[]PodcastItem, int64, error) {
var podcasts []PodcastItem
var total int64
query := DB.Debug().Preload("Podcast")
if queryModel.IsDownloaded != nil {
isDownloaded, err := strconv.ParseBool(*queryModel.IsDownloaded)
if err == nil {
if isDownloaded {
query = query.Where("download_status=?", Downloaded)
} else {
query = query.Where("download_status!=?", Downloaded)
}
}
}
if queryModel.IsPlayed != nil {
isPlayed, err := strconv.ParseBool(*queryModel.IsPlayed)
if err == nil {
if isPlayed {
query = query.Where("is_played=?", 1)
} else {
query = query.Where("is_played=?", 0)
}
}
}
if queryModel.Q != "" {
query = query.Where("UPPER(title) like ?", "%"+strings.TrimSpace(strings.ToUpper(queryModel.Q))+"%")
}
if len(queryModel.TagIds) > 0 {
query = query.Where("podcast_id in (select podcast_id from podcast_tags where tag_id in ?)", queryModel.TagIds)
}
if len(queryModel.PodcastIds) > 0 {
query = query.Where("podcast_id in ?", queryModel.PodcastIds)
}
totalsQuery := query.Order(getSortOrder(queryModel.Sorting)).Find(&podcasts)
totalsQuery.Count(&total)
result := query.Limit(queryModel.Count).Offset((queryModel.Page - 1) * queryModel.Count).Order("pub_date desc").Find(&podcasts)
return &podcasts, total, result.Error
}
func GetPaginatedPodcastItems(page int, count int, downloadedOnly *bool, playedOnly *bool, fromDate time.Time, podcasts *[]PodcastItem, total *int64) error {
query := DB.Preload("Podcast")
if downloadedOnly != nil {
if *downloadedOnly {
query = query.Where("download_status=?", Downloaded)
} else {
query = query.Where("download_status!=?", Downloaded)
}
}
if playedOnly != nil {
if *playedOnly {
query = query.Where("is_played=?", 1)
} else {
query = query.Where("is_played=?", 0)
}
}
if (fromDate != time.Time{}) {
query = query.Where("pub_date>=?", fromDate)
}
totalsQuery := query.Order("pub_date desc").Find(&podcasts)
totalsQuery.Count(total)
result := query.Limit(count).Offset((page - 1) * count).Order("pub_date desc").Find(&podcasts)
return result.Error
}
func GetPaginatedTags(page int, count int, tags *[]Tag, total *int64) error {
query := DB.Preload("Podcasts")
result := query.Limit(count).Offset((page - 1) * count).Order("created_at desc").Find(&tags)
query.Count(total)
return result.Error
}
func GetPodcastById(id string, podcast *Podcast) error {
result := DB.Preload("PodcastItems", func(db *gorm.DB) *gorm.DB {
return db.Order("podcast_items.pub_date DESC")
}).First(&podcast, "id=?", id)
return result.Error
}
func GetPodcastItemById(id string, podcastItem *PodcastItem) error {
result := DB.Preload(clause.Associations).First(&podcastItem, "id=?", id)
return result.Error
}
func DeletePodcastItemById(id string) error {
result := DB.Where("id=?", id).Delete(&PodcastItem{})
return result.Error
}
func DeletePodcastById(id string) error {
result := DB.Where("id=?", id).Delete(&Podcast{})
return result.Error
}
func DeleteTagById(id string) error {
result := DB.Where("id=?", id).Delete(&Tag{})
return result.Error
}
func GetAllPodcastItemsByPodcastId(podcastId string, podcastItems *[]PodcastItem) error {
result := DB.Preload(clause.Associations).Where(&PodcastItem{PodcastID: podcastId}).Find(&podcastItems)
return result.Error
}
func GetAllPodcastItemsByPodcastIds(podcastIds []string, podcastItems *[]PodcastItem) error {
result := DB.Preload(clause.Associations).Where("podcast_id in ?", podcastIds).Order("pub_date desc").Find(&podcastItems)
return result.Error
}
func GetAllPodcastItemsByIds(podcastItemIds []string) (*[]PodcastItem, error) {
var podcastItems []PodcastItem
var sb strings.Builder
sb.WriteString("\n CASE ID \n")
for i, v := range podcastItemIds {
sb.WriteString(fmt.Sprintf("WHEN '%v' THEN %v \n", v, i+1))
}
sb.WriteString(fmt.Sprintln("END"))
result := DB.Debug().Preload(clause.Associations).Where("id in ?", podcastItemIds).Order(sb.String()).Find(&podcastItems)
return &podcastItems, result.Error
}
func SetAllEpisodesToDownload(podcastId string) error {
result := DB.Model(PodcastItem{}).Where(&PodcastItem{PodcastID: podcastId, DownloadStatus: Deleted}).Update("download_status", NotDownloaded)
return result.Error
}
func UpdateLastEpisodeDateForPodcast(podcastId string, lastEpisode time.Time) error {
result := DB.Model(Podcast{}).Where("id=?", podcastId).Update("last_episode", lastEpisode)
return result.Error
}
func UpdatePodcastItemFileSize(podcastItemId string, size int64) error {
result := DB.Model(PodcastItem{}).Where("id=?", podcastItemId).Update("file_size", size)
return result.Error
}
func GetAllPodcastItemsWithoutImage() (*[]PodcastItem, error) {
var podcastItems []PodcastItem
result := DB.Preload(clause.Associations).Where("local_image is ?", nil).Where("image != ?", "").Where("download_status=?", Downloaded).Order("created_at desc").Find(&podcastItems)
//fmt.Println("To be downloaded : " + string(len(podcastItems)))
return &podcastItems, result.Error
}
func GetAllPodcastItemsToBeDownloaded() (*[]PodcastItem, error) {
var podcastItems []PodcastItem
result := DB.Preload(clause.Associations).Where("download_status=?", NotDownloaded).Find(&podcastItems)
//fmt.Println("To be downloaded : " + string(len(podcastItems)))
return &podcastItems, result.Error
}
func GetAllPodcastItemsAlreadyDownloaded() (*[]PodcastItem, error) {
var podcastItems []PodcastItem
result := DB.Preload(clause.Associations).Where("download_status=?", Downloaded).Find(&podcastItems)
return &podcastItems, result.Error
}
func GetPodcastEpisodeStats() (*[]PodcastItemStatsModel, error) {
var stats []PodcastItemStatsModel
result := DB.Model(&PodcastItem{}).Select("download_status,podcast_id, count(1) as count,sum(file_size) as size").Group("podcast_id,download_status").Find(&stats)
return &stats, result.Error
}
func GetPodcastEpisodeDiskStats() (PodcastItemConsolidateDiskStatsModel, error) {
var stats []PodcastItemDiskStatsModel
result := DB.Model(&PodcastItem{}).Select("download_status,count(1) as count,sum(file_size) as size").Group("download_status").Find(&stats)
dict := make(map[DownloadStatus]int64)
for _, stat := range stats {
dict[stat.DownloadStatus] = stat.Size
}
toReturn := PodcastItemConsolidateDiskStatsModel{
Downloaded: dict[Downloaded],
Downloading: dict[Downloading],
Deleted: dict[Deleted],
NotDownloaded: dict[NotDownloaded],
PendingDownload: dict[NotDownloaded] + dict[Downloading],
}
return toReturn, result.Error
}
func GetEpisodeNumber(podcastItemId, podcastId string) (int, error) {
var id string
var sequence int
row := DB.Raw(`;With cte as(
SELECT
id,
RANK() OVER (ORDER BY pub_date) as sequence
FROM
podcast_items
WHERE
podcast_id=?
)
select *
from cte
where id = ?
`, podcastId, podcastItemId).Row()
error := row.Scan(&id, &sequence)
return sequence, error
}
func ForceSetLastEpisodeDate(podcastId string) {
DB.Exec("update podcasts set last_episode = (select max(pi.pub_date) from podcast_items pi where pi.podcast_id = @id) where id = @id", sql.Named("id", podcastId))
}
func TogglePodcastPauseStatus(podcastId string, isPaused bool) error {
tx := DB.Debug().Exec("update podcasts set is_paused = @isPaused where id = @id", sql.Named("id", podcastId), sql.Named("isPaused", isPaused))
return tx.Error
}
func GetPodcastItemsByPodcastIdAndGUIDs(podcastId string, guids []string) (*[]PodcastItem, error) {
var podcastItems []PodcastItem
result := DB.Preload(clause.Associations).Where(&PodcastItem{PodcastID: podcastId}).Where("guid IN ?", guids).Find(&podcastItems)
return &podcastItems, result.Error
}
func GetPodcastItemByPodcastIdAndGUID(podcastId string, guid string, podcastItem *PodcastItem) error {
result := DB.Preload(clause.Associations).Where(&PodcastItem{PodcastID: podcastId, GUID: guid}).First(&podcastItem)
return result.Error
}
func GetPodcastByTitleAndAuthor(title string, author string, podcast *Podcast) error {
result := DB.Preload(clause.Associations).Where(&Podcast{Title: title, Author: author}).First(&podcast)
return result.Error
}
func CreatePodcast(podcast *Podcast) error {
tx := DB.Create(&podcast)
return tx.Error
}
func CreatePodcastItem(podcastItem *PodcastItem) error {
tx := DB.Omit("Podcast").Create(&podcastItem)
return tx.Error
}
func UpdatePodcast(podcast *Podcast) error {
tx := DB.Save(&podcast)
return tx.Error
}
func UpdatePodcastItem(podcastItem *PodcastItem) error {
tx := DB.Omit("Podcast").Save(&podcastItem)
return tx.Error
}
func UpdateSettings(setting *Setting) error {
tx := DB.Save(&setting)
return tx.Error
}
func GetOrCreateSetting() *Setting {
var setting Setting
result := DB.First(&setting)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
DB.Save(&Setting{})
DB.First(&setting)
}
return &setting
}
func GetLock(name string) *JobLock {
var jobLock JobLock
result := DB.Where("name = ?", name).First(&jobLock)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &JobLock{
Name: name,
}
}
return &jobLock
}
func Lock(name string, duration int) {
jobLock := GetLock(name)
if jobLock == nil {
jobLock = &JobLock{
Name: name,
}
}
jobLock.Duration = duration
jobLock.Date = time.Now()
if jobLock.ID == "" {
DB.Create(&jobLock)
} else {
DB.Save(&jobLock)
}
}
func Unlock(name string) {
jobLock := GetLock(name)
if jobLock == nil {
return
}
jobLock.Duration = 0
jobLock.Date = time.Time{}
DB.Save(&jobLock)
}
func UnlockMissedJobs() {
var jobLocks []JobLock
result := DB.Find(&jobLocks)
if result.Error != nil {
return
}
for _, job := range jobLocks {
if (job.Date == time.Time{}) {
continue
}
var duration time.Duration
duration = time.Duration(job.Duration)
d := job.Date.Add(time.Minute * duration)
if d.Before(time.Now()) {
fmt.Println(job.Name + " is unlocked")
Unlock(job.Name)
}
}
}
func GetAllTags(sorting string) (*[]Tag, error) {
var tags []Tag
if sorting == "" {
sorting = "created_at"
}
result := DB.Preload(clause.Associations).Order(sorting).Find(&tags)
return &tags, result.Error
}
func GetTagById(id string) (*Tag, error) {
var tag Tag
result := DB.Preload(clause.Associations).
First(&tag, "id=?", id)
return &tag, result.Error
}
func GetTagsByIds(ids []string) (*[]Tag, error) {
var tag []Tag
result := DB.Preload(clause.Associations).Where("id in ?", ids).Find(&tag)
return &tag, result.Error
}
func GetTagByLabel(label string) (*Tag, error) {
var tag Tag
result := DB.Preload(clause.Associations).
First(&tag, "label=?", label)
return &tag, result.Error
}
func CreateTag(tag *Tag) error {
tx := DB.Omit("Podcasts").Create(&tag)
return tx.Error
}
func UpdateTag(tag *Tag) error {
tx := DB.Omit("Podcast").Save(&tag)
return tx.Error
}
func AddTagToPodcast(id, tagId string) error {
tx := DB.Exec("INSERT INTO `podcast_tags` (`podcast_id`,`tag_id`) VALUES (?,?) ON CONFLICT DO NOTHING", id, tagId)
return tx.Error
}
func RemoveTagFromPodcast(id, tagId string) error {
tx := DB.Exec("DELETE FROM `podcast_tags` WHERE `podcast_id`=? AND `tag_id`=?", id, tagId)
return tx.Error
}
func UntagAllByTagId(tagId string) error {
tx := DB.Exec("DELETE FROM `podcast_tags` WHERE `tag_id`=?", tagId)
return tx.Error
}