all repos

onasty @ aebfa02

a one-time notes service

onasty/internal/store/psql/userepo/userepo.go(view raw)

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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package userepo

import (
	"context"
	"errors"

	"github.com/gofrs/uuid/v5"
	"github.com/henvic/pgq"
	"github.com/jackc/pgx/v5"
	"github.com/olexsmir/onasty/internal/models"
	"github.com/olexsmir/onasty/internal/store/psqlutil"
)

type UserStorer interface {
	Create(ctx context.Context, inp models.User) (uuid.UUID, error)

	// GetByEmail returns user by email and password
	// the password should be hashed
	GetByEmail(ctx context.Context, email string) (models.User, error)
	GetUserIDByEmail(ctx context.Context, email string) (uuid.UUID, error)
	GetByID(ctx context.Context, userID uuid.UUID) (models.User, error)

	MarkUserAsActivated(ctx context.Context, id uuid.UUID) error

	// ChangePassword changes user password from oldPassword to newPassword
	// and oldPassword and newPassword should be hashed
	ChangePassword(ctx context.Context, userID uuid.UUID, newPassword string) error

	// SetPassword sets new password for user by their id
	// password should be hashed
	SetPassword(ctx context.Context, userID uuid.UUID, newPassword string) error

	GetByOAuthID(ctx context.Context, provider, providerID string) (models.User, error)
	LinkOAuthIdentity(ctx context.Context, userID uuid.UUID, provider, providerID string) error

	CheckIfUserExists(ctx context.Context, userID uuid.UUID) (bool, error)
	CheckIfUserIsActivated(ctx context.Context, userID uuid.UUID) (bool, error)
}

var _ UserStorer = (*UserRepo)(nil)

type UserRepo struct {
	db *psqlutil.DB
}

func New(db *psqlutil.DB) *UserRepo {
	return &UserRepo{
		db: db,
	}
}

func (r *UserRepo) Create(ctx context.Context, inp models.User) (uuid.UUID, error) {
	query, args, err := pgq.
		Insert("users").
		Columns("email", "password", "activated", "created_at", "last_login_at").
		Values(inp.Email, inp.Password, inp.Activated, inp.CreatedAt, inp.LastLoginAt).
		Returning("id").
		SQL()
	if err != nil {
		return uuid.UUID{}, err
	}

	var id uuid.UUID
	err = r.db.QueryRow(ctx, query, args...).Scan(&id)

	// FIXME: somehow this does return errors but i can't errors.Is them in api layer
	if psqlutil.IsDuplicateErr(err, "users_email_key") {
		return uuid.UUID{}, models.ErrUserEmailIsAlreadyInUse
	}

	return id, err
}

func (r *UserRepo) GetByEmail(
	ctx context.Context,
	email string,
) (models.User, error) {
	query, args, err := pgq.
		Select("id", "email", "password", "activated", "created_at", "last_login_at").
		From("users").
		Where(pgq.Eq{"email": email}).
		SQL()
	if err != nil {
		return models.User{}, err
	}

	var user models.User
	err = r.db.QueryRow(ctx, query, args...).
		Scan(&user.ID, &user.Email, &user.Password, &user.Activated, &user.CreatedAt, &user.LastLoginAt)
	if errors.Is(err, pgx.ErrNoRows) {
		return models.User{}, models.ErrUserNotFound
	}

	return user, err
}

func (r *UserRepo) GetUserIDByEmail(ctx context.Context, email string) (uuid.UUID, error) {
	query, args, err := pgq.
		Select("id").
		From("users").
		Where(pgq.Eq{"email": email}).
		SQL()
	if err != nil {
		return uuid.Nil, err
	}

	var id uuid.UUID
	err = r.db.QueryRow(ctx, query, args...).Scan(&id)
	if errors.Is(err, pgx.ErrNoRows) {
		return uuid.Nil, models.ErrUserNotFound
	}

	return id, err
}

func (r *UserRepo) GetByID(ctx context.Context, userID uuid.UUID) (models.User, error) {
	query := `--sql
select id, email, password, activated, created_at, last_login_at
from users
where id = $1`

	var user models.User
	err := r.db.QueryRow(ctx, query, userID).
		Scan(&user.ID, &user.Email, &user.Password, &user.Activated, &user.CreatedAt, &user.LastLoginAt)
	if errors.Is(err, pgx.ErrNoRows) {
		return models.User{}, models.ErrUserNotFound
	}

	return user, err
}

func (r *UserRepo) GetByOAuthID(
	ctx context.Context,
	provider, providerID string,
) (models.User, error) {
	query := `--sql
	select u.id, u.email, u.password, u.activated, u.created_at, u.last_login_at
	from users u
	join oauth_identities oi on u.id = oi.user_id
	where oi.provider = $1
		and oi.provider_id = $2
	limit 1`

	var user models.User
	err := r.db.QueryRow(ctx, query, provider, providerID).
		Scan(&user.ID, &user.Email, &user.Password, &user.Activated, &user.CreatedAt, &user.LastLoginAt)
	if errors.Is(err, pgx.ErrNoRows) {
		return models.User{}, models.ErrUserNotFound
	}

	return user, err
}

func (r *UserRepo) LinkOAuthIdentity(
	ctx context.Context,
	userID uuid.UUID,
	provider, providerID string,
) error {
	query := `--sql
insert into oauth_identities (user_id, provider, provider_id)
values ($1, $2, $3)
on conflict (provider, provider_id) do update
set user_id = $1,
	provider = $2,
	provider_id = $3`

	_, err := r.db.Exec(ctx, query, userID, provider, providerID)
	return err
}

func (r *UserRepo) MarkUserAsActivated(ctx context.Context, id uuid.UUID) error {
	query, args, err := pgq.
		Update("users").
		Set("activated ", true).
		Where(pgq.Eq{"id": id.String()}).
		SQL()
	if err != nil {
		return err
	}

	_, err = r.db.Exec(ctx, query, args...)
	return err
}

func (r *UserRepo) ChangePassword(
	ctx context.Context,
	userID uuid.UUID,
	newPasswd string,
) error {
	query, args, err := pgq.
		Update("users").
		Set("password", newPasswd).
		Where(pgq.Eq{"id": userID.String()}).
		SQL()
	if err != nil {
		return err
	}
	_, err = r.db.Exec(ctx, query, args...)
	return err
}

func (r *UserRepo) SetPassword(ctx context.Context, userID uuid.UUID, password string) error {
	query, args, err := pgq.
		Update("users").
		Set("password", password).
		Where(pgq.Eq{"id": userID.String()}).
		SQL()
	if err != nil {
		return err
	}

	_, err = r.db.Exec(ctx, query, args...)
	return err
}

func (r *UserRepo) CheckIfUserExists(ctx context.Context, id uuid.UUID) (bool, error) {
	var exists bool
	err := r.db.QueryRow(
		ctx,
		`SELECT EXISTS(SELECT 1 FROM users WHERE id = $1)`,
		id.String(),
	).Scan(&exists)
	if errors.Is(err, pgx.ErrNoRows) {
		return false, models.ErrUserNotFound
	}

	return exists, err
}

func (r *UserRepo) CheckIfUserIsActivated(ctx context.Context, id uuid.UUID) (bool, error) {
	var activated bool
	err := r.db.QueryRow(ctx, `SELECT activated FROM users WHERE id = $1`, id.String()).
		Scan(&activated)
	if errors.Is(err, pgx.ErrNoRows) {
		return false, models.ErrUserNotFound
	}
	return activated, err
}