diff options
Diffstat (limited to 'pkg/database')
-rw-r--r-- | pkg/database/repository/auth.go | 10 | ||||
-rw-r--r-- | pkg/database/repository/user.go | 35 | ||||
-rw-r--r-- | pkg/database/sql/user.go | 23 |
3 files changed, 56 insertions, 12 deletions
diff --git a/pkg/database/repository/auth.go b/pkg/database/repository/auth.go new file mode 100644 index 0000000..b319495 --- /dev/null +++ b/pkg/database/repository/auth.go @@ -0,0 +1,10 @@ +package repository + +import "context" + +type ( + AuthRepository interface { + GetIDByUsername(ctx context.Context, username string) (uint, error) + GetPassword(ctx context.Context, id uint) ([]byte, error) + } +) diff --git a/pkg/database/repository/user.go b/pkg/database/repository/user.go new file mode 100644 index 0000000..f8bd719 --- /dev/null +++ b/pkg/database/repository/user.go @@ -0,0 +1,35 @@ +package repository + +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 []byte + IsAdmin bool + Path string + } + + UserRepository interface { + Get(ctx context.Context, id uint) (*User, error) + List(ctx context.Context) ([]*User, error) + Create(ctx context.Context, createUser *CreateUser) (uint, error) + Update(ctx context.Context, id uint, updateUser *UpdateUser) error + Any(ctx context.Context) (bool, error) + } +) diff --git a/pkg/database/sql/user.go b/pkg/database/sql/user.go index a0884f4..479a9c5 100644 --- a/pkg/database/sql/user.go +++ b/pkg/database/sql/user.go @@ -6,8 +6,7 @@ import ( "golang.org/x/crypto/bcrypt" "gorm.io/gorm" - "git.sr.ht/~gabrielgio/img/pkg/components/auth" - "git.sr.ht/~gabrielgio/img/pkg/components/user" + "git.sr.ht/~gabrielgio/img/pkg/database/repository" ) type ( @@ -27,8 +26,8 @@ type ( } ) -var _ auth.Repository = &UserRepository{} -var _ user.Repository = &UserRepository{} +var _ repository.UserRepository = &UserRepository{} +var _ repository.AuthRepository = &UserRepository{} func NewUserRepository(db *gorm.DB) *UserRepository { return &UserRepository{ @@ -36,8 +35,8 @@ func NewUserRepository(db *gorm.DB) *UserRepository { } } -func (self *User) ToModel() *user.User { - return &user.User{ +func (self *User) ToModel() *repository.User { + return &repository.User{ ID: self.Model.ID, Name: self.Name, Username: self.Username, @@ -46,7 +45,7 @@ func (self *User) ToModel() *user.User { } } -func (self Users) ToModel() (users []*user.User) { +func (self Users) ToModel() (users []*repository.User) { for _, user := range self { users = append(users, user.ToModel()) } @@ -75,7 +74,7 @@ func (self *UserRepository) EnsureAdmin(ctx context.Context) { } } -func (self *UserRepository) List(ctx context.Context) ([]*user.User, error) { +func (self *UserRepository) List(ctx context.Context) ([]*repository.User, error) { users := Users{} result := self.db. WithContext(ctx). @@ -88,8 +87,8 @@ func (self *UserRepository) List(ctx context.Context) ([]*user.User, error) { return users.ToModel(), nil } -func (self *UserRepository) Get(ctx context.Context, id uint) (*user.User, error) { - var user = &user.User{ID: id} +func (self *UserRepository) Get(ctx context.Context, id uint) (*repository.User, error) { + var user = &repository.User{ID: id} result := self.db. WithContext(ctx). First(user) @@ -137,7 +136,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 *repository.CreateUser) (uint, error) { user := &User{ Username: createUser.Username, Name: createUser.Name, @@ -154,7 +153,7 @@ func (self *UserRepository) Create(ctx context.Context, createUser *user.CreateU return user.Model.ID, nil } -func (self *UserRepository) Update(ctx context.Context, id uint, update *user.UpdateUser) error { +func (self *UserRepository) Update(ctx context.Context, id uint, update *repository.UpdateUser) error { user := &User{ Model: gorm.Model{ ID: id, |