Total Lines | 41 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | package user |
||
2 | |||
3 | import ( |
||
4 | "github.com/memnix/memnix-rest/domain" |
||
5 | "github.com/rs/zerolog/log" |
||
6 | "gorm.io/gorm" |
||
7 | ) |
||
8 | |||
9 | type SqlRepository struct { |
||
10 | DBConn *gorm.DB |
||
11 | } |
||
12 | |||
13 | func NewRepository(dbConn *gorm.DB) IRepository { |
||
14 | return &SqlRepository{DBConn: dbConn} |
||
15 | } |
||
16 | |||
17 | // GetName returns the name of the user. |
||
18 | func (r *SqlRepository) GetName(id uint) string { |
||
19 | var user domain.User |
||
20 | r.DBConn.First(&user, id) |
||
21 | log.Info().Msgf("user: %v", user) |
||
22 | return user.Username |
||
23 | } |
||
24 | |||
25 | // GetByID returns the user with the given id. |
||
26 | func (r *SqlRepository) GetByID(id uint) (domain.User, error) { |
||
27 | var user domain.User |
||
28 | err := r.DBConn.First(&user, id).Error |
||
29 | return user, err |
||
30 | } |
||
31 | |||
32 | // GetByEmail returns the user with the given email. |
||
33 | func (r *SqlRepository) GetByEmail(email string) (domain.User, error) { |
||
34 | var user domain.User |
||
35 | err := r.DBConn.Where("email = ?", email).First(&user).Error |
||
36 | return user, err |
||
37 | } |
||
38 | |||
39 | // Create creates a new user. |
||
40 | func (r *SqlRepository) Create(user *domain.User) error { |
||
41 | return r.DBConn.Create(&user).Error |
||
42 | } |
||
43 |