Passed
Push — main ( f6597a...58b626 )
by Yume
01:31 queued 12s
created

user.*SqlRepository.GetName   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
package user
2
3
import (
4
	"github.com/memnix/memnix-rest/domain"
5
	"gorm.io/gorm"
6
)
7
8
type SqlRepository struct {
9
	DBConn *gorm.DB
10
}
11
12
func NewRepository(dbConn *gorm.DB) IRepository {
13
	return &SqlRepository{DBConn: dbConn}
14
}
15
16
// GetName returns the name of the user.
17
func (r *SqlRepository) GetName(id uint) string {
18
	var user domain.User
19
	r.DBConn.First(&user, id)
20
	return user.Username
21
}
22
23
// GetByID returns the user with the given id.
24
func (r *SqlRepository) GetByID(id uint) (domain.User, error) {
25
	var user domain.User
26
	err := r.DBConn.First(&user, id).Error
27
	return user, err
28
}
29
30
// GetByEmail returns the user with the given email.
31
func (r *SqlRepository) GetByEmail(email string) (domain.User, error) {
32
	var user domain.User
33
	err := r.DBConn.Where("email = ?", email).First(&user).Error
34
	return user, err
35
}
36
37
// Create creates a new user.
38
func (r *SqlRepository) Create(user *domain.User) error {
39
	return r.DBConn.Create(&user).Error
40
}
41
42
func (r *SqlRepository) Update(user *domain.User) error {
43
	return r.DBConn.Save(&user).Error
44
}
45
46
func (r *SqlRepository) Delete(id uint) error {
47
	return r.DBConn.Delete(&domain.User{}, id).Error
48
}
49
50
func (r *SqlRepository) GetByOauthID(id string) (domain.User, error) {
51
	var user domain.User
52
	err := r.DBConn.Where("oauth_id = ?", id).First(&user).Error
53
	return user, err
54
}
55