Files
ycc-proxy-server/app/main/api/internal/logic/admin_api/admincreateapilogic.go
2025-12-09 18:55:28 +08:00

80 lines
2.2 KiB
Go

package admin_api
import (
"context"
"ycc-server/app/main/api/internal/svc"
"ycc-server/app/main/api/internal/types"
"ycc-server/app/main/model"
"ycc-server/common/xerr"
"github.com/google/uuid"
"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{
Id: uuid.NewString(),
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. 返回结果
_ = result
return &types.AdminCreateApiResp{Id: apiData.Id}, nil
}