basic hygiene

pull/52/head
Akhil Gupta 4 years ago
parent 3f22953a66
commit 3ed9a8422f

@ -1,23 +1,3 @@
<!--
*** Thanks for checking out this README Template. If you have a suggestion that would
*** make this better, please fork the repo and create a pull request or simply open
*** an issue with the tag "enhancement".
*** Thanks again! Now go create something AMAZING! :D
***
***
***
*** To avoid retyping too much info. Do a search and replace for the following:
*** akhilrex, podgrab, akhilrex, email
-->
<!-- PROJECT SHIELDS -->
<!--
*** I'm using markdown "reference style" links for readability.
*** Reference links are enclosed in brackets [ ] instead of parentheses ( ).
*** See the bottom of this document for the declaration of the reference variables
*** for contributors-url, forks-url, etc. This is an optional, concise syntax you may use.
*** https://www.markdownguide.org/basic-syntax/#reference-style-links
-->
[![Contributors][contributors-shield]][contributors-url] [![Contributors][contributors-shield]][contributors-url]
[![Forks][forks-shield]][forks-url] [![Forks][forks-shield]][forks-url]

@ -135,7 +135,7 @@ func PlayerPage(c *gin.Context) {
totalCount = int64(len(items)) totalCount = int64(len(items))
} else { } else {
title = "Playing Latest Episodes" 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()) fmt.Println(err.Error())
} }
} }
@ -195,43 +195,60 @@ func BackupsPage(c *gin.Context) {
} }
func AllEpisodesPage(c *gin.Context) { func AllEpisodesPage(c *gin.Context) {
var pagination Pagination var pagination Pagination
if c.ShouldBindQuery(&pagination) == nil { var page, count int
var page, count int c.ShouldBindQuery(&pagination)
if page = pagination.Page; page == 0 { if page = pagination.Page; page == 0 {
page = 1 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 { if page > 1 {
count = 10 previousPage = page - 1
} }
var podcastItems []db.PodcastItem toReturn := gin.H{
var totalCount int64 "title": "All Episodes",
if err := db.GetPaginatedPodcastItems(page, count, pagination.DownloadedOnly, &podcastItems, &totalCount); err == nil { "podcastItems": podcastItems,
setting := c.MustGet("setting").(*db.Setting) "setting": setting,
totalPages := math.Ceil(float64(totalCount) / float64(count)) "page": page,
nextPage, previousPage := 0, 0 "count": count,
if float64(page) < totalPages { "totalCount": totalCount,
nextPage = page + 1 "totalPages": totalPages,
} "nextPage": nextPage,
if page > 1 { "previousPage": previousPage,
previousPage = page - 1 "downloadedOnly": filter.DownloadedOnly,
}
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)
} }
fmt.Printf("%+v\n", totalCount)
c.HTML(http.StatusOK, "episodes.html", toReturn)
} else { } else {
c.JSON(http.StatusBadRequest, gin.H{"message": "Invalid request"}) c.JSON(http.StatusBadRequest, err)
} }
} }

@ -39,9 +39,14 @@ type SearchByIdQuery struct {
} }
type Pagination struct { type Pagination struct {
Page int `uri:"page" query:"page" json:"page" form:"page"` Page int `uri:"page" query:"page" json:"page" form:"page"`
Count int `uri:"count" query:"count" json:"count" form:"count"` Count int `uri:"count" query:"count" json:"count" form:"count"`
DownloadedOnly bool `uri:"downloadedOnly" query:"downloadedOnly" json:"downloadedOnly" form:"downloadedOnly"` }
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 { type PatchPodcastItem struct {

@ -31,15 +31,29 @@ func GetAllPodcastItems(podcasts *[]PodcastItem) error {
result := DB.Preload("Podcast").Order("pub_date desc").Find(&podcasts) result := DB.Preload("Podcast").Order("pub_date desc").Find(&podcasts)
return result.Error 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") query := DB.Debug().Preload("Podcast")
if downloadedOnly { if downloadedOnly != nil {
query = query.Where("download_status=?", Downloaded) 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) 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 return result.Error
} }

Loading…
Cancel
Save