-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathrepository.go
50 lines (43 loc) · 1.02 KB
/
repository.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
package main
import (
pb "github.com/EwanValentine/shippy-user-service/proto/auth"
"github.com/jinzhu/gorm"
)
type Repository interface {
GetAll() ([]*pb.User, error)
Get(id string) (*pb.User, error)
Create(user *pb.User) error
GetByEmail(email string) (*pb.User, error)
}
type UserRepository struct {
db *gorm.DB
}
func (repo *UserRepository) GetAll() ([]*pb.User, error) {
var users []*pb.User
if err := repo.db.Find(&users).Error; err != nil {
return nil, err
}
return users, nil
}
func (repo *UserRepository) Get(id string) (*pb.User, error) {
var user *pb.User
user.Id = id
if err := repo.db.First(&user).Error; err != nil {
return nil, err
}
return user, nil
}
func (repo *UserRepository) GetByEmail(email string) (*pb.User, error) {
user := &pb.User{}
if err := repo.db.Where("email = ?", email).
First(&user).Error; err != nil {
return nil, err
}
return user, nil
}
func (repo *UserRepository) Create(user *pb.User) error {
if err := repo.db.Create(user).Error; err != nil {
return err
}
return nil
}