Skip to content

Commit

Permalink
Merge pull request #22 from LyricTian/develop
Browse files Browse the repository at this point in the history
Optimize the storage layer
  • Loading branch information
LyricTian authored Apr 23, 2019
2 parents ca4496a + b0017e7 commit 7cdfcc7
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 57 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
- 日志追踪(基于[logrus](https://github.com/sirupsen/logrus),日志钩子支持 gorm)
- JWT 认证(基于黑名单的认证模式,存储支持:file/redis)
- 支持 Swagger 文档
- 支持跨域请求
- 支持请求频次限制
- 支持静态站点
- 单元测试

## 下载并运行
Expand Down
2 changes: 1 addition & 1 deletion internal/app/ginadmin/bll/b_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (a *Role) checkName(ctx context.Context, name string) error {
func (a *Role) Create(ctx context.Context, item schema.Role) (*schema.Role, error) {
err := a.checkName(ctx, item.Name)
if err != nil {
return nil, nil
return nil, err
}

item.RecordID = util.MustUUID()
Expand Down
28 changes: 25 additions & 3 deletions internal/app/ginadmin/model/gorm/entity/e_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (a SchemaMenu) ToMenu() *Menu {
Sequence: a.Sequence,
Icon: a.Icon,
Router: a.Router,
Hidden: a.Hidden,
Hidden: &a.Hidden,
ParentID: a.ParentID,
ParentPath: a.ParentPath,
Creator: a.Creator,
Expand Down Expand Up @@ -67,7 +67,7 @@ type Menu struct {
Sequence int `gorm:"column:sequence;index;"` // 排序值
Icon string `gorm:"column:icon;size:255;"` // 菜单图标
Router string `gorm:"column:router;size:255;"` // 访问路由
Hidden int `gorm:"column:hidden;index;"` // 隐藏菜单(0:不隐藏 1:隐藏)
Hidden *int `gorm:"column:hidden;index;"` // 隐藏菜单(0:不隐藏 1:隐藏)
ParentID string `gorm:"column:parent_id;size:36;index;"` // 父级内码
ParentPath string `gorm:"column:parent_path;size:518;index;"` // 父级路径
Creator string `gorm:"column:creator;size:36;"` // 创建人
Expand All @@ -90,7 +90,7 @@ func (a Menu) ToSchemaMenu() *schema.Menu {
Sequence: a.Sequence,
Icon: a.Icon,
Router: a.Router,
Hidden: a.Hidden,
Hidden: *a.Hidden,
ParentID: a.ParentID,
ParentPath: a.ParentPath,
Creator: a.Creator,
Expand Down Expand Up @@ -148,6 +148,17 @@ func (a MenuAction) ToSchemaMenuAction() *schema.MenuAction {
// MenuActions 菜单动作关联实体列表
type MenuActions []*MenuAction

// GetByMenuID 根据菜单ID获取菜单动作列表
func (a MenuActions) GetByMenuID(menuID string) []*schema.MenuAction {
var list []*schema.MenuAction
for _, item := range a {
if item.MenuID == menuID {
list = append(list, item.ToSchemaMenuAction())
}
}
return list
}

// ToSchemaMenuActions 转换为菜单动作列表
func (a MenuActions) ToSchemaMenuActions() []*schema.MenuAction {
list := make([]*schema.MenuAction, len(a))
Expand Down Expand Up @@ -208,6 +219,17 @@ func (a MenuResource) ToSchemaMenuResource() *schema.MenuResource {
// MenuResources 菜单资源关联实体列表
type MenuResources []*MenuResource

// GetByMenuID 根据菜单ID获取菜单资源列表
func (a MenuResources) GetByMenuID(menuID string) []*schema.MenuResource {
var list []*schema.MenuResource
for _, item := range a {
if item.MenuID == menuID {
list = append(list, item.ToSchemaMenuResource())
}
}
return list
}

// ToSchemaMenuResources 转换为菜单资源列表
func (a MenuResources) ToSchemaMenuResources() []*schema.MenuResource {
list := make([]*schema.MenuResource, len(a))
Expand Down
11 changes: 11 additions & 0 deletions internal/app/ginadmin/model/gorm/entity/e_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ func (a RoleMenu) ToSchemaRoleMenu() *schema.RoleMenu {
// RoleMenus 角色菜单关联实体列表
type RoleMenus []*RoleMenu

// GetByRoleID 根据角色ID获取角色菜单对象列表
func (a RoleMenus) GetByRoleID(roleID string) []*schema.RoleMenu {
var list []*schema.RoleMenu
for _, item := range a {
if item.RoleID == roleID {
list = append(list, item.ToSchemaRoleMenu())
}
}
return list
}

// ToSchemaRoleMenus 转换为角色菜单对象列表
func (a RoleMenus) ToSchemaRoleMenus() []*schema.RoleMenu {
list := make([]*schema.RoleMenu, len(a))
Expand Down
11 changes: 11 additions & 0 deletions internal/app/ginadmin/model/gorm/entity/e_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ func (a UserRole) ToSchemaUserRole() *schema.UserRole {
// UserRoles 用户角色关联列表
type UserRoles []*UserRole

// GetByUserID 根据用户ID获取用户角色对象列表
func (a UserRoles) GetByUserID(userID string) []*schema.UserRole {
var list []*schema.UserRole
for _, item := range a {
if item.UserID == userID {
list = append(list, item.ToSchemaUserRole())
}
}
return list
}

// ToSchemaUserRoles 转换为用户角色对象列表
func (a UserRoles) ToSchemaUserRoles() []*schema.UserRole {
list := make([]*schema.UserRole, len(a))
Expand Down
62 changes: 40 additions & 22 deletions internal/app/ginadmin/model/gorm/model/m_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,35 +68,53 @@ func (a *Menu) Query(ctx context.Context, params schema.MenuQueryParam, opts ...
Data: list.ToSchemaMenus(),
}

for _, item := range qr.Data {
err := a.fillSchemaMenu(ctx, item, opts...)
if err != nil {
return nil, err
}
err = a.fillSchemaMenus(ctx, qr.Data, opts...)
if err != nil {
return nil, err
}

return qr, nil
}

// 填充菜单对象数据
func (a *Menu) fillSchemaMenu(ctx context.Context, item *schema.Menu, opts ...schema.MenuQueryOptions) error {
func (a *Menu) fillSchemaMenus(ctx context.Context, items []*schema.Menu, opts ...schema.MenuQueryOptions) error {
opt := a.getQueryOption(opts...)

if opt.IncludeActions {
list, err := a.queryActions(ctx, item.RecordID)
if err != nil {
return err
if opt.IncludeActions || opt.IncludeResources {

menuIDs := make([]string, len(items))
for i, item := range items {
menuIDs[i] = item.RecordID
}
item.Actions = list.ToSchemaMenuActions()
}

if opt.IncludeResources {
list, err := a.queryResources(ctx, item.RecordID)
if err != nil {
return err
var actionList entity.MenuActions
var resourceList entity.MenuResources
if opt.IncludeActions {
items, err := a.queryActions(ctx, menuIDs...)
if err != nil {
return err
}
actionList = items
}

if opt.IncludeResources {
items, err := a.queryResources(ctx, menuIDs...)
if err != nil {
return err
}
resourceList = items
}

for i, item := range items {
if len(actionList) > 0 {
items[i].Actions = actionList.GetByMenuID(item.RecordID)
}
if len(resourceList) > 0 {
items[i].Resources = resourceList.GetByMenuID(item.RecordID)
}
}
item.Resources = list.ToSchemaMenuResources()
}

return nil
}

Expand All @@ -115,7 +133,7 @@ func (a *Menu) Get(ctx context.Context, recordID string, opts ...schema.MenuQuer
}

sitem := item.ToSchemaMenu()
err = a.fillSchemaMenu(ctx, sitem, opts...)
err = a.fillSchemaMenus(ctx, []*schema.Menu{sitem}, opts...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -331,12 +349,12 @@ func (a *Menu) Delete(ctx context.Context, recordID string) error {
})
}

func (a *Menu) queryActions(ctx context.Context, menuID string) (entity.MenuActions, error) {
func (a *Menu) queryActions(ctx context.Context, menuIDs ...string) (entity.MenuActions, error) {
span := logger.StartSpan(ctx, "查询菜单动作数据", a.getFuncName("queryActions"))
defer span.Finish()

var list entity.MenuActions
result := entity.GetMenuActionDB(ctx, a.db).Where("menu_id=?", menuID).Find(&list)
result := entity.GetMenuActionDB(ctx, a.db).Where("menu_id IN(?)", menuIDs).Find(&list)
if err := result.Error; err != nil {
span.Errorf(err.Error())
return nil, errors.New("查询菜单动作数据发生错误")
Expand All @@ -345,12 +363,12 @@ func (a *Menu) queryActions(ctx context.Context, menuID string) (entity.MenuActi
return list, nil
}

func (a *Menu) queryResources(ctx context.Context, menuID string) (entity.MenuResources, error) {
func (a *Menu) queryResources(ctx context.Context, menuIDs ...string) (entity.MenuResources, error) {
span := logger.StartSpan(ctx, "查询菜单资源数据", a.getFuncName("queryResources"))
defer span.Finish()

var list entity.MenuResources
result := entity.GetMenuResourceDB(ctx, a.db).Where("menu_id=?", menuID).Find(&list)
result := entity.GetMenuResourceDB(ctx, a.db).Where("menu_id IN(?)", menuIDs).Find(&list)
if err := result.Error; err != nil {
span.Errorf(err.Error())
return nil, errors.New("查询菜单资源数据发生错误")
Expand Down
39 changes: 26 additions & 13 deletions internal/app/ginadmin/model/gorm/model/m_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,39 @@ func (a *Role) Query(ctx context.Context, params schema.RoleQueryParam, opts ...
Data: list.ToSchemaRoles(),
}

for _, item := range qr.Data {
err := a.fillSchameRole(ctx, item, opts...)
if err != nil {
return nil, err
}
err = a.fillSchameRoles(ctx, qr.Data, opts...)
if err != nil {
return nil, err
}

return qr, nil
}

// 填充角色对象
func (a *Role) fillSchameRole(ctx context.Context, item *schema.Role, opts ...schema.RoleQueryOptions) error {
func (a *Role) fillSchameRoles(ctx context.Context, items []*schema.Role, opts ...schema.RoleQueryOptions) error {
opt := a.getQueryOption(opts...)

if opt.IncludeMenus {
list, err := a.queryMenus(ctx, item.RecordID)
if err != nil {
return err

roleIDs := make([]string, len(items))
for i, item := range items {
roleIDs[i] = item.RecordID
}

var menuList entity.RoleMenus
if opt.IncludeMenus {
items, err := a.queryMenus(ctx, roleIDs...)
if err != nil {
return err
}
menuList = items
}

for i, item := range items {
if len(menuList) > 0 {
items[i].Menus = menuList.GetByRoleID(item.RecordID)
}
}
item.Menus = list.ToSchemaRoleMenus()
}
return nil
}
Expand All @@ -105,7 +118,7 @@ func (a *Role) Get(ctx context.Context, recordID string, opts ...schema.RoleQuer
}

sitem := role.ToSchemaRole()
err = a.fillSchameRole(ctx, sitem, opts...)
err = a.fillSchameRoles(ctx, []*schema.Role{sitem}, opts...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -236,12 +249,12 @@ func (a *Role) Delete(ctx context.Context, recordID string) error {
})
}

func (a *Role) queryMenus(ctx context.Context, roleID string) (entity.RoleMenus, error) {
func (a *Role) queryMenus(ctx context.Context, roleIDs ...string) (entity.RoleMenus, error) {
span := logger.StartSpan(ctx, "查询角色菜单数据", a.getFuncName("queryMenus"))
defer span.Finish()

var list entity.RoleMenus
result := entity.GetRoleMenuDB(ctx, a.db).Where("role_id=?", roleID).Find(&list)
result := entity.GetRoleMenuDB(ctx, a.db).Where("role_id IN(?)", roleIDs).Find(&list)
if err := result.Error; err != nil {
span.Errorf(err.Error())
return nil, errors.New("查询角色菜单数据发生错误")
Expand Down
41 changes: 26 additions & 15 deletions internal/app/ginadmin/model/gorm/model/m_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,26 +70,37 @@ func (a *User) Query(ctx context.Context, params schema.UserQueryParam, opts ...
Data: list.ToSchemaUsers(),
}

for i, item := range qr.Data {
err := a.fillSchemaUser(ctx, item, opts...)
if err != nil {
return nil, err
}
qr.Data[i] = item
err = a.fillSchemaUsers(ctx, qr.Data, opts...)
if err != nil {
return nil, err
}

return qr, nil
}

func (a *User) fillSchemaUser(ctx context.Context, item *schema.User, opts ...schema.UserQueryOptions) error {
func (a *User) fillSchemaUsers(ctx context.Context, items []*schema.User, opts ...schema.UserQueryOptions) error {
opt := a.getQueryOption(opts...)

if opt.IncludeRoles {
list, err := a.queryRoles(ctx, item.RecordID)
if err != nil {
return err
userIDs := make([]string, len(items))
for i, item := range items {
userIDs[i] = item.RecordID
}

var roleList entity.UserRoles
if opt.IncludeRoles {
items, err := a.queryRoles(ctx, userIDs...)
if err != nil {
return err
}
roleList = items
}

for i, item := range items {
if len(roleList) > 0 {
items[i].Roles = roleList.GetByUserID(item.RecordID)
}
}
item.Roles = list.ToSchemaUserRoles()
}

return nil
Expand All @@ -110,7 +121,7 @@ func (a *User) Get(ctx context.Context, recordID string, opts ...schema.UserQuer
}

sitem := item.ToSchemaUser()
err = a.fillSchemaUser(ctx, sitem, opts...)
err = a.fillSchemaUsers(ctx, []*schema.User{sitem}, opts...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -273,15 +284,15 @@ func (a *User) UpdatePassword(ctx context.Context, recordID, password string) er
return nil
}

func (a *User) queryRoles(ctx context.Context, userID string) (entity.UserRoles, error) {
func (a *User) queryRoles(ctx context.Context, userIDs ...string) (entity.UserRoles, error) {
span := logger.StartSpan(ctx, "查询用户角色数据", a.getFuncName("queryRoles"))
defer span.Finish()

var list entity.UserRoles
result := entity.GetUserRoleDB(ctx, a.db).Where("user_id=?", userID).Find(&list)
result := entity.GetUserRoleDB(ctx, a.db).Where("user_id IN(?)", userIDs).Find(&list)
if err := result.Error; err != nil {
span.Errorf(err.Error())
return nil, errors.New("查询角色ID列表发生错误")
return nil, errors.New("查询用户角色数据发生错误")
}
return list, nil
}

0 comments on commit 7cdfcc7

Please sign in to comment.