新增角色接口
This commit is contained in:
parent
9868e9a3a8
commit
f2dea316bd
|
|
@ -55,13 +55,15 @@ CREATE TABLE IF NOT EXISTS `user_organization` (
|
|||
-- 角色表
|
||||
CREATE TABLE IF NOT EXISTS `role` (
|
||||
`id` int UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'id' ,
|
||||
`code` VARCHAR(255) NOT NULL COMMENT 'code',
|
||||
`name` VARCHAR(255) NOT NULL COMMENT '名称',
|
||||
`created_by` VARCHAR(64) DEFAULT '' COMMENT '创建人',
|
||||
`created_on` DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '记录创建时间',
|
||||
`modified_by` VARCHAR(64) DEFAULT '' COMMENT '修改人',
|
||||
`modified_on` DATETIME DEFAULT CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP COMMENT '记录修改时间',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb3 COMMENT='表';
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_code` (`code`)
|
||||
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb3 COMMENT='角色表';
|
||||
|
||||
|
||||
-- 组织角色表
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
package role
|
||||
|
||||
type Query struct {
|
||||
Page int `form:"page" json:"page" `
|
||||
PageSize int `form:"page_size" json:"page_size"`
|
||||
Keyword string `form:"keyword" Json:"keyword"`
|
||||
Sort string `form:"sort" json:"sort" binding:"sql_sort"`
|
||||
}
|
||||
|
||||
func (q *Query) Default() {
|
||||
if q.Page < 0 {
|
||||
q.Page = 0
|
||||
}
|
||||
|
||||
if q.PageSize <= 0 {
|
||||
q.PageSize = 20
|
||||
}
|
||||
|
||||
if len(q.Sort) == 0 {
|
||||
q.Sort = "created_on desc"
|
||||
}
|
||||
}
|
||||
|
||||
type CreateReq struct {
|
||||
Code string `json:"code" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
}
|
||||
|
||||
type SaveReq struct {
|
||||
Id uint `json:"id" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Code string `json:"code" binding:"required"`
|
||||
}
|
||||
|
||||
type DelReq struct {
|
||||
Id uint `json:"id" binding:"required"`
|
||||
}
|
||||
|
||||
type GetReq struct {
|
||||
Id uint `form:"id" json:"id" binding:"required"`
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package role
|
||||
|
||||
import "time"
|
||||
|
||||
type Role struct {
|
||||
ID uint `json:"id"` // id
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"` // 名称
|
||||
CreatedOn time.Time `json:"created_on"` // 记录创建时间
|
||||
}
|
||||
|
|
@ -2,8 +2,9 @@
|
|||
package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"fmt"
|
||||
"github.com/samber/do"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
|
@ -15,6 +16,7 @@ func init(){
|
|||
// 表
|
||||
type Role struct {
|
||||
ID uint `gorm:"column:id;primary_key;AUTO_INCREMENT"` // id
|
||||
Code string `gorm:"column:code;NOT NULL"` // code
|
||||
Name string `gorm:"column:name;NOT NULL"` // 名称
|
||||
CreatedBy string `gorm:"column:created_by"` // 创建人
|
||||
CreatedOn time.Time `gorm:"column:created_on;default:CURRENT_TIMESTAMP;NOT NULL"` // 记录创建时间
|
||||
|
|
@ -27,7 +29,13 @@ func (m *Role) TableName() string {
|
|||
}
|
||||
|
||||
type RoleRepo interface{
|
||||
|
||||
ExistCode(ctx context.Context,code string)(exist bool,err error)
|
||||
ExistId(ctx context.Context,id uint)(exist bool,err error)
|
||||
Create(ctx context.Context,role Role)error
|
||||
Save(ctx context.Context,role Role)error
|
||||
Search(ctx context.Context,query Query)([]Role,error)
|
||||
GetById(ctx context.Context,id uint)(Role,error)
|
||||
DelById(ctx context.Context,id uint)error
|
||||
}
|
||||
|
||||
type roleRepo struct{
|
||||
|
|
@ -38,4 +46,55 @@ func NewRoleRepo(i *do.Injector)(RoleRepo,error){
|
|||
return &roleRepo{
|
||||
db :do.MustInvoke[*gorm.DB](i),
|
||||
},nil
|
||||
}
|
||||
|
||||
func (r *roleRepo)ExistCode(ctx context.Context,code string)(exist bool,err error){
|
||||
var count int64 = 0
|
||||
err = r.db.Model(&Role{}).Where("code = ?",code).Count(&count).Error
|
||||
if count > 0{
|
||||
return true,err
|
||||
}
|
||||
|
||||
return false,err
|
||||
}
|
||||
|
||||
func (r *roleRepo)ExistId(ctx context.Context,id uint)(exist bool,err error){
|
||||
var count int64 = 0
|
||||
err = r.db.Model(&Role{}).Where("id = ?",id).Count(&count).Error
|
||||
if count > 0{
|
||||
return true,err
|
||||
}
|
||||
|
||||
return false,err
|
||||
}
|
||||
|
||||
func (r *roleRepo)Create(ctx context.Context,role Role)error{
|
||||
return r.db.Create(&role).Error
|
||||
}
|
||||
|
||||
func (r *roleRepo)Save(ctx context.Context,role Role)error{
|
||||
return r.db.Model(&Role{}).Where("id = ?",role.ID).Update("name",role.Name).Error
|
||||
}
|
||||
|
||||
func (r *roleRepo)Search(ctx context.Context,query Query)([]Role,error){
|
||||
roles := make([]Role, 0)
|
||||
|
||||
keyword := fmt.Sprintf("%%%s%%", query.Keyword)
|
||||
db := r.db.Model(&Role{})
|
||||
if query.Keyword != "" {
|
||||
db = db.Where("code like ? or name like ?", keyword, keyword)
|
||||
}
|
||||
|
||||
err := db.Order(query.Sort).Limit(query.PageSize).Offset(query.Page * query.PageSize).Find(&roles).Error
|
||||
return roles, err
|
||||
}
|
||||
|
||||
func (r *roleRepo)GetById(ctx context.Context,id uint)(Role,error){
|
||||
role := Role{}
|
||||
err := r.db.Where("id = ?",id).Take(&role).Error
|
||||
return role,err
|
||||
}
|
||||
|
||||
func (r *roleRepo)DelById(ctx context.Context,id uint)error{
|
||||
return r.db.Where("id = ?",id).Delete(&Role{}).Error
|
||||
}
|
||||
|
|
@ -6,12 +6,12 @@ import (
|
|||
)
|
||||
|
||||
type OrganizationService interface {
|
||||
CreateOrganization(ctx context.Context, info orgModel.CreateOrgReq) error
|
||||
SaveOrganization(ctx context.Context, info orgModel.SaveOrgReq) error
|
||||
DelOrganization(ctx context.Context, info orgModel.DelOrgReq) error
|
||||
Organization(ctx context.Context, info orgModel.GetOrgReq) (orgModel.Organization, error)
|
||||
MoveOrganization(ctx context.Context, info orgModel.MoveOrgReq) error
|
||||
CreateOrganization(ctx context.Context, info *orgModel.CreateOrgReq) error
|
||||
SaveOrganization(ctx context.Context, info *orgModel.SaveOrgReq) error
|
||||
DelOrganization(ctx context.Context, info *orgModel.DelOrgReq) error
|
||||
Organization(ctx context.Context, info *orgModel.GetOrgReq) (orgModel.Organization, error)
|
||||
MoveOrganization(ctx context.Context, info *orgModel.MoveOrgReq) error
|
||||
OrganizationTree(ctx context.Context) (orgModel.OrgTree, error)
|
||||
DisableOrganization(ctx context.Context) error
|
||||
EnableOrganization(ctx context.Context) error
|
||||
DisableOrganization(ctx context.Context, info *orgModel.DisableOrgReq) error
|
||||
EnableOrganization(ctx context.Context, info *orgModel.EnableOrgReq) error
|
||||
}
|
||||
|
|
|
|||
|
|
@ -130,13 +130,13 @@ func (o *organizationService) OrganizationTree(ctx context.Context) (orgModel.Or
|
|||
return tree, nil
|
||||
}
|
||||
|
||||
func (o *organizationService) DisableOrganization(ctx context.Context, info orgModel.DisableOrgReq) error {
|
||||
func (o *organizationService) DisableOrganization(ctx context.Context, info *orgModel.DisableOrgReq) error {
|
||||
// todo 判断有没有权限
|
||||
// 判断是否成员
|
||||
return o.orgRepo.SetStatus(ctx, info.Id, repo.OrganizationDisableStatus)
|
||||
}
|
||||
|
||||
func (o *organizationService) EnableOrganization(ctx context.Context, info orgModel.EnableOrgReq) error {
|
||||
func (o *organizationService) EnableOrganization(ctx context.Context, info *orgModel.EnableOrgReq) error {
|
||||
// todo 判断有没有权限
|
||||
return o.orgRepo.SetStatus(ctx, info.Id, repo.OrganizationEnableStatus)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,15 @@
|
|||
package role
|
||||
|
||||
import (
|
||||
roleModel "busniess-user-center/internal/models/role"
|
||||
"busniess-user-center/internal/repo"
|
||||
)
|
||||
|
||||
func convertDTM(role repo.Role) roleModel.Role {
|
||||
return roleModel.Role{
|
||||
ID: role.ID,
|
||||
Name: role.Name,
|
||||
Code: role.Code,
|
||||
CreatedOn: role.CreatedOn,
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
package role
|
||||
|
||||
import (
|
||||
roleModel "busniess-user-center/internal/models/role"
|
||||
"context"
|
||||
)
|
||||
|
||||
type RoleService interface {
|
||||
Create(ctx context.Context, info *roleModel.CreateReq) error
|
||||
Save(ctx context.Context, info *roleModel.SaveReq) error
|
||||
Delete(ctx context.Context, info *roleModel.DelReq) error
|
||||
Role(ctx context.Context, info *roleModel.GetReq) (roleModel.Role, error)
|
||||
Search(ctx context.Context, info *roleModel.Query) ([]roleModel.Role, error)
|
||||
}
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
package role
|
||||
|
||||
import (
|
||||
"busniess-user-center/config"
|
||||
roleModel "busniess-user-center/internal/models/role"
|
||||
"busniess-user-center/internal/repo"
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/samber/do"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func init() {
|
||||
do.Provide(nil, NewRoleService)
|
||||
}
|
||||
|
||||
type roleService struct {
|
||||
logger *zap.SugaredLogger
|
||||
roleRepo repo.RoleRepo
|
||||
conf *config.AppConfig
|
||||
}
|
||||
|
||||
func NewRoleService(i *do.Injector) (RoleService, error) {
|
||||
return &roleService{
|
||||
logger: do.MustInvoke[*zap.SugaredLogger](i),
|
||||
roleRepo: do.MustInvoke[repo.RoleRepo](i),
|
||||
conf: do.MustInvoke[*config.AppConfig](i),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (o *roleService) Create(ctx context.Context, info *roleModel.CreateReq) error {
|
||||
dbRole := repo.Role{
|
||||
Code: info.Code,
|
||||
Name: info.Name,
|
||||
}
|
||||
|
||||
exist, err := o.roleRepo.ExistCode(ctx, info.Code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if exist {
|
||||
return fmt.Errorf("角色code:%s已经存在", info.Code)
|
||||
}
|
||||
|
||||
return o.roleRepo.Create(ctx, dbRole)
|
||||
}
|
||||
|
||||
func (o *roleService) Save(ctx context.Context, info *roleModel.SaveReq) error {
|
||||
dbRole, err := o.roleRepo.GetById(ctx, info.Id)
|
||||
if err != nil && err != gorm.ErrRecordNotFound {
|
||||
return err
|
||||
}
|
||||
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return fmt.Errorf("角色:%s不存在", info.Code)
|
||||
}
|
||||
|
||||
dbRole.Name = info.Name
|
||||
|
||||
return o.roleRepo.Save(ctx, dbRole)
|
||||
}
|
||||
|
||||
func (o *roleService) Delete(ctx context.Context, info *roleModel.DelReq) error {
|
||||
return o.roleRepo.DelById(ctx, info.Id)
|
||||
}
|
||||
|
||||
func (o *roleService) Role(ctx context.Context, info *roleModel.GetReq) (role roleModel.Role, err error) {
|
||||
dbRole, err := o.roleRepo.GetById(ctx, info.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = copier.Copy(role, dbRole)
|
||||
return
|
||||
}
|
||||
|
||||
func (o *roleService) Search(ctx context.Context, info *roleModel.Query) ([]roleModel.Role, error) {
|
||||
dbQuery := repo.Query{}
|
||||
copier.Copy(dbQuery, info)
|
||||
dbRoles, err := o.roleRepo.Search(ctx, dbQuery)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
roles := make([]roleModel.Role, len(dbRoles))
|
||||
for _, role := range dbRoles {
|
||||
roles = append(roles, convertDTM(role))
|
||||
}
|
||||
|
||||
return roles, nil
|
||||
}
|
||||
|
|
@ -14,6 +14,9 @@ import (
|
|||
"busniess-user-center/pkg/redis"
|
||||
"busniess-user-center/pkg/utils/token"
|
||||
"busniess-user-center/pkg/validator"
|
||||
"busniess-user-center/server/application"
|
||||
"busniess-user-center/server/organization"
|
||||
"busniess-user-center/server/role"
|
||||
"busniess-user-center/server/user"
|
||||
|
||||
"github.com/gin-contrib/gzip"
|
||||
|
|
@ -104,6 +107,15 @@ func registerWebRoute(engine *gin.Engine) error {
|
|||
userApi := api.Group("/user")
|
||||
user.RegisterRoute(userApi)
|
||||
|
||||
orgApi := api.Group("/organization")
|
||||
organization.RegisterRoute(orgApi)
|
||||
|
||||
roleApi := api.Group("/role")
|
||||
role.RegisterRoute(roleApi)
|
||||
|
||||
appApi := api.Group("/application")
|
||||
application.RegisterRoute(appApi)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -36,8 +36,8 @@ func RegisterRoute(api *gin.RouterGroup) {
|
|||
api.GET("/get", ginUtil.Wrap(server.Organization))
|
||||
api.POST("/move", ginUtil.WrapNoRsp(server.MoveOrganization))
|
||||
api.GET("/orgs", ginUtil.WrapNoReq(server.OrganizationTree))
|
||||
api.POST("/disenable", ginUtil.WrapNo(server.DisableOrganization))
|
||||
api.POST("/enable", ginUtil.WrapNo(server.EnableOrganization))
|
||||
api.POST("/disenable", ginUtil.WrapNoRsp(server.DisableOrganization))
|
||||
api.POST("/enable", ginUtil.WrapNoRsp(server.EnableOrganization))
|
||||
}
|
||||
|
||||
func (u *OrganizationServer) Create(ctx context.Context, req *orgModel.CreateOrgReq) (err error) {
|
||||
|
|
@ -65,10 +65,10 @@ func (u *OrganizationServer) OrganizationTree(ctx context.Context) (orgModel.Org
|
|||
return u.organizationService.OrganizationTree(ctx)
|
||||
}
|
||||
|
||||
func (u *OrganizationServer) DisableOrganization(ctx context.Context) error {
|
||||
return u.organizationService.DisableOrganization(ctx)
|
||||
func (u *OrganizationServer) DisableOrganization(ctx context.Context, info *orgModel.DisableOrgReq) error {
|
||||
return u.organizationService.DisableOrganization(ctx, info)
|
||||
}
|
||||
|
||||
func (u *OrganizationServer) EnableOrganization(ctx context.Context) error {
|
||||
return u.organizationService.EnableOrganization(ctx)
|
||||
func (u *OrganizationServer) EnableOrganization(ctx context.Context, info *orgModel.EnableOrgReq) error {
|
||||
return u.organizationService.EnableOrganization(ctx, info)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,59 @@
|
|||
package role
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
roleModel "busniess-user-center/internal/models/role"
|
||||
roleService "busniess-user-center/internal/service/role"
|
||||
ginUtil "busniess-user-center/pkg/utils/gin"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/samber/do"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func init() {
|
||||
do.Provide(nil, NewRoleServer)
|
||||
}
|
||||
|
||||
type RoleServer struct {
|
||||
roleService roleService.RoleService
|
||||
logger *zap.SugaredLogger
|
||||
}
|
||||
|
||||
func NewRoleServer(i *do.Injector) (*RoleServer, error) {
|
||||
return &RoleServer{
|
||||
roleService: do.MustInvoke[roleService.RoleService](i),
|
||||
logger: do.MustInvoke[*zap.SugaredLogger](i),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func RegisterRoute(api *gin.RouterGroup) {
|
||||
server := do.MustInvoke[*RoleServer](nil)
|
||||
api.POST("/create", ginUtil.WrapNoRsp(server.Create))
|
||||
api.POST("/save", ginUtil.WrapNoRsp(server.Save))
|
||||
api.POST("/delete", ginUtil.WrapNoRsp(server.DelRole))
|
||||
api.GET("/get", ginUtil.Wrap(server.Role))
|
||||
api.GET("/search", ginUtil.Wrap(server.Search))
|
||||
}
|
||||
|
||||
func (u *RoleServer) Create(ctx context.Context, req *roleModel.CreateReq) (err error) {
|
||||
// 转换dto
|
||||
return u.roleService.Create(ctx, req)
|
||||
}
|
||||
|
||||
func (u *RoleServer) Save(ctx context.Context, info *roleModel.SaveReq) error {
|
||||
return u.roleService.Save(ctx, info)
|
||||
}
|
||||
|
||||
func (u *RoleServer) DelRole(ctx context.Context, info *roleModel.DelReq) error {
|
||||
return u.roleService.Delete(ctx, info)
|
||||
}
|
||||
|
||||
func (u *RoleServer) Role(ctx context.Context, info *roleModel.GetReq) (roleModel.Role, error) {
|
||||
return u.roleService.Role(ctx, info)
|
||||
}
|
||||
|
||||
func (u *RoleServer) Search(ctx context.Context, info *roleModel.Query) ([]roleModel.Role, error) {
|
||||
return u.roleService.Search(ctx, info)
|
||||
}
|
||||
Loading…
Reference in New Issue