summaryrefslogtreecommitdiff
path: root/app/controllers/sparkles/controller.go
blob: 061b5c6824e2fddf51e07622f39ddb5d1770a4ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package sparkles

import (
	"net/http"

	"github.com/xlgmokha/x/pkg/serde"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/db"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/domain"
)

type Controller struct {
	db db.Repository
}

func New(db db.Repository) *Controller {
	return &Controller{db: db}
}

func (c *Controller) MountTo(mux *http.ServeMux) {
	mux.HandleFunc("GET /sparkles", c.Index)
	mux.HandleFunc("POST /sparkles", c.Create)
}

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)
}