From 33811c85d3fd101b5b936c9dda1ab549898cd66e Mon Sep 17 00:00:00 2001 From: Akhil Gupta Date: Wed, 17 Mar 2021 06:36:18 +0530 Subject: [PATCH] player improvement --- controllers/pages.go | 25 ++++++++++++++++++++----- db/dbfunctions.go | 15 ++++++++++----- service/podcastService.go | 4 ++-- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/controllers/pages.go b/controllers/pages.go index a1750e6..f56b013 100644 --- a/controllers/pages.go +++ b/controllers/pages.go @@ -7,6 +7,7 @@ import ( "math" "net/http" "os" + "sort" "strings" "time" @@ -124,7 +125,7 @@ func getItemsToPlay(itemId, podcastId string, tagIds []string) []db.PodcastItem podIds = append(podIds, pod.ID) } } - items = *service.GetAllPodcastItemsByPodcastIds(podIds) + items = *service.GetAllPodcastItemsByPodcastIds(podIds, "") } return items } @@ -134,6 +135,15 @@ func PlayerPage(c *gin.Context) { itemId, hasItemId := c.GetQuery("itemId") podcastId, hasPodcastId := c.GetQuery("podcastId") tagIds, hasTagIds := c.GetQueryArray("tagIds") + sortOrder, hasSortOrder := c.GetQuery("sort") + + if !hasSortOrder { + sortOrder = "desc" + } + sorting := "pub_date desc" + if sortOrder == "asc" { + sorting = "pub_date asc" + } title := "Podgrab" var items []db.PodcastItem var totalCount int64 @@ -156,7 +166,7 @@ func PlayerPage(c *gin.Context) { podIds = append(podIds, pod.ID) } } - items = *service.GetAllPodcastItemsByPodcastIds(podIds) + items = *service.GetAllPodcastItemsByPodcastIds(podIds, sorting) if len(tagNames) == 1 { title = fmt.Sprintf("Playing episodes with tag : %s", (tagNames[0])) } else { @@ -164,12 +174,17 @@ func PlayerPage(c *gin.Context) { } } else { title = "Playing Latest Episodes" - if err := db.GetPaginatedPodcastItems(1, 20, nil, nil, time.Time{}, &items, &totalCount); err != nil { + + if err := db.GetPaginatedPodcastItems(1, 20, nil, nil, time.Time{}, sorting, &items, &totalCount); err != nil { fmt.Println(err.Error()) } } setting := c.MustGet("setting").(*db.Setting) - + if sortOrder == "asc" { + sort.Slice(items, func(i, j int) bool { + return items[i].PubDate.Before(items[j].PubDate) + }) + } c.HTML(http.StatusOK, "player.html", gin.H{ "title": title, "podcastItems": items, @@ -252,7 +267,7 @@ func AllEpisodesPage(c *gin.Context) { } if err := db.GetPaginatedPodcastItems(page, count, - nil, filter.PlayedOnly, fromDate, + nil, filter.PlayedOnly, fromDate, "", &podcastItems, &totalCount); err == nil { setting := c.MustGet("setting").(*db.Setting) diff --git a/db/dbfunctions.go b/db/dbfunctions.go index b47c5cb..3d4a6dc 100644 --- a/db/dbfunctions.go +++ b/db/dbfunctions.go @@ -35,7 +35,7 @@ func GetAllPodcastItemsWithoutSize() (*[]PodcastItem, error) { result := DB.Where("file_size<=?", 0).Order("pub_date desc").Find(&podcasts) return &podcasts, result.Error } -func GetPaginatedPodcastItems(page int, count int, downloadedOnly *bool, playedOnly *bool, fromDate time.Time, podcasts *[]PodcastItem, total *int64) error { +func GetPaginatedPodcastItems(page int, count int, downloadedOnly *bool, playedOnly *bool, fromDate time.Time, sortOrder string, podcasts *[]PodcastItem, total *int64) error { query := DB.Preload("Podcast") if downloadedOnly != nil { if *downloadedOnly { @@ -54,8 +54,11 @@ func GetPaginatedPodcastItems(page int, count int, downloadedOnly *bool, playedO if (fromDate != time.Time{}) { query = query.Where("pub_date>=?", fromDate) } + if sortOrder == "" { + sortOrder = "pub_date desc" + } - totalsQuery := query.Order("pub_date desc").Find(&podcasts) + totalsQuery := query.Order(sortOrder).Find(&podcasts) totalsQuery.Count(total) result := query.Limit(count).Offset((page - 1) * count).Order("pub_date desc").Find(&podcasts) @@ -105,9 +108,11 @@ func GetAllPodcastItemsByPodcastId(podcastId string, podcastItems *[]PodcastItem 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) +func GetAllPodcastItemsByPodcastIds(podcastIds []string, sortOrder string, podcastItems *[]PodcastItem) error { + if sortOrder == "" { + sortOrder = "pub_date desc" + } + result := DB.Preload(clause.Associations).Where("podcast_id in ?", podcastIds).Order(sortOrder).Find(&podcastItems) return result.Error } diff --git a/service/podcastService.go b/service/podcastService.go index 8c83c25..e8ae052 100644 --- a/service/podcastService.go +++ b/service/podcastService.go @@ -59,10 +59,10 @@ func GetPodcastItemById(id string) *db.PodcastItem { return &podcastItem } -func GetAllPodcastItemsByPodcastIds(podcastIds []string) *[]db.PodcastItem { +func GetAllPodcastItemsByPodcastIds(podcastIds []string, sortOrder string) *[]db.PodcastItem { var podcastItems []db.PodcastItem - db.GetAllPodcastItemsByPodcastIds(podcastIds, &podcastItems) + db.GetAllPodcastItemsByPodcastIds(podcastIds, sortOrder, &podcastItems) return &podcastItems }