added settings page

pull/24/head
Akhil Gupta 4 years ago
parent e66eb6093e
commit 94f893465c

@ -57,6 +57,7 @@
<li class="navbar-item"><a class="navbar-link" href="/">Home</a></li> <li class="navbar-item"><a class="navbar-link" href="/">Home</a></li>
<li class="navbar-item"><a class="navbar-link" href="/episodes">Episodes</a></li> <li class="navbar-item"><a class="navbar-link" href="/episodes">Episodes</a></li>
<li class="navbar-item"><a class="navbar-link" href="/add">Add Podcast</a></li> <li class="navbar-item"><a class="navbar-link" href="/add">Add Podcast</a></li>
<li class="navbar-item"><a class="navbar-link" href="/settings">Settings</a></li>
</ul> </ul>
</div> </div>

@ -0,0 +1,108 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/skeleton/2.0.4/skeleton.min.css" integrity="sha512-EZLkOqwILORob+p0BXZc+Vm3RgJBOe1Iq/0fiI7r/wJgzOFZMlsqTa29UEl6v6U6gsV4uIpsNZoV32YZqrCRCQ==" crossorigin="anonymous" />
<title>PodGrab</title>
{{template "commoncss"}}
<style>
.button-delete{
background-color: indianred;
color:wheat;
}
img{
display: none;
}
/* Larger than tablet */
@media (min-width: 750px) {
img{
display: block
}
}
</style>
</head>
<body>
<div class="container">
<section class="header">
<h1>{{ .title }}</h1>
</section>
{{template "navbar"}}
<br>
<div class="row" id="app">
<form action="/saveSettings" method="post" @submit="saveSettings">
<label for="downloadOnAdd">
<input type="checkbox" name="downloadOnAdd" v-model="downloadOnAdd">
<span class="label-body">Download episodes whenever new podcast is added</span>
</label>
<div v-if="downloadOnAdd">
<label for="initialDownloadCount" >
<span class="label-body">How many episodes to be downloaded when a new podcast is added</span>
</label>
<input type="number" name="initialDownloadCount" v-model.number="initialDownloadCount" min="0">
</div>
<label for="autoDownload">
<input type="checkbox" name="autoDownload" v-model="autoDownload">
<span class="label-body">Automatically download new episodes to the disk</span>
</label>
<input type="submit" value="Save" class="button button-primary">
</form>
</div>
{{template "scripts"}}
<script>
var app = new Vue({
delimiters: ['${', '}'],
el: '#app',
methods:{
saveSettings:function(e){
e.preventDefault();
var self=this;
self.searching=true;
axios.post("/settings",{
downloadOnAdd: self.downloadOnAdd,
initialDownloadCount:self.initialDownloadCount,
autoDownload:self.autoDownload
})
.then(function(response){
Vue.toasted.show('Settings saved successfully.' ,{
theme: "bubble",
position: "top-right",
duration : 5000
})
})
.catch(function(error){
if(error.response){
Vue.toasted.show(error.response.data?.message, {
theme: "bubble",
position: "top-right",
duration : 5000
})
}
}).
then(function(){
})
return false;
}
},
data: {
downloadOnAdd: {{ .setting.DownloadOnAdd }},
initialDownloadCount: {{ .setting.InitialDownloadCount }},
autoDownload: {{ .setting.AutoDownload }},
},
})
</script>
</body>
</html>

@ -14,6 +14,11 @@ import (
type SearchGPodderData struct { type SearchGPodderData struct {
Q string `binding:"required" form:"q" json:"q" query:"q"` Q string `binding:"required" form:"q" json:"q" query:"q"`
} }
type SettingModel struct {
DownloadOnAdd bool `form:"downloadOnAdd" json:"downloadOnAdd" query:"downloadOnAdd"`
InitialDownloadCount int `form:"initialDownloadCount" json:"initialDownloadCount" query:"initialDownloadCount"`
AutoDownload bool `form:"autoDownload" json:"autoDownload" query:"autoDownload"`
}
func AddPage(c *gin.Context) { func AddPage(c *gin.Context) {
@ -50,6 +55,15 @@ func PodcastPage(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"}) c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
} }
}
func SettingsPage(c *gin.Context) {
setting := c.MustGet("setting").(*db.Setting)
c.HTML(http.StatusOK, "settings.html", gin.H{
"setting": setting,
"title": "Update your preferences",
})
} }
func AllEpisodesPage(c *gin.Context) { func AllEpisodesPage(c *gin.Context) {
var pagination Pagination var pagination Pagination

@ -133,3 +133,25 @@ func AddPodcast(c *gin.Context) {
c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()}) c.JSON(http.StatusBadRequest, gin.H{"message": err.Error()})
} }
} }
func UpdateSetting(c *gin.Context) {
var model SettingModel
err := c.ShouldBind(&model)
if err == nil {
err = service.UpdateSettings(model.DownloadOnAdd, model.InitialDownloadCount, model.AutoDownload)
if err == nil {
c.JSON(200, gin.H{"message": "Success"})
} else {
c.JSON(http.StatusBadRequest, err)
}
} else {
// fmt.Println(err.Error())
c.JSON(http.StatusBadRequest, err)
}
}

@ -94,6 +94,10 @@ func UpdatePodcastItem(podcastItem *PodcastItem) error {
tx := DB.Omit("Podcast").Save(&podcastItem) tx := DB.Omit("Podcast").Save(&podcastItem)
return tx.Error return tx.Error
} }
func UpdateSettings(setting *Setting) error {
tx := DB.Save(&setting)
return tx.Error
}
func GetOrCreateSetting() *Setting { func GetOrCreateSetting() *Setting {
var setting Setting var setting Setting
result := DB.First(&setting) result := DB.First(&setting)

@ -83,6 +83,8 @@ func main() {
r.GET("/", controllers.HomePage) r.GET("/", controllers.HomePage)
r.GET("/podcasts/:id/view", controllers.PodcastPage) r.GET("/podcasts/:id/view", controllers.PodcastPage)
r.GET("/episodes", controllers.AllEpisodesPage) r.GET("/episodes", controllers.AllEpisodesPage)
r.GET("/settings", controllers.SettingsPage)
r.POST("/settings", controllers.UpdateSetting)
go assetEnv() go assetEnv()
go intiCron() go intiCron()

@ -214,3 +214,13 @@ func GetSearchFromItunes(pod model.ItunesSingleResult) *model.CommonSearchResult
return p return p
} }
func UpdateSettings(downloadOnAdd bool, initialDownloadCount int, autoDownload bool) error {
setting := db.GetOrCreateSetting()
setting.AutoDownload = autoDownload
setting.DownloadOnAdd = downloadOnAdd
setting.InitialDownloadCount = initialDownloadCount
return db.UpdateSettings(setting)
}

Loading…
Cancel
Save