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/database/sql/user.go | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'pkg/database/sql/user.go') diff --git a/pkg/database/sql/user.go b/pkg/database/sql/user.go index 15dbe72..6b1cf0f 100644 --- a/pkg/database/sql/user.go +++ b/pkg/database/sql/user.go @@ -27,6 +27,7 @@ type ( ) var _ repository.UserRepository = &UserRepository{} + var _ repository.AuthRepository = &UserRepository{} func NewUserRepository(db *gorm.DB) *UserRepository { @@ -141,6 +142,7 @@ func (self *UserRepository) Create(ctx context.Context, createUser *repository.C Username: createUser.Username, Name: createUser.Name, Path: createUser.Path, + IsAdmin: createUser.IsAdmin, Password: string(createUser.Password), } @@ -161,11 +163,14 @@ func (self *UserRepository) Update(ctx context.Context, id uint, update *reposit }, Username: update.Username, Name: update.Name, + IsAdmin: update.IsAdmin, + Path: update.Path, } result := self.db. WithContext(ctx). - Save(user) + Omit("password"). + Updates(user) if result.Error != nil { return result.Error } @@ -174,14 +179,15 @@ func (self *UserRepository) Update(ctx context.Context, id uint, update *reposit } func (self *UserRepository) Delete(ctx context.Context, id uint) error { - userID := struct { - ID uint - }{ - ID: id, + user := &User{ + Model: gorm.Model{ + ID: id, + }, } + result := self.db. WithContext(ctx). - Delete(userID) + Delete(user) if result.Error != nil { return result.Error } @@ -219,3 +225,13 @@ func (u *UserRepository) GetPathFromUserID(ctx context.Context, id uint) (string return userPath, nil } + +func (u *UserRepository) UpdatePassword(ctx context.Context, id uint, password []byte) error { + result := u.db. + WithContext(ctx). + Model(&User{}). + Where("id = ?", id). + Update("password", password) + + return result.Error +} -- cgit v1.2.3