/
role.repo.go
96 lines (78 loc) · 2.44 KB
/
role.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
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
package role
import (
"context"
"github.com/google/wire"
"gorm.io/gorm"
"github.com/LyricTian/gin-admin/v8/internal/app/dao/util"
"github.com/LyricTian/gin-admin/v8/internal/app/schema"
"github.com/LyricTian/gin-admin/v8/pkg/errors"
)
var RoleSet = wire.NewSet(wire.Struct(new(RoleRepo), "*"))
type RoleRepo struct {
DB *gorm.DB
}
func (a *RoleRepo) getQueryOption(opts ...schema.RoleQueryOptions) schema.RoleQueryOptions {
var opt schema.RoleQueryOptions
if len(opts) > 0 {
opt = opts[0]
}
return opt
}
func (a *RoleRepo) Query(ctx context.Context, params schema.RoleQueryParam, opts ...schema.RoleQueryOptions) (*schema.RoleQueryResult, error) {
opt := a.getQueryOption(opts...)
db := GetRoleDB(ctx, a.DB)
if v := params.IDs; len(v) > 0 {
db = db.Where("id IN (?)", v)
}
if v := params.Name; v != "" {
db = db.Where("name=?", v)
}
if v := params.QueryValue; v != "" {
v = "%" + v + "%"
db = db.Where("name LIKE ?", v)
}
if len(opt.SelectFields) > 0 {
db = db.Select(opt.SelectFields)
}
if len(opt.OrderFields) > 0 {
db = db.Order(util.ParseOrder(opt.OrderFields))
}
var list Roles
pr, err := util.WrapPageQuery(ctx, db, params.PaginationParam, &list)
if err != nil {
return nil, errors.WithStack(err)
}
qr := &schema.RoleQueryResult{
PageResult: pr,
Data: list.ToSchemaRoles(),
}
return qr, nil
}
func (a *RoleRepo) Get(ctx context.Context, id uint64, opts ...schema.RoleQueryOptions) (*schema.Role, error) {
var role Role
ok, err := util.FindOne(ctx, GetRoleDB(ctx, a.DB).Where("id=?", id), &role)
if err != nil {
return nil, errors.WithStack(err)
} else if !ok {
return nil, nil
}
return role.ToSchemaRole(), nil
}
func (a *RoleRepo) Create(ctx context.Context, item schema.Role) error {
eitem := SchemaRole(item).ToRole()
result := GetRoleDB(ctx, a.DB).Create(eitem)
return errors.WithStack(result.Error)
}
func (a *RoleRepo) Update(ctx context.Context, id uint64, item schema.Role) error {
eitem := SchemaRole(item).ToRole()
result := GetRoleDB(ctx, a.DB).Where("id=?", id).Updates(eitem)
return errors.WithStack(result.Error)
}
func (a *RoleRepo) Delete(ctx context.Context, id uint64) error {
result := GetRoleDB(ctx, a.DB).Where("id=?", id).Delete(Role{})
return errors.WithStack(result.Error)
}
func (a *RoleRepo) UpdateStatus(ctx context.Context, id uint64, status int) error {
result := GetRoleDB(ctx, a.DB).Where("id=?", id).Update("status", status)
return errors.WithStack(result.Error)
}