From 024da3e546e98cbaeea5f7bc86af12b671996f41 Mon Sep 17 00:00:00 2001 From: Gabriel Arakaki Giovanini Date: Thu, 29 Jun 2023 23:05:23 +0200 Subject: ref: Refactor how repository is define To make things easier and reduce the number of package I'll move all repository to one folder, starting with auth and user repository. Also implements all testing on top of the repository interface with a im memory implementation. This will later make mescling unit and integration easier. --- pkg/database/sql/user.go | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'pkg/database/sql') 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, -- cgit v1.2.3