1diff --git a/controller/controller.go b/controller/controller.go
2index ffa89ce6ee06c6473aa5245a981b6122e3200e01..252be7358e94dbc89dc1ce75d29916d7381fa37e 100644
3--- a/controller/controller.go
4+++ b/controller/controller.go
5@@ -10,6 +10,10 @@ work "git.sr.ht/~sircmpwn/dowork"
6 "github.com/gin-gonic/gin"
7 )
8
9+type Env struct {
10+ Entries db.EntryModel
11+}
12+
13 func spawnWorker(link string, output string) {
14 work.Submit(func(ctx context.Context) error {
15 yt.RunYtDlpProcess(link, output)
16@@ -17,38 +21,35 @@ return nil
17 })
18 }
19
20-func GetEntries(c *gin.Context) {
21- var entries []db.YdlEntry
22- db.DB.Find(&entries)
23+func (e Env) GetEntries(c *gin.Context) {
24+ entries := e.Entries.All()
25 c.HTML(http.StatusOK, "index", entries)
26 }
27
28-func GetEntry(c *gin.Context) {
29- var entry db.YdlEntry
30+func (e *Env) GetEntry(c *gin.Context) {
31 id := c.Param("id")
32- where := "id = " + id
33- db.DB.Where(where).FirstOrInit(&entry)
34+ entry := e.Entries.Find(id)
35 c.HTML(http.StatusOK, "entry", entry)
36 }
37
38-func UpdateEntry(c *gin.Context) {
39- var entry db.YdlEntry
40+func (e *Env) UpdateEntry(c *gin.Context) {
41+ var entry db.Entry
42 c.ShouldBind(&entry)
43- db.DB.Save(&entry)
44- c.HTML(http.StatusOK, "entry", entry)
45+ e.Entries.Update(entry)
46+ c.Redirect(http.StatusFound, "/")
47 }
48
49-func CreateEntry(c *gin.Context) {
50- var entry db.YdlEntry
51+func (e *Env) CreateEntry(c *gin.Context) {
52+ var entry db.Entry
53 c.ShouldBind(&entry)
54- db.DB.Create(&entry)
55+ e.Entries.Create(entry)
56 spawnWorker(entry.Link, entry.OutputFolder)
57 c.Redirect(http.StatusFound, "/")
58 }
59
60-func DeleteEntry(c *gin.Context) {
61- var entry db.YdlEntry
62+func (e *Env) DeleteEntry(c *gin.Context) {
63+ var entry db.Entry
64 id := c.Param("id")
65- db.DB.Delete(&entry, id)
66+ e.Entries.Delete(id)
67 c.HTML(http.StatusOK, "entry", entry)
68 }
69diff --git a/db/db.go b/db/db.go
70index 128dcc89a0e73e3dc48cfac96c7e3327e8e246ea..5c904fd9821d6d805e59d51b0f9387010ca0bb99 100644
71--- a/db/db.go
72+++ b/db/db.go
73@@ -17,5 +17,5 @@ if err != nil {
74 panic("failed to connect to the database.")
75 }
76
77- DB.AutoMigrate(&YdlEntry{})
78+ DB.AutoMigrate(&Entry{})
79 }
80diff --git a/db/model.go b/db/model.go
81index 6f35cd0ac9fd2b4f496ba5ab26b167342ee23951..4b814f9e78828b1f26edf72722cb9251c0745b7e 100644
82--- a/db/model.go
83+++ b/db/model.go
84@@ -1,11 +1,42 @@
85 package db
86
87-import "gorm.io/gorm"
88+import (
89+ "gorm.io/gorm"
90+)
91
92-type YdlEntry struct {
93+type Entry struct {
94 gorm.Model
95 Title string
96 Link string
97 Format string
98 OutputFolder string
99 }
100+
101+type EntryModel struct {
102+ DB *gorm.DB
103+}
104+
105+func (m EntryModel) Find(id string) Entry {
106+ var entry Entry
107+ where := "id = " + id
108+ m.DB.Where(where).FirstOrInit(&entry)
109+ return entry
110+}
111+
112+func (m EntryModel) All() []Entry {
113+ var entries []Entry
114+ m.DB.Find(&entries)
115+ return entries
116+}
117+
118+func (m EntryModel) Create(entry Entry) {
119+ m.DB.Create(&entry)
120+}
121+
122+func (m EntryModel) Update(entry Entry) {
123+ m.DB.Save(&entry)
124+}
125+
126+func (m EntryModel) Delete(id string) {
127+ m.DB.Delete(&Entry{}, id)
128+}
129diff --git a/routes/routes.go b/routes/routes.go
130index d78acc76dd57fe5b38e4ae04ae1a14df3d09f660..a960277cd5f3cddd61f4a70192c8d2ed3a1c1ed7 100644
131--- a/routes/routes.go
132+++ b/routes/routes.go
133@@ -2,17 +2,23 @@ package routes
134
135 import (
136 "git.sr.ht/~gabrielgio/midr/controller"
137+ "git.sr.ht/~gabrielgio/midr/db"
138 "github.com/gin-gonic/gin"
139 )
140
141 func HandleRequests() {
142+
143+ env := &controller.Env{
144+ Entries: db.EntryModel{DB: db.DB},
145+ }
146+
147 r := gin.Default()
148 r.LoadHTMLGlob("templates/*")
149- r.GET("/", controller.GetEntries)
150- r.GET("entries/", controller.GetEntry)
151- r.POST("entries/", controller.CreateEntry)
152- r.GET("entries/:id", controller.GetEntry)
153- r.POST("entries/:id", controller.UpdateEntry)
154- r.DELETE("entries/:id", controller.DeleteEntry)
155+ r.GET("/", env.GetEntries)
156+ r.GET("entries/", env.GetEntry)
157+ r.POST("entries/", env.CreateEntry)
158+ r.GET("entries/:id", env.GetEntry)
159+ r.POST("entries/:id", env.UpdateEntry)
160+ r.DELETE("entries/:id", env.DeleteEntry)
161 r.Run(":8000")
162 }