From 64496464b3812839c1e4b440bdf69cc84f39c491 Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Wed, 15 Jun 2022 12:40:12 +0200 Subject: ref: Move to a MVC like approach Before everything was dumped into the controller file, now it is spread out a bit. It is still far from good, like the controller is not really a controller... baby steps I guess The refactored was based on this post: https://www.alexedwards.net/blog/organising-database-access --- routes/routes.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'routes') 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") } -- cgit v1.2.3