1package view
2
3import (
4 "net/http"
5 "strconv"
6
7 "git.sr.ht/~gabrielgio/img/pkg/database/repository"
8 "git.sr.ht/~gabrielgio/img/pkg/ext"
9 "git.sr.ht/~gabrielgio/img/pkg/service"
10 "git.sr.ht/~gabrielgio/img/templates"
11)
12
13type (
14 SettingsView struct {
15 // there is not need to create a controller for this
16 settingsRepository repository.SettingsRepository
17 userController *service.AuthController
18 }
19)
20
21func NewSettingsView(
22 settingsRespository repository.SettingsRepository,
23 userController *service.AuthController,
24) *SettingsView {
25 return &SettingsView{
26 settingsRepository: settingsRespository,
27 userController: userController,
28 }
29}
30
31func (self *SettingsView) Index(w http.ResponseWriter, r *http.Request) error {
32 s, err := self.settingsRepository.Load(r.Context())
33 if err != nil {
34 return err
35 }
36
37 users, err := self.userController.List(r.Context())
38 if err != nil {
39 return err
40 }
41
42 user := ext.GetUserFromCtx(r)
43
44 templates.WritePageTemplate(w, &templates.SettingsPage{
45 Settings: s,
46 Users: users,
47 }, user.IsAdmin)
48
49 return nil
50}
51
52func (self *SettingsView) User(w http.ResponseWriter, r *http.Request) error {
53 var (
54 id = r.URL.Query().Get("userId")
55 user = ext.GetUserFromCtx(r)
56 )
57 idValue, err := ParseUint(id)
58 if err != nil {
59 return err
60 }
61
62 if idValue == nil {
63 templates.WritePageTemplate(w, &templates.UserPage{}, user.IsAdmin)
64 } else {
65 user, err := self.userController.Get(r.Context(), *idValue)
66 if err != nil {
67 return err
68 }
69
70 templates.WritePageTemplate(w, &templates.UserPage{
71 ID: idValue,
72 Username: user.Username,
73 Path: user.Path,
74 IsAdmin: user.IsAdmin,
75 }, user.IsAdmin)
76 }
77
78 return nil
79}
80
81func (self *SettingsView) UpsertUser(w http.ResponseWriter, r *http.Request) error {
82 var (
83 username = r.FormValue("username")
84 password = []byte(r.FormValue("password"))
85 path = r.FormValue("path")
86 isAdmin = r.FormValue("isAdmin") == "on"
87 id = r.FormValue("userId")
88 )
89
90 idValue, err := ParseUint(id)
91 if err != nil {
92 return err
93 }
94
95 err = self.userController.Upsert(
96 r.Context(),
97 idValue,
98 username,
99 "",
100 password,
101 isAdmin,
102 path,
103 )
104 if err != nil {
105 return err
106 }
107
108 http.Redirect(w, r, "/settings", http.StatusTemporaryRedirect)
109 return nil
110}
111
112func (self *SettingsView) Delete(w http.ResponseWriter, r *http.Request) error {
113 var (
114 id = r.FormValue("userId")
115 )
116
117 idValue, err := ParseUint(id)
118 if err != nil {
119 return err
120 }
121
122 if idValue != nil {
123 err = self.userController.Delete(r.Context(), *idValue)
124 if err != nil {
125 return err
126 }
127 }
128
129 http.Redirect(w, r, "/settings", http.StatusTemporaryRedirect)
130 return nil
131}
132
133func (self *SettingsView) Save(w http.ResponseWriter, r *http.Request) error {
134 var (
135 showMode = r.FormValue("showMode") == "on"
136 showOwner = r.FormValue("showOwner") == "on"
137 preloadVideoMetadata = r.FormValue("preloadVideoMetadata") == "on"
138 )
139
140 err := self.settingsRepository.Save(r.Context(), &repository.Settings{
141 ShowMode: showMode,
142 ShowOwner: showOwner,
143 PreloadVideoMetadata: preloadVideoMetadata,
144 })
145 if err != nil {
146 return err
147 }
148
149 return self.Index(w, r)
150}
151
152func (self *SettingsView) SetMyselfIn(r *ext.Router) {
153 r.GET("/settings", Protect(self.Index))
154 r.POST("/settings", Protect(self.Save))
155
156 r.GET("/users", Protect(self.User))
157 r.GET("/users/delete", Protect(self.Delete))
158 r.POST("/users", Protect(self.UpsertUser))
159}
160
161func ParseUint(id string) (*uint, error) {
162 var idValue *uint
163 if id != "" {
164 v, err := strconv.Atoi(id)
165 if err != nil {
166 return nil, err
167 }
168
169 u := uint(v)
170 idValue = &u
171 }
172 return idValue, nil
173}