lens @ 398ae343e6a33fb81f82380b17081b8f9bae328a

 1package view
 2
 3import (
 4	"github.com/valyala/fasthttp"
 5
 6	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
 7	"git.sr.ht/~gabrielgio/img/pkg/ext"
 8	"git.sr.ht/~gabrielgio/img/templates"
 9)
10
11type (
12	SettingsView struct {
13		// there is not need to create a controller for this
14		settingsRepository repository.SettingsRepository
15		userRepository     repository.UserRepository
16	}
17)
18
19func NewSettingsView(
20	settingsRespository repository.SettingsRepository,
21	userRepository repository.UserRepository,
22) *SettingsView {
23	return &SettingsView{
24		settingsRepository: settingsRespository,
25		userRepository:     userRepository,
26	}
27}
28
29func (self *SettingsView) Index(ctx *fasthttp.RequestCtx) error {
30	s, err := self.settingsRepository.Load(ctx)
31	if err != nil {
32		return err
33	}
34
35	users, err := self.userRepository.List(ctx)
36	if err != nil {
37		return err
38	}
39
40	templates.WritePageTemplate(ctx, &templates.SettingsPage{
41		Settings: s,
42		Users:    users,
43	})
44
45	return nil
46}
47
48func (self *SettingsView) Save(ctx *fasthttp.RequestCtx) error {
49	var (
50		showMode  = string(ctx.FormValue("showMode")) == "on"
51		showOwner = string(ctx.FormValue("showOwner")) == "on"
52	)
53
54	err := self.settingsRepository.Save(ctx, &repository.Settings{
55		ShowMode:  showMode,
56		ShowOwner: showOwner,
57	})
58	if err != nil {
59		return err
60	}
61
62	return self.Index(ctx)
63}
64
65func (self *SettingsView) SetMyselfIn(r *ext.Router) {
66	r.GET("/settings/", self.Index)
67	r.POST("/settings/", self.Save)
68}