diff options
author | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-29 23:33:02 +0200 |
---|---|---|
committer | Gabriel Arakaki Giovanini <mail@gabrielgio.me> | 2023-06-29 23:33:02 +0200 |
commit | 7a414da9a802d5eeee911b3536790a061e1d7503 (patch) | |
tree | e009cd94e72bf908701ca6067833ed14f6860b43 /pkg/service/main_test.go | |
parent | 1ae70dbd9124675d4a510954619b01edd5f1f6c3 (diff) | |
download | lens-7a414da9a802d5eeee911b3536790a061e1d7503.tar.gz lens-7a414da9a802d5eeee911b3536790a061e1d7503.tar.bz2 lens-7a414da9a802d5eeee911b3536790a061e1d7503.zip |
ref: Move all controller under the same folder
Move all controller to the same folder and rename them to service.
Moving them to the same folder allow an easier setup for testing.
Diffstat (limited to 'pkg/service/main_test.go')
-rw-r--r-- | pkg/service/main_test.go | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/pkg/service/main_test.go b/pkg/service/main_test.go new file mode 100644 index 0000000..5c10ecd --- /dev/null +++ b/pkg/service/main_test.go @@ -0,0 +1,121 @@ +//go:build unit + +package service + +import ( + "context" + "errors" + + "git.sr.ht/~gabrielgio/img/pkg/database/repository" +) + +type ( + User struct { + ID uint + Username string + Name string + Password []byte + IsAdmin bool + Path string + } + + Users map[uint]*User + + UserRepository struct { + icount uint + users Users + } +) + +var _ repository.UserRepository = &UserRepository{} +var _ repository.AuthRepository = &UserRepository{} + +func NewUserRepository() *UserRepository { + return &UserRepository{ + users: make(map[uint]*User), + } +} + +func (u *User) ToModel() *repository.User { + return &repository.User{ + ID: u.ID, + Username: u.Username, + Name: u.Name, + IsAdmin: u.IsAdmin, + Path: u.Path, + } +} + +func (u Users) ToModels() []*repository.User { + users := make([]*repository.User, 0, len(u)) + for _, i := range u { + users = append(users, i.ToModel()) + } + return users +} + +func (u *UserRepository) Get(ctx context.Context, id uint) (*repository.User, error) { + if user, ok := u.users[id]; ok { + return user.ToModel(), nil + } + + return nil, errors.New("Not Found") +} + +func (u *UserRepository) List(_ context.Context) ([]*repository.User, error) { + return u.users.ToModels(), nil +} + +func (u *UserRepository) Create(_ context.Context, createUser *repository.CreateUser) (uint, error) { + id := u.furtherID() + u.users[id] = &User{ + ID: id, + Name: createUser.Name, + Username: createUser.Username, + Path: createUser.Path, + Password: createUser.Password, + } + return id, nil +} + +func (u *UserRepository) Update(_ context.Context, id uint, updateUser *repository.UpdateUser) error { + user, ok := u.users[id] + if !ok { + return errors.New("Invalid ID") + } + + user.Name = updateUser.Name + user.Username = updateUser.Username + if updateUser.Password != "" { + user.Password = []byte(updateUser.Password) + } + + return nil +} + +func (u *UserRepository) Any(_ context.Context) (bool, error) { + return len(u.users) > 0, nil +} + +func (u *UserRepository) GetIDByUsername(ctx context.Context, username string) (uint, error) { + for id, u := range u.users { + if u.Username == username { + return id, nil + } + } + + return 0, errors.New("Not Found") +} + +func (u *UserRepository) GetPassword(ctx context.Context, id uint) ([]byte, error) { + if user, ok := u.users[id]; ok { + return []byte(user.Password), nil + } + + return nil, errors.New("Not Found") +} + +func (u *UserRepository) furtherID() uint { + u.icount++ + return u.icount +} |