1package controller
2
3import (
4 "context"
5 "net/http"
6
7 "git.sr.ht/~gabrielgio/midr/db"
8 "git.sr.ht/~gabrielgio/midr/yt"
9 work "git.sr.ht/~sircmpwn/dowork"
10 "github.com/gin-gonic/gin"
11)
12
13func spawnWorker(link string, output string) {
14 work.Submit(func(ctx context.Context) error {
15 yt.RunYtDlpProcess(link, output)
16 return nil
17 })
18}
19
20func GetEntries(c *gin.Context) {
21 var entries []db.YdlEntry
22 db.DB.Find(&entries)
23 c.HTML(http.StatusOK, "index", entries)
24}
25
26func GetEntry(c *gin.Context) {
27 var entry db.YdlEntry
28 id := c.Param("id")
29 where := "id = " + id
30 db.DB.Where(where).FirstOrInit(&entry)
31 c.HTML(http.StatusOK, "entry", entry)
32}
33
34func UpdateEntry(c *gin.Context) {
35 var entry db.YdlEntry
36 c.ShouldBind(&entry)
37 db.DB.Save(&entry)
38 c.HTML(http.StatusOK, "entry", entry)
39}
40
41func CreateEntry(c *gin.Context) {
42 var entry db.YdlEntry
43 c.ShouldBind(&entry)
44 db.DB.Create(&entry)
45 spawnWorker(entry.Link, entry.OutputFolder)
46 c.Redirect(http.StatusFound, "/")
47}
48
49func DeleteEntry(c *gin.Context) {
50 var entry db.YdlEntry
51 id := c.Param("id")
52 db.DB.Delete(&entry, id)
53 c.HTML(http.StatusOK, "entry", entry)
54}