lens @ 5bb3d3ecabfc658035e69d5d5286cabaf44e3f73

 1package view
 2
 3import (
 4	"github.com/valyala/fasthttp"
 5
 6	"git.sr.ht/~gabrielgio/img"
 7	"git.sr.ht/~gabrielgio/img/pkg/components/settings"
 8	"git.sr.ht/~gabrielgio/img/pkg/ext"
 9)
10
11type SettingsView struct {
12	// there is not need to create a controller for this
13	repository settings.Repository
14}
15
16func NewSettingsView(respository settings.Repository) *SettingsView {
17	return &SettingsView{
18		repository: respository,
19	}
20}
21
22func (self *SettingsView) Index(ctx *fasthttp.RequestCtx) error {
23	s, err := self.repository.Load(ctx)
24	if err != nil {
25		return err
26	}
27	return img.Render(ctx, "settings.html", &img.HTMLView[*settings.Settings]{
28		Title: "Settings",
29		Data:  s,
30	})
31}
32
33func (self *SettingsView) Save(ctx *fasthttp.RequestCtx) error {
34	var (
35		showMode  = string(ctx.FormValue("showMode")) == "on"
36		showOwner = string(ctx.FormValue("showOwner")) == "on"
37	)
38
39	err := self.repository.Save(ctx, &settings.Settings{
40		ShowMode:  showMode,
41		ShowOwner: showOwner,
42	})
43	if err != nil {
44		return err
45	}
46
47	return self.Index(ctx)
48}
49
50func (self *SettingsView) SetMyselfIn(r *ext.Router) {
51	r.GET("/settings/", self.Index)
52	r.POST("/settings/", self.Save)
53}