feat: Role 增删改查
This commit is contained in:
@@ -6,9 +6,11 @@ type ApiGroup struct {
|
|||||||
AuthApi
|
AuthApi
|
||||||
UserApi
|
UserApi
|
||||||
ClientApi
|
ClientApi
|
||||||
|
RoleApi
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
UserService = service.ServiceGroupApp.SystemServiceGroup.UserService
|
UserService = service.ServiceGroupApp.SystemServiceGroup.UserService
|
||||||
ClientService = service.ServiceGroupApp.SystemServiceGroup.ClientService
|
ClientService = service.ServiceGroupApp.SystemServiceGroup.ClientService
|
||||||
|
RoleService = service.ServiceGroupApp.SystemServiceGroup.RoleService
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,98 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"go.uber.org/zap"
|
||||||
|
"sundynix-go/global"
|
||||||
|
"sundynix-go/model/commom/request"
|
||||||
|
"sundynix-go/model/commom/response"
|
||||||
|
"sundynix-go/model/system"
|
||||||
|
systemreq "sundynix-go/model/system/request"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RoleApi struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *RoleApi) SaveRole(context *gin.Context) {
|
||||||
|
var role system.Role
|
||||||
|
err := context.ShouldBindJSON(&role)
|
||||||
|
if err != nil {
|
||||||
|
response.FailWithMsg(err.Error(), context)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = RoleService.SaveRole(role)
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Error("保存角色失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), context)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.OkWithMsg("保存角色成功!", context)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *RoleApi) UpdateRole(context *gin.Context) {
|
||||||
|
var role system.Role
|
||||||
|
err := context.ShouldBindJSON(&role)
|
||||||
|
if err != nil {
|
||||||
|
response.FailWithMsg(err.Error(), context)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = RoleService.UpdateRole(role)
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Error("更新角色失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), context)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.OkWithMsg("更新角色成功!", context)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *RoleApi) GetRoleList(c *gin.Context) {
|
||||||
|
var pageInfo systemreq.GetRoleList
|
||||||
|
err := c.ShouldBindJSON(&pageInfo)
|
||||||
|
if err != nil {
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
list, total, err := RoleService.GetRoleList(pageInfo)
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Error("获取角色列表失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), c)
|
||||||
|
}
|
||||||
|
response.OkWithData(response.PageResult{
|
||||||
|
List: list,
|
||||||
|
Total: total,
|
||||||
|
Page: pageInfo.Current,
|
||||||
|
PageSize: pageInfo.PageSize,
|
||||||
|
}, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *RoleApi) Delete(context *gin.Context) {
|
||||||
|
var ids request.IdsReq
|
||||||
|
err := context.ShouldBindJSON(&ids)
|
||||||
|
if err != nil {
|
||||||
|
response.FailWithMsg(err.Error(), context)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = RoleService.DeleteRoleByIds(ids)
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Error("删除角色失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), context)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.OkWithMsg("删除角色成功!", context)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *RoleApi) Detail(context *gin.Context) {
|
||||||
|
var idInfo request.GetById
|
||||||
|
err := context.ShouldBindJSON(&idInfo)
|
||||||
|
if err != nil {
|
||||||
|
response.FailWithMsg(err.Error(), context)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
role, err := RoleService.GetRoleById(idInfo.ID)
|
||||||
|
if err != nil {
|
||||||
|
global.Logger.Error("获取角色详情失败!", zap.Error(err))
|
||||||
|
response.FailWithMsg(err.Error(), context)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
response.OkWithData(role, context)
|
||||||
|
}
|
||||||
@@ -35,6 +35,7 @@ func Routers() {
|
|||||||
//需要鉴权的路由
|
//需要鉴权的路由
|
||||||
systemRouter.InitUserRouter(NeedAuthGroup) //用户相关
|
systemRouter.InitUserRouter(NeedAuthGroup) //用户相关
|
||||||
systemRouter.InitClientRouter(NeedAuthGroup) //客户端相关
|
systemRouter.InitClientRouter(NeedAuthGroup) //客户端相关
|
||||||
|
systemRouter.InitRoleRouter(NeedAuthGroup) //角色相关
|
||||||
}
|
}
|
||||||
|
|
||||||
address := fmt.Sprintf(":%d", global.Config.System.Addr)
|
address := fmt.Sprintf(":%d", global.Config.System.Addr)
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package request
|
||||||
|
|
||||||
|
import common "sundynix-go/model/commom/request"
|
||||||
|
|
||||||
|
type GetRoleList struct {
|
||||||
|
common.PageInfo
|
||||||
|
Code string `json:"code" form:"code"`
|
||||||
|
Name string `json:"name" form:"name"`
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
import "sundynix-go/global"
|
||||||
|
|
||||||
|
type Role struct {
|
||||||
|
global.BaseModel
|
||||||
|
Name string `json:"name" form:"name"`
|
||||||
|
Code string `json:"code" form:"code"`
|
||||||
|
Sort int `json:"sort" form:"sort"`
|
||||||
|
}
|
||||||
@@ -6,6 +6,7 @@ type RouterGroup struct {
|
|||||||
AuthRouter
|
AuthRouter
|
||||||
UserRouter
|
UserRouter
|
||||||
ClientRouter
|
ClientRouter
|
||||||
|
RoleRouter
|
||||||
}
|
}
|
||||||
|
|
||||||
// 初始化路由
|
// 初始化路由
|
||||||
@@ -13,4 +14,5 @@ var (
|
|||||||
authApi = v1.ApiGroupApp.SystemApiGroup.AuthApi
|
authApi = v1.ApiGroupApp.SystemApiGroup.AuthApi
|
||||||
userApi = v1.ApiGroupApp.SystemApiGroup.UserApi
|
userApi = v1.ApiGroupApp.SystemApiGroup.UserApi
|
||||||
clientApi = v1.ApiGroupApp.SystemApiGroup.ClientApi
|
clientApi = v1.ApiGroupApp.SystemApiGroup.ClientApi
|
||||||
|
roleApi = v1.ApiGroupApp.SystemApiGroup.RoleApi
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
type RoleRouter struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *RoleRouter) InitRoleRouter(router *gin.RouterGroup) {
|
||||||
|
roleRouter := router.Group("role")
|
||||||
|
{
|
||||||
|
roleRouter.POST("save", roleApi.SaveRole)
|
||||||
|
roleRouter.POST("update", roleApi.UpdateRole)
|
||||||
|
roleRouter.POST("getRoleList", roleApi.GetRoleList)
|
||||||
|
roleRouter.GET("delete", roleApi.Delete)
|
||||||
|
roleRouter.GET("detail", roleApi.Detail)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,4 +3,5 @@ package system
|
|||||||
type ServiceGroup struct {
|
type ServiceGroup struct {
|
||||||
UserService
|
UserService
|
||||||
ClientService
|
ClientService
|
||||||
|
RoleService
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package system
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"sundynix-go/global"
|
||||||
|
common "sundynix-go/model/commom/request"
|
||||||
|
"sundynix-go/model/system"
|
||||||
|
systemReq "sundynix-go/model/system/request"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RoleService struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
var RoleServiceApp = new(RoleService)
|
||||||
|
|
||||||
|
func (s *RoleService) SaveRole(role system.Role) error {
|
||||||
|
if !errors.Is(global.DB.Where("code = ?", role.Code).First(&system.Role{}).Error, gorm.ErrRecordNotFound) {
|
||||||
|
return errors.New("存在重复角色")
|
||||||
|
}
|
||||||
|
return global.DB.Create(&role).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *RoleService) UpdateRole(role system.Role) error {
|
||||||
|
return global.DB.Model(&role).Where("id = ?", role.Id).Updates(&role).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *RoleService) GetRoleList(info systemReq.GetRoleList) (list interface{}, total int64, err error) {
|
||||||
|
limit := info.PageSize
|
||||||
|
offset := info.PageSize * (info.Current - 1)
|
||||||
|
db := global.DB.Model(&system.Role{})
|
||||||
|
var roleList []system.Role
|
||||||
|
if info.Code != "" {
|
||||||
|
db = db.Where("code = ?", info.Code)
|
||||||
|
}
|
||||||
|
if info.Name != "" {
|
||||||
|
db = db.Where("name LIKE ?", "%"+info.Name+"%")
|
||||||
|
}
|
||||||
|
err = db.Count(&total).Error
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = db.Limit(limit).Offset(offset).Find(&roleList).Error
|
||||||
|
return roleList, total, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *RoleService) DeleteRoleByIds(ids common.IdsReq) error {
|
||||||
|
return global.DB.Where("id in ?", ids.Ids).Delete(&system.Role{}).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *RoleService) GetRoleById(id int) (role system.Role, err error) {
|
||||||
|
err = global.DB.Where("id = ?", id).First(&role).Error
|
||||||
|
return role, err
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user