From 249ee195ce52ee4a4defeb67a33ef353919d3a11 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Sun, 25 Jun 2023 19:09:54 +0200 Subject: feat: Add user list UI Fill user settings UI with actual data. --- pkg/components/auth/controller.go | 14 ------------- pkg/components/auth/model.go | 22 -------------------- pkg/components/user/controller.go | 1 + pkg/components/user/model.go | 33 ++++++++++++++++++++++++++++++ pkg/database/sql/user.go | 15 ++++++++++---- pkg/view/auth.go | 20 ------------------- pkg/view/settings.go | 42 +++++++++++++++++++++++++++++---------- 7 files changed, 77 insertions(+), 70 deletions(-) create mode 100644 pkg/components/user/controller.go create mode 100644 pkg/components/user/model.go (limited to 'pkg') diff --git a/pkg/components/auth/controller.go b/pkg/components/auth/controller.go index 4da6071..a81a1c0 100644 --- a/pkg/components/auth/controller.go +++ b/pkg/components/auth/controller.go @@ -41,17 +41,3 @@ func (c *Controller) Login(ctx context.Context, username, password []byte) ([]by } return ext.WriteToken(token, c.key) } - -func (c *Controller) Register(ctx context.Context, username, password []byte) error { - hash, err := bcrypt.GenerateFromPassword(password, bcrypt.MinCost) - if err != nil { - return err - } - - _, err = c.repository.Create(ctx, &CreateUser{ - Username: string(username), - Password: hash, - }) - - return err -} diff --git a/pkg/components/auth/model.go b/pkg/components/auth/model.go index e46ef49..dd6ce50 100644 --- a/pkg/components/auth/model.go +++ b/pkg/components/auth/model.go @@ -3,30 +3,8 @@ package auth import "context" type ( - // TODO: move to user later - User struct { - ID uint - Username string - Name string - } - - // TODO: move to user later - UpdateUser struct { - Username string - Name string - } - - // TODO: move to user later - CreateUser struct { - Username string - Name string - Password []byte - } - Repository interface { GetIDByUsername(ctx context.Context, username string) (uint, error) GetPassword(ctx context.Context, id uint) ([]byte, error) - // TODO: move to user later - Create(ctx context.Context, createUser *CreateUser) (uint, error) } ) diff --git a/pkg/components/user/controller.go b/pkg/components/user/controller.go new file mode 100644 index 0000000..a00006b --- /dev/null +++ b/pkg/components/user/controller.go @@ -0,0 +1 @@ +package user diff --git a/pkg/components/user/model.go b/pkg/components/user/model.go new file mode 100644 index 0000000..f957c39 --- /dev/null +++ b/pkg/components/user/model.go @@ -0,0 +1,33 @@ +package user + +import "context" + +type ( + User struct { + ID uint + Username string + Name string + IsAdmin bool + Path string + } + + UpdateUser struct { + Username string + Name string + Password *string + } + + CreateUser struct { + Username string + Name string + Password string + IsAdmin bool + Path string + } + + Repository interface { + List(ctx context.Context) ([]*User, error) + Create(ctx context.Context, createUser *CreateUser) error + Update(ctx context.Context, id uint, updateUser *UpdateUser) error + } +) diff --git a/pkg/database/sql/user.go b/pkg/database/sql/user.go index d449b05..2d74162 100644 --- a/pkg/database/sql/user.go +++ b/pkg/database/sql/user.go @@ -7,7 +7,7 @@ import ( "gorm.io/gorm" "git.sr.ht/~gabrielgio/img/pkg/components/auth" - user "git.sr.ht/~gabrielgio/img/pkg/components/auth" + "git.sr.ht/~gabrielgio/img/pkg/components/user" ) type ( @@ -16,6 +16,8 @@ type ( Username string Name string Password string + IsAdmin bool + Path string } Users []*User @@ -26,6 +28,7 @@ type ( ) var _ auth.Repository = &UserRepository{} +var _ user.Repository = &UserRepository{} func NewUserRepository(db *gorm.DB) *UserRepository { return &UserRepository{ @@ -38,6 +41,8 @@ func (self *User) ToModel() *user.User { ID: self.Model.ID, Name: self.Name, Username: self.Username, + Path: self.Path, + IsAdmin: self.IsAdmin, } } @@ -63,6 +68,8 @@ func (self *UserRepository) EnsureAdmin(ctx context.Context) { hash, _ := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.MinCost) self.db.Save(&User{ Username: "admin", + Path: "/", + IsAdmin: true, Password: string(hash), }) } @@ -130,7 +137,7 @@ func (self *UserRepository) GetPassword(ctx context.Context, id uint) ([]byte, e return userPassword.Password, nil } -func (self *UserRepository) Create(ctx context.Context, createUser *user.CreateUser) (uint, error) { +func (self *UserRepository) Create(ctx context.Context, createUser *user.CreateUser) error { user := &User{ Username: createUser.Username, Name: createUser.Name, @@ -141,10 +148,10 @@ func (self *UserRepository) Create(ctx context.Context, createUser *user.CreateU WithContext(ctx). Create(user) if result.Error != nil { - return 0, result.Error + return result.Error } - return user.Model.ID, nil + return nil } func (self *UserRepository) Update(ctx context.Context, id uint, update *user.UpdateUser) error { diff --git a/pkg/view/auth.go b/pkg/view/auth.go index 5c83eba..d44424d 100644 --- a/pkg/view/auth.go +++ b/pkg/view/auth.go @@ -64,23 +64,6 @@ func (v *AuthView) Login(ctx *fasthttp.RequestCtx) error { return nil } -func (v *AuthView) RegisterView(ctx *fasthttp.RequestCtx) error { - return img.Render[interface{}](ctx, "register.html", nil) -} - -func (v *AuthView) Register(ctx *fasthttp.RequestCtx) error { - username := ctx.FormValue("username") - password := ctx.FormValue("password") - - err := v.userController.Register(ctx, username, password) - if err != nil { - return err - } - - ctx.Redirect("/login", 307) - return nil -} - func Index(ctx *fasthttp.RequestCtx) { ctx.Redirect("/login", 307) } @@ -89,9 +72,6 @@ func (v *AuthView) SetMyselfIn(r *ext.Router) { r.GET("/login", v.LoginView) r.POST("/login", v.Login) - r.GET("/register", v.RegisterView) - r.POST("/register", v.Register) - r.GET("/logout", v.Logout) r.POST("/logout", v.Logout) } diff --git a/pkg/view/settings.go b/pkg/view/settings.go index 746dee4..e5acb1b 100644 --- a/pkg/view/settings.go +++ b/pkg/view/settings.go @@ -5,28 +5,50 @@ import ( "git.sr.ht/~gabrielgio/img" "git.sr.ht/~gabrielgio/img/pkg/components/settings" + "git.sr.ht/~gabrielgio/img/pkg/components/user" "git.sr.ht/~gabrielgio/img/pkg/ext" ) -type SettingsView struct { - // there is not need to create a controller for this - repository settings.Repository -} +type ( + SettingsView struct { + // there is not need to create a controller for this + settingsRepository settings.Repository + userRepository user.Repository + } + + SettingsPage struct { + Settings *settings.Settings + Users []*user.User + } +) -func NewSettingsView(respository settings.Repository) *SettingsView { +func NewSettingsView( + settingsRespository settings.Repository, + userRepository user.Repository, +) *SettingsView { return &SettingsView{ - repository: respository, + settingsRepository: settingsRespository, + userRepository: userRepository, } } func (self *SettingsView) Index(ctx *fasthttp.RequestCtx) error { - s, err := self.repository.Load(ctx) + s, err := self.settingsRepository.Load(ctx) if err != nil { return err } - return img.Render(ctx, "settings.html", &img.HTMLView[*settings.Settings]{ + + users, err := self.userRepository.List(ctx) + if err != nil { + return err + } + + return img.Render(ctx, "settings.html", &img.HTMLView[*SettingsPage]{ Title: "Settings", - Data: s, + Data: &SettingsPage{ + Settings: s, + Users: users, + }, }) } @@ -36,7 +58,7 @@ func (self *SettingsView) Save(ctx *fasthttp.RequestCtx) error { showOwner = string(ctx.FormValue("showOwner")) == "on" ) - err := self.repository.Save(ctx, &settings.Settings{ + err := self.settingsRepository.Save(ctx, &settings.Settings{ ShowMode: showMode, ShowOwner: showOwner, }) -- cgit v1.2.3