diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-02-26 19:54:48 +0100 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-18 16:30:36 +0200 |
commit | c8e1328164e9ffbd681c3c0e449f1e6b9856b896 (patch) | |
tree | faee639a4c55c5dc3bfc59a5400026822c40221d /pkg/database/sql/user.go | |
download | lens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.tar.gz lens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.tar.bz2 lens-c8e1328164e9ffbd681c3c0e449f1e6b9856b896.zip |
feat: Inicial commit
It contains rough template for the server and runners.
It contains rough template for the server and runners.
Diffstat (limited to 'pkg/database/sql/user.go')
-rw-r--r-- | pkg/database/sql/user.go | 182 |
1 files changed, 182 insertions, 0 deletions
diff --git a/pkg/database/sql/user.go b/pkg/database/sql/user.go new file mode 100644 index 0000000..d449b05 --- /dev/null +++ b/pkg/database/sql/user.go @@ -0,0 +1,182 @@ +package sql + +import ( + "context" + + "golang.org/x/crypto/bcrypt" + "gorm.io/gorm" + + "git.sr.ht/~gabrielgio/img/pkg/components/auth" + user "git.sr.ht/~gabrielgio/img/pkg/components/auth" +) + +type ( + User struct { + gorm.Model + Username string + Name string + Password string + } + + Users []*User + + UserRepository struct { + db *gorm.DB + } +) + +var _ auth.Repository = &UserRepository{} + +func NewUserRepository(db *gorm.DB) *UserRepository { + return &UserRepository{ + db: db, + } +} + +func (self *User) ToModel() *user.User { + return &user.User{ + ID: self.Model.ID, + Name: self.Name, + Username: self.Username, + } +} + +func (self Users) ToModel() (users []*user.User) { + for _, user := range self { + users = append(users, user.ToModel()) + } + return +} + +// Testing function, will remove later +// TODO: remove later +func (self *UserRepository) EnsureAdmin(ctx context.Context) { + var exists bool + self.db. + WithContext(ctx). + Model(&User{}). + Select("count(*) > 0"). + Where("username = ?", "admin"). + Find(&exists) + + if !exists { + hash, _ := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.MinCost) + self.db.Save(&User{ + Username: "admin", + Password: string(hash), + }) + } +} + +func (self *UserRepository) List(ctx context.Context) ([]*user.User, error) { + users := Users{} + result := self.db. + WithContext(ctx). + Find(&users) + + if result.Error != nil { + return nil, result.Error + } + + return users.ToModel(), nil +} + +func (self *UserRepository) Get(ctx context.Context, id uint) (*user.User, error) { + var user = &user.User{ID: id} + result := self.db. + WithContext(ctx). + First(user) + + if result.Error != nil { + return nil, result.Error + } + + return user, nil +} + +func (self *UserRepository) GetIDByUsername(ctx context.Context, username string) (uint, error) { + userID := struct { + ID uint + }{} + + result := self.db. + WithContext(ctx). + Model(&User{}). + Where("username = ?", username). + First(&userID) + + if result.Error != nil { + return 0, result.Error + } + + return userID.ID, nil +} + +func (self *UserRepository) GetPassword(ctx context.Context, id uint) ([]byte, error) { + userPassword := struct { + Password []byte + }{} + + result := self.db. + WithContext(ctx). + Model(&User{}). + Where("id = ?", id). + First(&userPassword) + + if result.Error != nil { + return nil, result.Error + } + + return userPassword.Password, nil +} + +func (self *UserRepository) Create(ctx context.Context, createUser *user.CreateUser) (uint, error) { + user := &User{ + Username: createUser.Username, + Name: createUser.Name, + Password: string(createUser.Password), + } + + result := self.db. + WithContext(ctx). + Create(user) + if result.Error != nil { + return 0, result.Error + } + + return user.Model.ID, nil +} + +func (self *UserRepository) Update(ctx context.Context, id uint, update *user.UpdateUser) error { + user := &User{ + Model: gorm.Model{ + ID: id, + }, + Username: update.Username, + Name: update.Name, + } + + result := self.db. + WithContext(ctx). + Save(user) + if result.Error != nil { + return result.Error + } + + return nil +} + +func (self *UserRepository) Delete(ctx context.Context, id uint) error { + userID := struct { + ID uint + }{ + ID: id, + } + result := self.db. + WithContext(ctx). + Delete(userID) + if result.Error != nil { + return result.Error + } + return nil +} |