From 3ed9a8422ff34b636ab4f159a36cfb37500c3ada Mon Sep 17 00:00:00 2001 From: Akhil Gupta Date: Wed, 10 Feb 2021 06:23:46 +0530 Subject: [PATCH] basic hygiene --- Readme.md | 20 ---------- controllers/pages.go | 85 +++++++++++++++++++++++++----------------- controllers/podcast.go | 11 ++++-- db/dbfunctions.go | 22 +++++++++-- 4 files changed, 77 insertions(+), 61 deletions(-) diff --git a/Readme.md b/Readme.md index 5e1ba86..edc5f2b 100644 --- a/Readme.md +++ b/Readme.md @@ -1,23 +1,3 @@ - - - - [![Contributors][contributors-shield]][contributors-url] [![Forks][forks-shield]][forks-url] diff --git a/controllers/pages.go b/controllers/pages.go index 20e4905..650edfd 100644 --- a/controllers/pages.go +++ b/controllers/pages.go @@ -135,7 +135,7 @@ func PlayerPage(c *gin.Context) { totalCount = int64(len(items)) } else { title = "Playing Latest Episodes" - if err := db.GetPaginatedPodcastItems(1, 20, false, &items, &totalCount); err != nil { + if err := db.GetPaginatedPodcastItems(1, 20, nil, nil, time.Time{}, &items, &totalCount); err != nil { fmt.Println(err.Error()) } } @@ -195,43 +195,60 @@ func BackupsPage(c *gin.Context) { } func AllEpisodesPage(c *gin.Context) { var pagination Pagination - if c.ShouldBindQuery(&pagination) == nil { - var page, count int - if page = pagination.Page; page == 0 { - page = 1 + var page, count int + c.ShouldBindQuery(&pagination) + if page = pagination.Page; page == 0 { + page = 1 + } + if count = pagination.Count; count == 0 { + count = 10 + } + + var filter EpisodesFilter + c.ShouldBindQuery(&filter) + + var podcastItems []db.PodcastItem + var totalCount int64 + //fmt.Printf("%+v\n", filter) + fromDate := time.Time{} + if filter.FromDate != "" { + parsedDate, err := time.Parse("2006-01-02", strings.Trim(filter.FromDate, "\"")) + if err != nil { + fromDate = time.Time{} + } else { + fromDate = parsedDate + } + } + + if err := db.GetPaginatedPodcastItems(page, count, + filter.DownloadedOnly, filter.PlayedOnly, fromDate, + &podcastItems, &totalCount); err == nil { + + setting := c.MustGet("setting").(*db.Setting) + totalPages := math.Ceil(float64(totalCount) / float64(count)) + nextPage, previousPage := 0, 0 + if float64(page) < totalPages { + nextPage = page + 1 } - if count = pagination.Count; count == 0 { - count = 10 + if page > 1 { + previousPage = page - 1 } - var podcastItems []db.PodcastItem - var totalCount int64 - if err := db.GetPaginatedPodcastItems(page, count, pagination.DownloadedOnly, &podcastItems, &totalCount); err == nil { - setting := c.MustGet("setting").(*db.Setting) - totalPages := math.Ceil(float64(totalCount) / float64(count)) - nextPage, previousPage := 0, 0 - if float64(page) < totalPages { - nextPage = page + 1 - } - if page > 1 { - previousPage = page - 1 - } - c.HTML(http.StatusOK, "episodes.html", gin.H{ - "title": "All Episodes", - "podcastItems": podcastItems, - "setting": setting, - "page": page, - "count": count, - "totalCount": totalCount, - "totalPages": totalPages, - "nextPage": nextPage, - "previousPage": previousPage, - "downloadedOnly": pagination.DownloadedOnly, - }) - } else { - c.JSON(http.StatusBadRequest, err) + toReturn := gin.H{ + "title": "All Episodes", + "podcastItems": podcastItems, + "setting": setting, + "page": page, + "count": count, + "totalCount": totalCount, + "totalPages": totalPages, + "nextPage": nextPage, + "previousPage": previousPage, + "downloadedOnly": filter.DownloadedOnly, } + fmt.Printf("%+v\n", totalCount) + c.HTML(http.StatusOK, "episodes.html", toReturn) } else { - c.JSON(http.StatusBadRequest, gin.H{"message": "Invalid request"}) + c.JSON(http.StatusBadRequest, err) } } diff --git a/controllers/podcast.go b/controllers/podcast.go index 5991199..c737b40 100644 --- a/controllers/podcast.go +++ b/controllers/podcast.go @@ -39,9 +39,14 @@ type SearchByIdQuery struct { } type Pagination struct { - Page int `uri:"page" query:"page" json:"page" form:"page"` - Count int `uri:"count" query:"count" json:"count" form:"count"` - DownloadedOnly bool `uri:"downloadedOnly" query:"downloadedOnly" json:"downloadedOnly" form:"downloadedOnly"` + Page int `uri:"page" query:"page" json:"page" form:"page"` + Count int `uri:"count" query:"count" json:"count" form:"count"` +} + +type EpisodesFilter struct { + DownloadedOnly *bool `uri:"downloadedOnly" query:"downloadedOnly" json:"downloadedOnly" form:"downloadedOnly"` + PlayedOnly *bool `uri:"playedOnly" query:"playedOnly" json:"playedOnly" form:"playedOnly"` + FromDate string `uri:"fromDate" query:"fromDate" json:"fromDate" form:"fromDate"` } type PatchPodcastItem struct { diff --git a/db/dbfunctions.go b/db/dbfunctions.go index 33738f9..6d92eaf 100644 --- a/db/dbfunctions.go +++ b/db/dbfunctions.go @@ -31,15 +31,29 @@ func GetAllPodcastItems(podcasts *[]PodcastItem) error { result := DB.Preload("Podcast").Order("pub_date desc").Find(&podcasts) return result.Error } -func GetPaginatedPodcastItems(page int, count int, downloadedOnly bool, podcasts *[]PodcastItem, total *int64) error { +func GetPaginatedPodcastItems(page int, count int, downloadedOnly *bool, playedOnly *bool, fromDate time.Time, podcasts *[]PodcastItem, total *int64) error { query := DB.Debug().Preload("Podcast") - if downloadedOnly { - query = query.Where("download_status=?", Downloaded) + 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) } result := query.Limit(count).Offset((page - 1) * count).Order("pub_date desc").Find(&podcasts) - DB.Model(&PodcastItem{}).Count(total) + query.Count(total) return result.Error }