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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
package view
import (
"net/http"
"strconv"
"git.sr.ht/~gabrielgio/img/pkg/database/repository"
"git.sr.ht/~gabrielgio/img/pkg/ext"
"git.sr.ht/~gabrielgio/img/pkg/service"
"git.sr.ht/~gabrielgio/img/templates"
)
type (
SettingsView struct {
// there is not need to create a controller for this
settingsRepository repository.SettingsRepository
userController *service.AuthController
}
)
func NewSettingsView(
settingsRespository repository.SettingsRepository,
userController *service.AuthController,
) *SettingsView {
return &SettingsView{
settingsRepository: settingsRespository,
userController: userController,
}
}
func (self *SettingsView) Index(w http.ResponseWriter, r *http.Request) error {
s, err := self.settingsRepository.Load(r.Context())
if err != nil {
return err
}
users, err := self.userController.List(r.Context())
if err != nil {
return err
}
templates.WritePageTemplate(w, &templates.SettingsPage{
Settings: s,
Users: users,
})
return nil
}
func (self *SettingsView) User(w http.ResponseWriter, r *http.Request) error {
id := r.FormValue("userId")
idValue, err := ParseUint(id)
if err != nil {
return err
}
if idValue == nil {
templates.WritePageTemplate(w, &templates.UserPage{})
} else {
user, err := self.userController.Get(r.Context(), *idValue)
if err != nil {
return err
}
templates.WritePageTemplate(w, &templates.UserPage{
ID: idValue,
Username: user.Username,
Path: user.Path,
IsAdmin: user.IsAdmin,
})
}
return nil
}
func (self *SettingsView) UpsertUser(w http.ResponseWriter, r *http.Request) error {
var (
username = r.FormValue("username")
password = []byte(r.FormValue("password"))
path = r.FormValue("path")
isAdmin = r.FormValue("isAdmin") == "on"
id = r.FormValue("userId")
)
idValue, err := ParseUint(id)
if err != nil {
return err
}
err = self.userController.Upsert(r.Context(), idValue, username, "", password, isAdmin, path)
if err != nil {
return err
}
http.Redirect(w, r, "/settings", http.StatusTemporaryRedirect)
return nil
}
func (self *SettingsView) Delete(w http.ResponseWriter, r *http.Request) error {
var (
id = r.FormValue("userId")
)
idValue, err := ParseUint(id)
if err != nil {
return err
}
if idValue != nil {
err = self.userController.Delete(r.Context(), *idValue)
if err != nil {
return err
}
}
http.Redirect(w, r, "/settings", http.StatusTemporaryRedirect)
return nil
}
func (self *SettingsView) Save(w http.ResponseWriter, r *http.Request) error {
var (
showMode = r.FormValue("showMode") == "on"
showOwner = r.FormValue("showOwner") == "on"
preloadVideoMetadata = r.FormValue("preloadVideoMetadata") == "on"
)
err := self.settingsRepository.Save(r.Context(), &repository.Settings{
ShowMode: showMode,
ShowOwner: showOwner,
PreloadVideoMetadata: preloadVideoMetadata,
})
if err != nil {
return err
}
return self.Index(w, r)
}
func (self *SettingsView) SetMyselfIn(r *ext.Router) {
r.GET("/settings/", self.Index)
r.POST("/settings/", self.Save)
r.GET("/users/", self.User)
r.GET("/users/delete", self.Delete)
r.POST("/users/", self.UpsertUser)
}
func ParseUint(id string) (*uint, error) {
var idValue *uint
if id != "" {
v, err := strconv.Atoi(id)
if err != nil {
return nil, err
}
u := uint(v)
idValue = &u
}
return idValue, nil
}
|