aboutsummaryrefslogtreecommitdiff
path: root/pkg/database/sql/user.go
blob: d449b05078914829508c79dd8da8583b7f66cbc9 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package sql

import (
	"context"

	"golang.org/x/crypto/bcrypt"
	"gorm.io/gorm"

	"git.sr.ht/~gabrielgio/img/pkg/components/auth"
	user "git.sr.ht/~gabrielgio/img/pkg/components/auth"
)

type (
	User struct {
		gorm.Model
		Username string
		Name     string
		Password string
	}

	Users []*User

	UserRepository struct {
		db *gorm.DB
	}
)

var _ auth.Repository = &UserRepository{}

func NewUserRepository(db *gorm.DB) *UserRepository {
	return &UserRepository{
		db: db,
	}
}

func (self *User) ToModel() *user.User {
	return &user.User{
		ID:       self.Model.ID,
		Name:     self.Name,
		Username: self.Username,
	}
}

func (self Users) ToModel() (users []*user.User) {
	for _, user := range self {
		users = append(users, user.ToModel())
	}
	return
}

// Testing function, will remove later
// TODO: remove later
func (self *UserRepository) EnsureAdmin(ctx context.Context) {
	var exists bool
	self.db.
		WithContext(ctx).
		Model(&User{}).
		Select("count(*) > 0").
		Where("username = ?", "admin").
		Find(&exists)

	if !exists {
		hash, _ := bcrypt.GenerateFromPassword([]byte("admin"), bcrypt.MinCost)
		self.db.Save(&User{
			Username: "admin",
			Password: string(hash),
		})
	}
}

func (self *UserRepository) List(ctx context.Context) ([]*user.User, error) {
	users := Users{}
	result := self.db.
		WithContext(ctx).
		Find(&users)

	if result.Error != nil {
		return nil, result.Error
	}

	return users.ToModel(), nil
}

func (self *UserRepository) Get(ctx context.Context, id uint) (*user.User, error) {
	var user = &user.User{ID: id}
	result := self.db.
		WithContext(ctx).
		First(user)

	if result.Error != nil {
		return nil, result.Error
	}

	return user, nil
}

func (self *UserRepository) GetIDByUsername(ctx context.Context, username string) (uint, error) {
	userID := struct {
		ID uint
	}{}

	result := self.db.
		WithContext(ctx).
		Model(&User{}).
		Where("username = ?", username).
		First(&userID)

	if result.Error != nil {
		return 0, result.Error
	}

	return userID.ID, nil
}

func (self *UserRepository) GetPassword(ctx context.Context, id uint) ([]byte, error) {
	userPassword := struct {
		Password []byte
	}{}

	result := self.db.
		WithContext(ctx).
		Model(&User{}).
		Where("id = ?", id).
		First(&userPassword)

	if result.Error != nil {
		return nil, result.Error
	}

	return userPassword.Password, nil
}

func (self *UserRepository) Create(ctx context.Context, createUser *user.CreateUser) (uint, error) {
	user := &User{
		Username: createUser.Username,
		Name:     createUser.Name,
		Password: string(createUser.Password),
	}

	result := self.db.
		WithContext(ctx).
		Create(user)
	if result.Error != nil {
		return 0, result.Error
	}

	return user.Model.ID, nil
}

func (self *UserRepository) Update(ctx context.Context, id uint, update *user.UpdateUser) error {
	user := &User{
		Model: gorm.Model{
			ID: id,
		},
		Username: update.Username,
		Name:     update.Name,
	}

	result := self.db.
		WithContext(ctx).
		Save(user)
	if result.Error != nil {
		return result.Error
	}

	return nil
}

func (self *UserRepository) Delete(ctx context.Context, id uint) error {
	userID := struct {
		ID uint
	}{
		ID: id,
	}
	result := self.db.
		WithContext(ctx).
		Delete(userID)
	if result.Error != nil {
		return result.Error
	}
	return nil
}