-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgorm_repository.go
77 lines (61 loc) · 1.75 KB
/
gorm_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package gorepo
import "github.com/jinzhu/gorm"
type GormRepository struct {
BaseRepository
Db *gorm.DB
}
func NewGormRepository(db *gorm.DB) GormRepository {
return GormRepository{Db:db}
}
func (gr *GormRepository) Initialize(args ...interface{}) {
if len(args) == 0 {
panic("*gorm.DB must be supplied for initialization")
}
if _, ok := args[0].(*gorm.DB); !ok {
panic("The first arg must be *gorm.DB")
}
gr.InitDb(args[0].(*gorm.DB))
}
func (gr *GormRepository) InitDb(db *gorm.DB) {
gr.Db = db
}
func (gr GormRepository) Insert(model IModel) (uint, error){
if err := model.Validate(); err != nil{
return 0, err
}
if err := gr.Db.Create(model).Error; err != nil{
return 0, err
}
return model.GetId(), nil
}
func (gr GormRepository) Update(model IModel) (error){
if err := model.Validate(); err != nil{
return err
}
return gr.Db.Save(model).Error
}
func (gr GormRepository) Save(model IModel) (uint, error){
if err := model.Validate(); err != nil{
return 0, err
}
if err := gr.Db.Save(model).Error; err != nil{
return 0, err
}
return model.GetId(), nil
}
func (gr GormRepository) FindById(receiver IModel, id uint) (error){
return gr.Db.First(receiver, id).Error
}
func (gr GormRepository) FindFirst(receiver IModel, where string, args ...interface{}) (error){
return gr.Db.Where(where, args...).Limit(1).Find(receiver).Error
}
func (gr GormRepository) FindAll(models interface{}, where string, args ...interface{}) (err error){
err = gr.Db.Where(where, args...).Find(models).Error
return
}
func (gr GormRepository) Delete(model IModel, where string, args ...interface{}) error {
return gr.Db.Where(where, args...).Delete(&model).Error
}
func (gr GormRepository) NewRecord(model IModel) bool {
return gr.Db.NewRecord(&model)
}