Passed
Push — main ( f0dc1d...8ca1be )
by Yume
01:47 queued 29s
created

user.*SQLRepository.GetByEmail   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
// SQLRepository is the repository for the user.
9
type SQLRepository struct {
10
	DBConn *gorm.DB // DBConn is the database connection.
11
}
12
13
// NewRepository returns a new repository.
14
func NewRepository(dbConn *gorm.DB) IRepository {
15
	return &SQLRepository{DBConn: dbConn}
16
}
17
18
// GetName returns the name of the user.
19
func (r *SQLRepository) GetName(id uint) string {
20
	var user domain.User
21
	r.DBConn.First(&user, id)
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
44
// Update updates the user with the given id.
45
func (r *SQLRepository) Update(user *domain.User) error {
46
	return r.DBConn.Save(&user).Error
47
}
48
49
// Delete deletes the user with the given id.
50
func (r *SQLRepository) Delete(id uint) error {
51
	return r.DBConn.Delete(&domain.User{}, id).Error
52
}
53
54
// GetByOauthID returns the user with the given oauth id.
55
func (r *SQLRepository) GetByOauthID(id string) (domain.User, error) {
56
	var user domain.User
57
	err := r.DBConn.Where("oauth_id = ?", id).First(&user).Error
58
	return user, err
59
}
60