midr @ d06f6e1398c5dedb3359ff929c5aaf7317c73ce1

 1package controller
 2
 3import (
 4	"net/http"
 5
 6	"git.sr.ht/~gabrielgio/midr/db"
 7	"git.sr.ht/~gabrielgio/midr/worker"
 8	"github.com/gin-gonic/gin"
 9)
10
11type Env struct {
12	Entries db.EntryModel
13	Worker  worker.Worker
14}
15
16func (e Env) GetEntries(c *gin.Context) {
17	entries := e.Entries.All()
18	c.HTML(http.StatusOK, "index", entries)
19}
20
21func (e *Env) GetEntry(c *gin.Context) {
22	id := c.Param("id")
23	if id != "" {
24		entry := e.Entries.Find(id)
25		c.HTML(http.StatusOK, "entry", entry)
26	} else {
27		c.HTML(http.StatusOK, "entry", db.Entry{})
28	}
29}
30
31func (e *Env) UpdateEntry(c *gin.Context) {
32	var entry db.Entry
33	c.ShouldBind(&entry)
34	e.Entries.Update(entry)
35	c.Redirect(http.StatusFound, "/")
36}
37
38func (e *Env) CreateEntry(c *gin.Context) {
39	var entry db.Entry
40	c.ShouldBind(&entry)
41	e.Entries.Create(entry)
42	e.Worker.SpawnWorker(entry.ID, entry.Link, entry.OutputFolder)
43	c.Redirect(http.StatusFound, "/")
44}
45
46func (e *Env) DeleteEntry(c *gin.Context) {
47	var entry db.Entry
48	id := c.Param("id")
49	e.Entries.Delete(id)
50	c.HTML(http.StatusOK, "entry", entry)
51}