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 }