diff options
-rw-r--r-- | controller/controller.go | 35 | ||||
-rw-r--r-- | db/db.go | 2 | ||||
-rw-r--r-- | db/model.go | 35 | ||||
-rw-r--r-- | routes/routes.go | 18 |
4 files changed, 64 insertions, 26 deletions
diff --git a/controller/controller.go b/controller/controller.go index ffa89ce..252be73 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -10,6 +10,10 @@ import ( "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 @@ func spawnWorker(link string, output string) { }) } -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) } @@ -17,5 +17,5 @@ func ConnectDb() { panic("failed to connect to the database.") } - DB.AutoMigrate(&YdlEntry{}) + DB.AutoMigrate(&Entry{}) } diff --git a/db/model.go b/db/model.go index 6f35cd0..4b814f9 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 d78acc7..a960277 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") } |