/
testRepo.go
68 lines (46 loc) · 935 Bytes
/
testRepo.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
package main
import (
"sync"
"github.com/ariandi/gocom"
"github.com/oklog/ulid/v2"
"gorm.io/gorm"
)
// Model --------------------------------------
type Test struct {
gorm.Model
ID string
DataString string
DataInt int
DataBool bool
}
func (o *Test) BeforeCreate(db *gorm.DB) error {
if o.ID == "" {
o.ID = ulid.Make().String()
}
return nil
}
// Repo --------------------------------------
type TestRepo struct {
gocom.BaseRepo
}
var testRepo *TestRepo
var testRepoOnce sync.Once
func (o *TestRepo) GetOne(id string) *Test {
ret := &Test{}
if o.Where("id = ?", id).First(ret).Error == nil {
return ret
}
return nil
}
func (o *TestRepo) GetAll() []*Test {
var ret []*Test = []*Test{}
o.Model(&Test{}).Order("id asc").Find(&ret)
return ret
}
func GetTestRepo() *TestRepo {
testRepoOnce.Do(func() {
testRepo = &TestRepo{}
testRepo.AutoMigrate(&Test{})
})
return testRepo
}