summaryrefslogtreecommitdiff
path: root/app/controllers/sparkles/controller.go
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2025-04-22 11:34:14 -0600
committermo khan <mo@mokhan.ca>2025-04-22 11:34:14 -0600
commitc11693668f7e230a21f806664b411a598bee9b10 (patch)
tree0fecffe3b5b7a98b2134cc86402370a777fdcd4c /app/controllers/sparkles/controller.go
parent699ef9cc2a910774d1f210ef0562496d08f04e03 (diff)
feat: add tiny vue.js app to list and add new sparkles
Diffstat (limited to 'app/controllers/sparkles/controller.go')
-rw-r--r--app/controllers/sparkles/controller.go23
1 files changed, 21 insertions, 2 deletions
diff --git a/app/controllers/sparkles/controller.go b/app/controllers/sparkles/controller.go
index bc388db..07a21ba 100644
--- a/app/controllers/sparkles/controller.go
+++ b/app/controllers/sparkles/controller.go
@@ -3,7 +3,10 @@ package sparkles
import (
"net/http"
+ "github.com/xlgmokha/x/pkg/log"
+ "github.com/xlgmokha/x/pkg/mapper"
"github.com/xlgmokha/x/pkg/serde"
+ "github.com/xlgmokha/x/pkg/x"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/db"
"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain"
)
@@ -26,7 +29,23 @@ func (c *Controller) Index(w http.ResponseWriter, r *http.Request) {
}
func (c *Controller) Create(w http.ResponseWriter, r *http.Request) {
- sparkle, _ := serde.FromHTTP[*domain.Sparkle](r)
- c.db.Save(sparkle)
+ sparkle := mapper.MapFrom[*http.Request, *domain.Sparkle](r)
+
+ if x.IsZero(sparkle) {
+ w.WriteHeader(http.StatusBadRequest)
+ return
+ }
+
+ if err := c.db.Save(sparkle); err != nil {
+ log.WithFields(r.Context(), log.Fields{"error": err})
+ w.WriteHeader(http.StatusBadRequest)
+ return
+ }
+
w.WriteHeader(http.StatusCreated)
+ if err := serde.ToHTTP(w, r, sparkle); err != nil {
+ log.WithFields(r.Context(), log.Fields{"error": err})
+ w.WriteHeader(http.StatusInternalServerError)
+ return
+ }
}