diff options
Diffstat (limited to 'controller')
-rw-r--r-- | controller/controller.go | 35 |
1 files changed, 18 insertions, 17 deletions
diff --git a/controller/controller.go b/controller/controller.go index ffa89ce..252be73 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -10,6 +10,10 @@ import ( "github.com/gin-gonic/gin" ) +type Env struct { + Entries db.EntryModel +} + func spawnWorker(link string, output string) { work.Submit(func(ctx context.Context) error { yt.RunYtDlpProcess(link, output) @@ -17,38 +21,35 @@ func spawnWorker(link string, output string) { }) } -func GetEntries(c *gin.Context) { - var entries []db.YdlEntry - db.DB.Find(&entries) +func (e Env) GetEntries(c *gin.Context) { + entries := e.Entries.All() c.HTML(http.StatusOK, "index", entries) } -func GetEntry(c *gin.Context) { - var entry db.YdlEntry +func (e *Env) GetEntry(c *gin.Context) { id := c.Param("id") - where := "id = " + id - db.DB.Where(where).FirstOrInit(&entry) + entry := e.Entries.Find(id) c.HTML(http.StatusOK, "entry", entry) } -func UpdateEntry(c *gin.Context) { - var entry db.YdlEntry +func (e *Env) UpdateEntry(c *gin.Context) { + var entry db.Entry c.ShouldBind(&entry) - db.DB.Save(&entry) - c.HTML(http.StatusOK, "entry", entry) + e.Entries.Update(entry) + c.Redirect(http.StatusFound, "/") } -func CreateEntry(c *gin.Context) { - var entry db.YdlEntry +func (e *Env) CreateEntry(c *gin.Context) { + var entry db.Entry c.ShouldBind(&entry) - db.DB.Create(&entry) + e.Entries.Create(entry) spawnWorker(entry.Link, entry.OutputFolder) c.Redirect(http.StatusFound, "/") } -func DeleteEntry(c *gin.Context) { - var entry db.YdlEntry +func (e *Env) DeleteEntry(c *gin.Context) { + var entry db.Entry id := c.Param("id") - db.DB.Delete(&entry, id) + e.Entries.Delete(id) c.HTML(http.StatusOK, "entry", entry) } |