summaryrefslogtreecommitdiff
path: root/app/controllers/sparkles/controller.go
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers/sparkles/controller.go')
-rw-r--r--app/controllers/sparkles/controller.go18
1 files changed, 16 insertions, 2 deletions
diff --git a/app/controllers/sparkles/controller.go b/app/controllers/sparkles/controller.go
index e0da8c4..35f2076 100644
--- a/app/controllers/sparkles/controller.go
+++ b/app/controllers/sparkles/controller.go
@@ -22,14 +22,28 @@ func New(db domain.Repository[*domain.Sparkle]) *Controller {
}
func (c *Controller) MountTo(mux *http.ServeMux) {
- requireUser := middleware.RequireUser(http.StatusFound, "/")
+ requireUser := middleware.RequireUser()
mux.HandleFunc("GET /sparkles", c.Index)
+ mux.Handle("GET /sparkles/new", requireUser(http.HandlerFunc(c.NewForm)))
mux.Handle("POST /sparkles", requireUser(http.HandlerFunc(c.Create)))
}
func (c *Controller) Index(w http.ResponseWriter, r *http.Request) {
- serde.ToHTTP(w, r, c.db.All())
+ if err := serde.ToHTTP(w, r, c.db.All()); err != nil {
+ log.WithFields(r.Context(), log.Fields{"error": err})
+ w.WriteHeader(http.StatusInternalServerError)
+ }
+}
+
+func (c *Controller) NewForm(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusOK)
+ w.Header().Add("Content-Type", "text/html")
+
+ dto := &NewSparkleDTO{CurrentUser: cfg.CurrentUser.From(r.Context())}
+ if err := views.Render(w, "sparkles/new", dto); err != nil {
+ log.WithFields(r.Context(), log.Fields{"error": err})
+ }
}
func (c *Controller) Create(w http.ResponseWriter, r *http.Request) {