-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_repo.go
48 lines (39 loc) · 1 KB
/
auth_repo.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
package auth
import (
"errors"
"github.com/adiet95/go-order-api/src/database/models"
"gorm.io/gorm"
)
type auth_repo struct {
db *gorm.DB
}
func NewRepo(db *gorm.DB) *auth_repo {
return &auth_repo{db}
}
func (re *auth_repo) FindByEmail(email string) (*models.User, error) {
var data *models.User
var datas *models.Users
res := re.db.Model(&datas).Where("email = ?", email).Find(&data)
if res.Error != nil {
return nil, errors.New("failed to find data")
}
if res.RowsAffected == 0 {
return nil, errors.New("email not found")
}
return data, nil
}
func (re *auth_repo) RegisterEmail(data *models.User) (*models.User, error) {
var datas *models.Users
res := re.db.Model(&datas).Where("email = ?", data.Email).Find(&data)
if res.Error != nil {
return nil, errors.New("failed to find data")
}
if res.RowsAffected > 0 {
return nil, errors.New("email registered, go to login")
}
r := re.db.Create(data)
if r.Error != nil {
return nil, errors.New("failled to obtain data")
}
return data, nil
}