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
13type Env struct {
14 Entries db.EntryModel
15}
16
17func spawnWorker(link string, output string) {
18 work.Submit(func(ctx context.Context) error {
19 yt.RunYtDlpProcess(link, output)
20 return nil
21 })
22}
23
24func (e Env) GetEntries(c *gin.Context) {
25 entries := e.Entries.All()
26 c.HTML(http.StatusOK, "index", entries)
27}
28
29func (e *Env) GetEntry(c *gin.Context) {
30 id := c.Param("id")
31 entry := e.Entries.Find(id)
32 c.HTML(http.StatusOK, "entry", entry)
33}
34
35func (e *Env) UpdateEntry(c *gin.Context) {
36 var entry db.Entry
37 c.ShouldBind(&entry)
38 e.Entries.Update(entry)
39 c.Redirect(http.StatusFound, "/")
40}
41
42func (e *Env) CreateEntry(c *gin.Context) {
43 var entry db.Entry
44 c.ShouldBind(&entry)
45 e.Entries.Create(entry)
46 spawnWorker(entry.Link, entry.OutputFolder)
47 c.Redirect(http.StatusFound, "/")
48}
49
50func (e *Env) DeleteEntry(c *gin.Context) {
51 var entry db.Entry
52 id := c.Param("id")
53 e.Entries.Delete(id)
54 c.HTML(http.StatusOK, "entry", entry)
55}