aboutsummaryrefslogtreecommitdiff
path: root/controller/controller.go
blob: 252be7358e94dbc89dc1ce75d29916d7381fa37e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package controller

import (
	"context"
	"net/http"

	"git.sr.ht/~gabrielgio/midr/db"
	"git.sr.ht/~gabrielgio/midr/yt"
	work "git.sr.ht/~sircmpwn/dowork"
	"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)
		return nil
	})
}

func (e Env) GetEntries(c *gin.Context) {
	entries := e.Entries.All()
	c.HTML(http.StatusOK, "index", entries)
}

func (e *Env) GetEntry(c *gin.Context) {
	id := c.Param("id")
	entry := e.Entries.Find(id)
	c.HTML(http.StatusOK, "entry", entry)
}

func (e *Env) UpdateEntry(c *gin.Context) {
	var entry db.Entry
	c.ShouldBind(&entry)
	e.Entries.Update(entry)
	c.Redirect(http.StatusFound, "/")
}

func (e *Env) CreateEntry(c *gin.Context) {
	var entry db.Entry
	c.ShouldBind(&entry)
	e.Entries.Create(entry)
	spawnWorker(entry.Link, entry.OutputFolder)
	c.Redirect(http.StatusFound, "/")
}

func (e *Env) DeleteEntry(c *gin.Context) {
	var entry db.Entry
	id := c.Param("id")
	e.Entries.Delete(id)
	c.HTML(http.StatusOK, "entry", entry)
}