diff --git a/controller/controller.go b/controller/controller.go
index ffa89ce6ee06c6473aa5245a981b6122e3200e01..252be7358e94dbc89dc1ce75d29916d7381fa37e 100644
--- a/controller/controller.go
+++ b/controller/controller.go
@@ -10,6 +10,10 @@ 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)
@@ -17,38 +21,35 @@ return nil
})
}
-func GetEntries(c *gin.Context) {
- var entries []db.YdlEntry
- db.DB.Find(&entries)
+func (e Env) GetEntries(c *gin.Context) {
+ entries := e.Entries.All()
c.HTML(http.StatusOK, "index", entries)
}
-func GetEntry(c *gin.Context) {
- var entry db.YdlEntry
+func (e *Env) GetEntry(c *gin.Context) {
id := c.Param("id")
- where := "id = " + id
- db.DB.Where(where).FirstOrInit(&entry)
+ entry := e.Entries.Find(id)
c.HTML(http.StatusOK, "entry", entry)
}
-func UpdateEntry(c *gin.Context) {
- var entry db.YdlEntry
+func (e *Env) UpdateEntry(c *gin.Context) {
+ var entry db.Entry
c.ShouldBind(&entry)
- db.DB.Save(&entry)
- c.HTML(http.StatusOK, "entry", entry)
+ e.Entries.Update(entry)
+ c.Redirect(http.StatusFound, "/")
}
-func CreateEntry(c *gin.Context) {
- var entry db.YdlEntry
+func (e *Env) CreateEntry(c *gin.Context) {
+ var entry db.Entry
c.ShouldBind(&entry)
- db.DB.Create(&entry)
+ e.Entries.Create(entry)
spawnWorker(entry.Link, entry.OutputFolder)
c.Redirect(http.StatusFound, "/")
}
-func DeleteEntry(c *gin.Context) {
- var entry db.YdlEntry
+func (e *Env) DeleteEntry(c *gin.Context) {
+ var entry db.Entry
id := c.Param("id")
- db.DB.Delete(&entry, id)
+ e.Entries.Delete(id)
c.HTML(http.StatusOK, "entry", entry)
}
diff --git a/db/db.go b/db/db.go
index 128dcc89a0e73e3dc48cfac96c7e3327e8e246ea..5c904fd9821d6d805e59d51b0f9387010ca0bb99 100644
--- a/db/db.go
+++ b/db/db.go
@@ -17,5 +17,5 @@ if err != nil {
panic("failed to connect to the database.")
}
- DB.AutoMigrate(&YdlEntry{})
+ DB.AutoMigrate(&Entry{})
}
diff --git a/db/model.go b/db/model.go
index 6f35cd0ac9fd2b4f496ba5ab26b167342ee23951..4b814f9e78828b1f26edf72722cb9251c0745b7e 100644
--- a/db/model.go
+++ b/db/model.go
@@ -1,11 +1,42 @@
package db
-import "gorm.io/gorm"
+import (
+ "gorm.io/gorm"
+)
-type YdlEntry struct {
+type Entry struct {
gorm.Model
Title string
Link string
Format string
OutputFolder string
}
+
+type EntryModel struct {
+ DB *gorm.DB
+}
+
+func (m EntryModel) Find(id string) Entry {
+ var entry Entry
+ where := "id = " + id
+ m.DB.Where(where).FirstOrInit(&entry)
+ return entry
+}
+
+func (m EntryModel) All() []Entry {
+ var entries []Entry
+ m.DB.Find(&entries)
+ return entries
+}
+
+func (m EntryModel) Create(entry Entry) {
+ m.DB.Create(&entry)
+}
+
+func (m EntryModel) Update(entry Entry) {
+ m.DB.Save(&entry)
+}
+
+func (m EntryModel) Delete(id string) {
+ m.DB.Delete(&Entry{}, id)
+}
diff --git a/routes/routes.go b/routes/routes.go
index d78acc76dd57fe5b38e4ae04ae1a14df3d09f660..a960277cd5f3cddd61f4a70192c8d2ed3a1c1ed7 100644
--- a/routes/routes.go
+++ b/routes/routes.go
@@ -2,17 +2,23 @@ package routes
import (
"git.sr.ht/~gabrielgio/midr/controller"
+ "git.sr.ht/~gabrielgio/midr/db"
"github.com/gin-gonic/gin"
)
func HandleRequests() {
+
+ env := &controller.Env{
+ Entries: db.EntryModel{DB: db.DB},
+ }
+
r := gin.Default()
r.LoadHTMLGlob("templates/*")
- r.GET("/", controller.GetEntries)
- r.GET("entries/", controller.GetEntry)
- r.POST("entries/", controller.CreateEntry)
- r.GET("entries/:id", controller.GetEntry)
- r.POST("entries/:id", controller.UpdateEntry)
- r.DELETE("entries/:id", controller.DeleteEntry)
+ r.GET("/", env.GetEntries)
+ r.GET("entries/", env.GetEntry)
+ r.POST("entries/", env.CreateEntry)
+ r.GET("entries/:id", env.GetEntry)
+ r.POST("entries/:id", env.UpdateEntry)
+ r.DELETE("entries/:id", env.DeleteEntry)
r.Run(":8000")
}