57 lines
1.3 KiB
Go
57 lines
1.3 KiB
Go
|
|
package logic
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
|
||
|
|
"muyu-apiserver/model"
|
||
|
|
"muyu-apiserver/rpc/system/internal/svc"
|
||
|
|
"muyu-apiserver/rpc/system/pb"
|
||
|
|
|
||
|
|
"github.com/zeromicro/go-zero/core/logx"
|
||
|
|
"google.golang.org/grpc/codes"
|
||
|
|
"google.golang.org/grpc/status"
|
||
|
|
)
|
||
|
|
|
||
|
|
type GetRoleLogic struct {
|
||
|
|
ctx context.Context
|
||
|
|
svcCtx *svc.ServiceContext
|
||
|
|
logx.Logger
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewGetRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetRoleLogic {
|
||
|
|
return &GetRoleLogic{
|
||
|
|
ctx: ctx,
|
||
|
|
svcCtx: svcCtx,
|
||
|
|
Logger: logx.WithContext(ctx),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (l *GetRoleLogic) GetRole(in *pb.GetRoleReq) (*pb.RoleInfo, error) {
|
||
|
|
role, err := l.svcCtx.RoleModel.FindOneByRoleId(l.ctx, in.RoleId)
|
||
|
|
if err != nil {
|
||
|
|
if errors.Is(err, model.ErrNotFound) {
|
||
|
|
return nil, status.Error(codes.NotFound, "role not found")
|
||
|
|
}
|
||
|
|
return nil, status.Error(codes.Internal, err.Error())
|
||
|
|
}
|
||
|
|
|
||
|
|
menuIds := make([]string, 0)
|
||
|
|
roleMenus, _ := l.svcCtx.RoleMenuModel.FindByRoleId(l.ctx, role.RoleId)
|
||
|
|
for _, rm := range roleMenus {
|
||
|
|
menuIds = append(menuIds, rm.MenuId)
|
||
|
|
}
|
||
|
|
|
||
|
|
return &pb.RoleInfo{
|
||
|
|
RoleId: role.RoleId,
|
||
|
|
RoleName: role.RoleName,
|
||
|
|
RoleKey: role.RoleKey,
|
||
|
|
RoleDesc: role.RoleDesc,
|
||
|
|
SortOrder: role.SortOrder,
|
||
|
|
Status: role.Status,
|
||
|
|
MenuIds: menuIds,
|
||
|
|
CreatedAt: role.CreatedAt.Format("2006-01-02 15:04:05"),
|
||
|
|
UpdatedAt: role.UpdatedAt.Format("2006-01-02 15:04:05"),
|
||
|
|
}, nil
|
||
|
|
}
|