diff options
| author | mo khan <mo@mokhan.ca> | 2025-04-25 21:25:40 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2025-04-28 09:07:31 -0600 |
| commit | 9b01d1616e130a589151bf1273e41181ecc727f4 (patch) | |
| tree | 639ec3b3c3857042a551c8e88b09413f590ebcec /app/controllers/sparkles | |
| parent | 13ab8de7d09b5d4b10132828277d17ba0543b901 (diff) | |
feat: use htmx to render partials
Diffstat (limited to 'app/controllers/sparkles')
| -rw-r--r-- | app/controllers/sparkles/controller.go | 18 | ||||
| -rw-r--r-- | app/controllers/sparkles/dto.go | 7 |
2 files changed, 23 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) { diff --git a/app/controllers/sparkles/dto.go b/app/controllers/sparkles/dto.go new file mode 100644 index 0000000..5e53dab --- /dev/null +++ b/app/controllers/sparkles/dto.go @@ -0,0 +1,7 @@ +package sparkles + +import "gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/domain" + +type NewSparkleDTO struct { + CurrentUser *domain.User +} |
