summaryrefslogtreecommitdiff
path: root/app/controllers/dashboard/controller.go
blob: d2799301076fedc5dce9887778799dbd4be9d9ea (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package dashboard

import (
	"net/http"

	"github.com/xlgmokha/x/pkg/x"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/cfg"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/middleware"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/app/views"
	"gitlab.com/gitlab-org/software-supply-chain-security/authorization/sparkled/pkg/pls"
)

type Controller struct {
}

func New() *Controller {
	return &Controller{}
}

func (c *Controller) MountTo(mux *http.ServeMux) {
	mux.Handle("GET /dashboard", x.Middleware[http.Handler](
		http.HandlerFunc(c.Show),
		middleware.RequireUser(),
	))

	mux.Handle("GET /dashboard/nav", http.HandlerFunc(c.Navigation))
}

func (c *Controller) Show(w http.ResponseWriter, r *http.Request) {
	currentUser := cfg.CurrentUser.From(r.Context())

	w.WriteHeader(http.StatusOK)
	w.Header().Add("Content-Type", "text/html")

	dto := &ViewDashboardDTO{CurrentUser: currentUser}
	if err := views.Render(w, "dashboard/show", dto); err != nil {
		pls.LogError(r.Context(), err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
}

func (c *Controller) Navigation(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Header().Add("Content-Type", "text/html")

	dto := &NavigationDTO{
		CurrentUser: cfg.CurrentUser.From(r.Context()),
		IsLoggedIn:  middleware.IsLoggedIn(r),
	}
	if err := views.Render(w, "dashboard/nav", dto); err != nil {
		pls.LogError(r.Context(), err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
}