fix
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package admin_api
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminBatchUpdateApiStatusLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminBatchUpdateApiStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminBatchUpdateApiStatusLogic {
|
||||
return &AdminBatchUpdateApiStatusLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminBatchUpdateApiStatusLogic) AdminBatchUpdateApiStatus(req *types.AdminBatchUpdateApiStatusReq) (resp *types.AdminBatchUpdateApiStatusResp, err error) {
|
||||
// 1. 参数验证
|
||||
if len(req.Ids) == 0 {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"API ID列表不能为空")
|
||||
}
|
||||
if req.Status != 0 && req.Status != 1 {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"状态值无效, status: %d", req.Status)
|
||||
}
|
||||
|
||||
// 2. 批量更新API状态
|
||||
successCount := 0
|
||||
for _, id := range req.Ids {
|
||||
if id <= 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// 查询API是否存在
|
||||
api, err := l.svcCtx.AdminApiModel.FindOne(l.ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
continue // 跳过不存在的API
|
||||
}
|
||||
logx.Errorf("查询API失败, err: %v, id: %d", err, id)
|
||||
continue
|
||||
}
|
||||
|
||||
// 更新状态
|
||||
api.Status = req.Status
|
||||
_, err = l.svcCtx.AdminApiModel.Update(l.ctx, nil, api)
|
||||
if err != nil {
|
||||
logx.Errorf("更新API状态失败, err: %v, id: %d", err, id)
|
||||
continue
|
||||
}
|
||||
|
||||
successCount++
|
||||
}
|
||||
|
||||
// 3. 返回结果
|
||||
return &types.AdminBatchUpdateApiStatusResp{Success: true}, nil
|
||||
}
|
||||
78
app/main/api/internal/logic/admin_api/admincreateapilogic.go
Normal file
78
app/main/api/internal/logic/admin_api/admincreateapilogic.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package admin_api
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminCreateApiLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminCreateApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminCreateApiLogic {
|
||||
return &AdminCreateApiLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminCreateApiLogic) AdminCreateApi(req *types.AdminCreateApiReq) (resp *types.AdminCreateApiResp, err error) {
|
||||
// 1. 参数验证
|
||||
if req.ApiName == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"API名称不能为空")
|
||||
}
|
||||
if req.ApiCode == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"API编码不能为空")
|
||||
}
|
||||
if req.Method == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"请求方法不能为空")
|
||||
}
|
||||
if req.Url == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"API路径不能为空")
|
||||
}
|
||||
|
||||
// 2. 检查API编码是否已存在
|
||||
existing, err := l.svcCtx.AdminApiModel.FindOneByApiCode(l.ctx, req.ApiCode)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"查询API失败, err: %v, apiCode: %s", err, req.ApiCode)
|
||||
}
|
||||
if existing != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"API编码已存在: %s", req.ApiCode)
|
||||
}
|
||||
|
||||
// 3. 创建API记录
|
||||
apiData := &model.AdminApi{
|
||||
ApiName: req.ApiName,
|
||||
ApiCode: req.ApiCode,
|
||||
Method: req.Method,
|
||||
Url: req.Url,
|
||||
Status: req.Status,
|
||||
Description: req.Description,
|
||||
}
|
||||
|
||||
result, err := l.svcCtx.AdminApiModel.Insert(l.ctx, nil, apiData)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"创建API失败, err: %v", err)
|
||||
}
|
||||
|
||||
// 4. 返回结果
|
||||
apiId, _ := result.LastInsertId()
|
||||
return &types.AdminCreateApiResp{Id: apiId}, nil
|
||||
}
|
||||
68
app/main/api/internal/logic/admin_api/admindeleteapilogic.go
Normal file
68
app/main/api/internal/logic/admin_api/admindeleteapilogic.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package admin_api
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminDeleteApiLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminDeleteApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminDeleteApiLogic {
|
||||
return &AdminDeleteApiLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminDeleteApiLogic) AdminDeleteApi(req *types.AdminDeleteApiReq) (resp *types.AdminDeleteApiResp, err error) {
|
||||
// 1. 参数验证
|
||||
if req.Id <= 0 {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"API ID必须大于0, id: %d", req.Id)
|
||||
}
|
||||
|
||||
// 2. 查询API是否存在
|
||||
api, err := l.svcCtx.AdminApiModel.FindOne(l.ctx, req.Id)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"API不存在, id: %d", req.Id)
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"查询API失败, err: %v, id: %d", err, req.Id)
|
||||
}
|
||||
|
||||
// 3. 检查是否有角色关联该API
|
||||
roleApiBuilder := l.svcCtx.AdminRoleApiModel.SelectBuilder().Where("api_id = ?", req.Id)
|
||||
roleApis, err := l.svcCtx.AdminRoleApiModel.FindAll(l.ctx, roleApiBuilder, "")
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"查询角色API关联失败, err: %v, apiId: %d", err, req.Id)
|
||||
}
|
||||
if len(roleApis) > 0 {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"该API已被角色使用,无法删除, apiId: %d", req.Id)
|
||||
}
|
||||
|
||||
// 4. 执行软删除
|
||||
err = l.svcCtx.AdminApiModel.DeleteSoft(l.ctx, nil, api)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"删除API失败, err: %v, id: %d", err, req.Id)
|
||||
}
|
||||
|
||||
// 5. 返回结果
|
||||
return &types.AdminDeleteApiResp{Success: true}, nil
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package admin_api
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminGetApiDetailLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminGetApiDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGetApiDetailLogic {
|
||||
return &AdminGetApiDetailLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminGetApiDetailLogic) AdminGetApiDetail(req *types.AdminGetApiDetailReq) (resp *types.AdminGetApiDetailResp, err error) {
|
||||
// 1. 参数验证
|
||||
if req.Id <= 0 {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"API ID必须大于0, id: %d", req.Id)
|
||||
}
|
||||
|
||||
// 2. 查询API详情
|
||||
api, err := l.svcCtx.AdminApiModel.FindOne(l.ctx, req.Id)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"API不存在, id: %d", req.Id)
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"查询API详情失败, err: %v, id: %d", err, req.Id)
|
||||
}
|
||||
|
||||
// 3. 返回结果
|
||||
return &types.AdminGetApiDetailResp{
|
||||
AdminApiInfo: types.AdminApiInfo{
|
||||
Id: api.Id,
|
||||
ApiName: api.ApiName,
|
||||
ApiCode: api.ApiCode,
|
||||
Method: api.Method,
|
||||
Url: api.Url,
|
||||
Status: api.Status,
|
||||
Description: api.Description,
|
||||
CreateTime: api.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
UpdateTime: api.UpdateTime.Format("2006-01-02 15:04:05"),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package admin_api
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminGetApiListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminGetApiListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGetApiListLogic {
|
||||
return &AdminGetApiListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminGetApiListLogic) AdminGetApiList(req *types.AdminGetApiListReq) (resp *types.AdminGetApiListResp, err error) {
|
||||
// 1. 参数验证
|
||||
if req.Page <= 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.PageSize <= 0 {
|
||||
req.PageSize = 20
|
||||
}
|
||||
if req.PageSize > 100 {
|
||||
req.PageSize = 100
|
||||
}
|
||||
|
||||
// 2. 构建查询条件
|
||||
builder := l.svcCtx.AdminApiModel.SelectBuilder()
|
||||
|
||||
// 添加搜索条件
|
||||
if req.ApiName != "" {
|
||||
builder = builder.Where("api_name LIKE ?", "%"+req.ApiName+"%")
|
||||
}
|
||||
if req.Method != "" {
|
||||
builder = builder.Where("method = ?", req.Method)
|
||||
}
|
||||
if req.Status > 0 {
|
||||
builder = builder.Where("status = ?", req.Status)
|
||||
}
|
||||
|
||||
// 3. 查询总数
|
||||
total, err := l.svcCtx.AdminApiModel.FindCount(l.ctx, builder, "id")
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"查询API总数失败, err: %v", err)
|
||||
}
|
||||
|
||||
// 4. 查询列表
|
||||
apis, err := l.svcCtx.AdminApiModel.FindPageListByPage(l.ctx, builder, req.Page, req.PageSize, "id DESC")
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"查询API列表失败, err: %v", err)
|
||||
}
|
||||
|
||||
// 5. 转换数据格式
|
||||
var apiList []types.AdminApiInfo
|
||||
for _, api := range apis {
|
||||
apiList = append(apiList, types.AdminApiInfo{
|
||||
Id: api.Id,
|
||||
ApiName: api.ApiName,
|
||||
ApiCode: api.ApiCode,
|
||||
Method: api.Method,
|
||||
Url: api.Url,
|
||||
Status: api.Status,
|
||||
Description: api.Description,
|
||||
CreateTime: api.CreateTime.Format("2006-01-02 15:04:05"),
|
||||
UpdateTime: api.UpdateTime.Format("2006-01-02 15:04:05"),
|
||||
})
|
||||
}
|
||||
|
||||
// 6. 返回结果
|
||||
return &types.AdminGetApiListResp{
|
||||
Items: apiList,
|
||||
Total: total,
|
||||
}, nil
|
||||
}
|
||||
92
app/main/api/internal/logic/admin_api/adminupdateapilogic.go
Normal file
92
app/main/api/internal/logic/admin_api/adminupdateapilogic.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package admin_api
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"tydata-server/app/main/api/internal/svc"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type AdminUpdateApiLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewAdminUpdateApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminUpdateApiLogic {
|
||||
return &AdminUpdateApiLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *AdminUpdateApiLogic) AdminUpdateApi(req *types.AdminUpdateApiReq) (resp *types.AdminUpdateApiResp, err error) {
|
||||
// 1. 参数验证
|
||||
if req.Id <= 0 {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"API ID必须大于0, id: %d", req.Id)
|
||||
}
|
||||
if req.ApiName == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"API名称不能为空")
|
||||
}
|
||||
if req.ApiCode == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"API编码不能为空")
|
||||
}
|
||||
if req.Method == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"请求方法不能为空")
|
||||
}
|
||||
if req.Url == "" {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"API路径不能为空")
|
||||
}
|
||||
|
||||
// 2. 查询API是否存在
|
||||
api, err := l.svcCtx.AdminApiModel.FindOne(l.ctx, req.Id)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"API不存在, id: %d", req.Id)
|
||||
}
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"查询API失败, err: %v, id: %d", err, req.Id)
|
||||
}
|
||||
|
||||
// 3. 检查API编码是否被其他记录使用
|
||||
if api.ApiCode != req.ApiCode {
|
||||
existing, err := l.svcCtx.AdminApiModel.FindOneByApiCode(l.ctx, req.ApiCode)
|
||||
if err != nil && !errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"查询API失败, err: %v, apiCode: %s", err, req.ApiCode)
|
||||
}
|
||||
if existing != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.PARAM_VERIFICATION_ERROR),
|
||||
"API编码已存在: %s", req.ApiCode)
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 更新API信息
|
||||
api.ApiName = req.ApiName
|
||||
api.ApiCode = req.ApiCode
|
||||
api.Method = req.Method
|
||||
api.Url = req.Url
|
||||
api.Status = req.Status
|
||||
api.Description = req.Description
|
||||
|
||||
_, err = l.svcCtx.AdminApiModel.Update(l.ctx, nil, api)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR),
|
||||
"更新API失败, err: %v, id: %d", err, req.Id)
|
||||
}
|
||||
|
||||
// 5. 返回结果
|
||||
return &types.AdminUpdateApiResp{Success: true}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user