From ff15873b766f5be308c9967859bfeb7da7cfa21a Mon Sep 17 00:00:00 2001 From: mo khan Date: Fri, 11 Apr 2025 17:52:29 -0600 Subject: refactor: split routes to separate controller files --- app/controllers/health/controller.go | 14 ++++++++++++++ app/controllers/sparkles/controller.go | 27 +++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 app/controllers/health/controller.go create mode 100644 app/controllers/sparkles/controller.go (limited to 'app/controllers') diff --git a/app/controllers/health/controller.go b/app/controllers/health/controller.go new file mode 100644 index 0000000..e241885 --- /dev/null +++ b/app/controllers/health/controller.go @@ -0,0 +1,14 @@ +package health + +import "net/http" + +type Controller struct { +} + +func New() *Controller { + return &Controller{} +} + +func (c *Controller) Index(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) +} diff --git a/app/controllers/sparkles/controller.go b/app/controllers/sparkles/controller.go new file mode 100644 index 0000000..365184f --- /dev/null +++ b/app/controllers/sparkles/controller.go @@ -0,0 +1,27 @@ +package sparkles + +import ( + "net/http" + + "github.com/xlgmokha/x/pkg/serde" + "gitlab.com/mokhax/sparkled/pkg/db" + "gitlab.com/mokhax/sparkled/pkg/domain" +) + +type Controller struct { + db db.Repository +} + +func New(db db.Repository) *Controller { + return &Controller{db: db} +} + +func (c *Controller) Index(w http.ResponseWriter, r *http.Request) { + serde.ToHTTP(w, r, c.db.All()) +} + +func (c *Controller) Create(w http.ResponseWriter, r *http.Request) { + sparkle, _ := serde.FromHTTP[*domain.Sparkle](r) + c.db.Save(sparkle) + w.WriteHeader(http.StatusCreated) +} -- cgit v1.2.3