forked from go-kratos/kratos-layout
-
Notifications
You must be signed in to change notification settings - Fork 4
/
game.go
129 lines (119 loc) · 2.9 KB
/
game.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package data
import (
"context"
"strings"
"github.com/go-cinch/common/constant"
"github.com/go-cinch/common/copierx"
"github.com/go-cinch/common/log"
"github.com/go-cinch/common/utils"
"github.com/go-cinch/layout/internal/biz"
"github.com/go-cinch/layout/internal/data/model"
"github.com/go-cinch/layout/internal/data/query"
"gorm.io/gen"
)
type gameRepo struct {
data *Data
}
func NewGameRepo(data *Data) biz.GameRepo {
return &gameRepo{
data: data,
}
}
func (ro gameRepo) Create(ctx context.Context, item *biz.Game) (err error) {
err = ro.NameExists(ctx, item.Name)
if err == nil {
err = biz.ErrDuplicateField(ctx, "name", item.Name)
return
}
var m model.Game
copierx.Copy(&m, item)
p := query.Use(ro.data.DB(ctx)).Game
db := p.WithContext(ctx)
m.ID = ro.data.Id(ctx)
err = db.Create(&m)
return
}
func (ro gameRepo) Get(ctx context.Context, id uint64) (item *biz.Game, err error) {
item = &biz.Game{}
p := query.Use(ro.data.DB(ctx)).Game
db := p.WithContext(ctx)
m := db.GetByID(id)
if m.ID == constant.UI0 {
err = biz.ErrRecordNotFound(ctx)
return
}
copierx.Copy(&item, m)
return
}
func (ro gameRepo) Find(ctx context.Context, condition *biz.FindGame) (rp []biz.Game) {
p := query.Use(ro.data.DB(ctx)).Game
db := p.WithContext(ctx)
rp = make([]biz.Game, 0)
list := make([]model.Game, 0)
conditions := make([]gen.Condition, 0, 2)
if condition.Name != nil {
conditions = append(conditions, p.Name.Like(strings.Join([]string{"%", *condition.Name, "%"}, "")))
}
condition.Page.Primary = "id"
condition.Page.
WithContext(ctx).
Query(
db.
Order(p.ID.Desc()).
Where(conditions...).
UnderlyingDB(),
).
Find(&list)
copierx.Copy(&rp, list)
return
}
func (ro gameRepo) Update(ctx context.Context, item *biz.UpdateGame) (err error) {
p := query.Use(ro.data.DB(ctx)).Game
db := p.WithContext(ctx)
m := db.GetByID(item.Id)
if m.ID == constant.UI0 {
err = biz.ErrRecordNotFound(ctx)
return
}
change := make(map[string]interface{})
utils.CompareDiff(m, item, &change)
if len(change) == 0 {
err = biz.ErrDataNotChange(ctx)
return
}
if item.Name != nil && *item.Name != m.Name {
err = ro.NameExists(ctx, *item.Name)
if err == nil {
err = biz.ErrDuplicateField(ctx, "name", *item.Name)
return
}
}
_, err = db.
Where(p.ID.Eq(item.Id)).
Updates(&change)
return
}
func (ro gameRepo) Delete(ctx context.Context, ids ...uint64) (err error) {
p := query.Use(ro.data.DB(ctx)).Game
db := p.WithContext(ctx)
_, err = db.
Where(p.ID.In(ids...)).
Delete()
return
}
func (ro gameRepo) NameExists(ctx context.Context, name string) (err error) {
p := query.Use(ro.data.DB(ctx)).Game
db := p.WithContext(ctx)
arr := strings.Split(name, ",")
for _, item := range arr {
res := db.GetByCol("name", item)
if res.ID == constant.UI0 {
err = biz.ErrRecordNotFound(ctx)
log.
WithError(err).
Warn("invalid `name`: %s", name)
return
}
}
return
}