lens @ 5168a9476f0e83264ecafc85bc9145e8bdcbb8dc

  1//go:build unit
  2
  3package service
  4
  5import (
  6	"context"
  7	"errors"
  8
  9	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
 10)
 11
 12type (
 13	User struct {
 14		ID       uint
 15		Username string
 16		Name     string
 17		Password []byte
 18		IsAdmin  bool
 19		Path     string
 20	}
 21
 22	Users map[uint]*User
 23
 24	UserRepository struct {
 25		icount uint
 26		users  Users
 27	}
 28)
 29
 30var _ repository.UserRepository = &UserRepository{}
 31
 32var _ repository.AuthRepository = &UserRepository{}
 33
 34func NewUserRepository() *UserRepository {
 35	return &UserRepository{
 36		users: make(map[uint]*User),
 37	}
 38}
 39
 40func (u *User) ToModel() *repository.User {
 41	return &repository.User{
 42		ID:       u.ID,
 43		Username: u.Username,
 44		Name:     u.Name,
 45		IsAdmin:  u.IsAdmin,
 46		Path:     u.Path,
 47	}
 48}
 49
 50func (u Users) ToModels() []*repository.User {
 51	users := make([]*repository.User, 0, len(u))
 52	for _, i := range u {
 53		users = append(users, i.ToModel())
 54	}
 55	return users
 56}
 57
 58func (u *UserRepository) Get(ctx context.Context, id uint) (*repository.User, error) {
 59	if user, ok := u.users[id]; ok {
 60		return user.ToModel(), nil
 61	}
 62
 63	return nil, errors.New("Not Found")
 64}
 65
 66func (u *UserRepository) List(_ context.Context) ([]*repository.User, error) {
 67	return u.users.ToModels(), nil
 68}
 69
 70func (u *UserRepository) Create(_ context.Context, createUser *repository.CreateUser) (uint, error) {
 71	id := u.furtherID()
 72	u.users[id] = &User{
 73		ID:       id,
 74		Name:     createUser.Name,
 75		Username: createUser.Username,
 76		Path:     createUser.Path,
 77		Password: createUser.Password,
 78	}
 79	return id, nil
 80}
 81
 82func (u *UserRepository) Update(_ context.Context, id uint, updateUser *repository.UpdateUser) error {
 83	user, ok := u.users[id]
 84	if !ok {
 85		return errors.New("Invalid ID")
 86	}
 87
 88	user.Name = updateUser.Name
 89	user.Username = updateUser.Username
 90	return nil
 91}
 92
 93func (u *UserRepository) Any(_ context.Context) (bool, error) {
 94	return len(u.users) > 0, nil
 95}
 96
 97func (u *UserRepository) GetIDByUsername(ctx context.Context, username string) (uint, error) {
 98	for id, u := range u.users {
 99		if u.Username == username {
100			return id, nil
101		}
102	}
103
104	return 0, errors.New("Not Found")
105}
106
107func (u *UserRepository) GetPassword(ctx context.Context, id uint) ([]byte, error) {
108	if user, ok := u.users[id]; ok {
109		return []byte(user.Password), nil
110	}
111
112	return nil, errors.New("Not Found")
113}
114
115func (u *UserRepository) furtherID() uint {
116	u.icount++
117	return u.icount
118}
119
120func (u *UserRepository) GetPathFromUserID(ctx context.Context, id uint) (string, error) {
121	if user, ok := u.users[id]; ok {
122		return user.Path, nil
123	}
124
125	return "", errors.New("Not Found")
126}
127
128func (u *UserRepository) UpdatePassword(ctx context.Context, id uint, password []byte) error {
129	panic("not implemented") // TODO: Implement
130}
131
132func (u *UserRepository) Delete(ctx context.Context, id uint) error {
133	panic("not implemented") // TODO: Implement
134}