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 preloadVideoMetadata = string(ctx.FormValue("preloadVideoMetadata")) == "on"
53 )
54
55 err := self.settingsRepository.Save(ctx, &repository.Settings{
56 ShowMode: showMode,
57 ShowOwner: showOwner,
58 PreloadVideoMetadata: preloadVideoMetadata,
59 })
60 if err != nil {
61 return err
62 }
63
64 return self.Index(ctx)
65}
66
67func (self *SettingsView) SetMyselfIn(r *ext.Router) {
68 r.GET("/settings/", self.Index)
69 r.POST("/settings/", self.Save)
70}