From 3b9d27649a31e5af3fb137ff5b3378e7b8f7b9ae Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sat, 22 Jul 2023 18:45:59 +0200 Subject: feat: Add user management As many things it is on crude state. The settings.go has become a big mess, but I have achieve MVP, so from now one things shall improve as I'll spent more time on refactoring. --- pkg/view/settings.go | 99 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 95 insertions(+), 4 deletions(-) (limited to 'pkg/view') diff --git a/pkg/view/settings.go b/pkg/view/settings.go index ffce86b..5131362 100644 --- a/pkg/view/settings.go +++ b/pkg/view/settings.go @@ -1,10 +1,13 @@ package view import ( + "strconv" + "github.com/valyala/fasthttp" "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" ) @@ -12,17 +15,17 @@ type ( SettingsView struct { // there is not need to create a controller for this settingsRepository repository.SettingsRepository - userRepository repository.UserRepository + userController *service.AuthController } ) func NewSettingsView( settingsRespository repository.SettingsRepository, - userRepository repository.UserRepository, + userController *service.AuthController, ) *SettingsView { return &SettingsView{ settingsRepository: settingsRespository, - userRepository: userRepository, + userController: userController, } } @@ -32,7 +35,7 @@ func (self *SettingsView) Index(ctx *fasthttp.RequestCtx) error { return err } - users, err := self.userRepository.List(ctx) + users, err := self.userController.List(ctx) if err != nil { return err } @@ -45,6 +48,76 @@ func (self *SettingsView) Index(ctx *fasthttp.RequestCtx) error { return nil } +func (self *SettingsView) User(ctx *fasthttp.RequestCtx) error { + id := string(ctx.FormValue("userId")) + idValue, err := ParseUint(id) + if err != nil { + return err + } + + if idValue == nil { + templates.WritePageTemplate(ctx, &templates.UserPage{}) + } else { + user, err := self.userController.Get(ctx, *idValue) + if err != nil { + return err + } + + templates.WritePageTemplate(ctx, &templates.UserPage{ + ID: idValue, + Username: user.Username, + Path: user.Path, + IsAdmin: user.IsAdmin, + }) + } + + return nil +} + +func (self *SettingsView) UpsertUser(ctx *fasthttp.RequestCtx) error { + var ( + username = string(ctx.FormValue("username")) + password = ctx.FormValue("password") + path = string(ctx.FormValue("path")) + isAdmin = string(ctx.FormValue("isAdmin")) == "on" + id = string(ctx.FormValue("userId")) + ) + + idValue, err := ParseUint(id) + if err != nil { + return err + } + + err = self.userController.Upsert(ctx, idValue, username, "", password, isAdmin, path) + if err != nil { + return err + } + + ctx.Redirect("/settings", 307) + return nil +} + +func (self *SettingsView) Delete(ctx *fasthttp.RequestCtx) error { + var ( + id = string(ctx.FormValue("userId")) + ) + + idValue, err := ParseUint(id) + if err != nil { + return err + } + + if idValue != nil { + err = self.userController.Delete(ctx, *idValue) + if err != nil { + return err + } + } + + ctx.Redirect("/settings", 307) + return nil +} + func (self *SettingsView) Save(ctx *fasthttp.RequestCtx) error { var ( showMode = string(ctx.FormValue("showMode")) == "on" @@ -67,4 +140,22 @@ func (self *SettingsView) Save(ctx *fasthttp.RequestCtx) error { 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 } -- cgit v1.2.3