aboutsummaryrefslogtreecommitdiff
path: root/pkg/service/main_test.go
blob: e1214dccec1e7c9fcf99fd4697db2452b6400e07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//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
}

func (u *UserRepository) GetPathFromUserID(ctx context.Context, id uint) (string, error) {
	if user, ok := u.users[id]; ok {
		return user.Path, nil
	}

	return "", errors.New("Not Found")
}