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]
[![Forks][forks-shield]][forks-url]

@ -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,17 +195,35 @@ func BackupsPage(c *gin.Context) {
}
func AllEpisodesPage(c *gin.Context) {
var pagination Pagination
if c.ShouldBindQuery(&pagination) == nil {
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
if err := db.GetPaginatedPodcastItems(page, count, pagination.DownloadedOnly, &podcastItems, &totalCount); err == nil {
//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
@ -215,7 +233,7 @@ func AllEpisodesPage(c *gin.Context) {
if page > 1 {
previousPage = page - 1
}
c.HTML(http.StatusOK, "episodes.html", gin.H{
toReturn := gin.H{
"title": "All Episodes",
"podcastItems": podcastItems,
"setting": setting,
@ -225,13 +243,12 @@ func AllEpisodesPage(c *gin.Context) {
"totalPages": totalPages,
"nextPage": nextPage,
"previousPage": previousPage,
"downloadedOnly": pagination.DownloadedOnly,
})
} else {
c.JSON(http.StatusBadRequest, err)
"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)
}
}

@ -41,7 +41,12 @@ 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"`
}
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 {

@ -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 {
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
}

Loading…
Cancel
Save