generated from evrone/go-clean-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
80 lines (57 loc) · 1.65 KB
/
user.go
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
package repo
import (
"context"
"fmt"
"github.com/dimk00z/GophKeeper/internal/entity"
"github.com/dimk00z/GophKeeper/internal/server/usecase/repo/models"
"github.com/dimk00z/GophKeeper/internal/utils"
"github.com/dimk00z/GophKeeper/internal/utils/errs"
"github.com/google/uuid"
)
func (r *GophKeeperRepo) GetUserByEmail(ctx context.Context, email, hashedPassword string) (user entity.User, err error) {
var userFromDB models.User
r.db.WithContext(ctx).Where("email = ?", email).First(&userFromDB)
if userFromDB.ID == uuid.Nil {
err = errs.ErrWrongCredentials
return
}
if err = utils.VerifyPassword(userFromDB.Password, hashedPassword); err != nil {
err = errs.ErrWrongCredentials
return
}
user.ID = userFromDB.ID
user.Email = userFromDB.Email
return
}
func (r *GophKeeperRepo) AddUser(ctx context.Context, email, hashedPassword string) (user entity.User, err error) {
newUser := models.User{
Email: email,
Password: hashedPassword,
}
result := r.db.WithContext(ctx).Create(&newUser)
if result.Error == nil {
user.ID = newUser.ID
user.Email = newUser.Email
return
}
switch errs.ParsePostgresErr(result.Error).Code {
case "23505":
r.l.Debug("AddUser - %w", result.Error)
err = errs.ErrEmailAlreadyExists
return
default:
err = fmt.Errorf("AddUser - %w", result.Error)
return
}
}
func (r *GophKeeperRepo) GetUserByID(ctx context.Context, id string) (user entity.User, err error) {
var userFromDB models.User
r.db.WithContext(ctx).Where("id = ?", id).First(&userFromDB)
if userFromDB.ID == uuid.Nil {
err = errs.ErrWrongCredentials
return
}
user.ID = userFromDB.ID
user.Email = userFromDB.Email
return
}