| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- // Code scaffolded by goctl. Safe to edit.
- // goctl 1.10.2
- package application
- import (
- "context"
- "database/sql"
- "strings"
- "udeServer/internal/apierr"
- "udeServer/internal/audit"
- "udeServer/internal/log"
- "udeServer/internal/model"
- "udeServer/internal/svc"
- "udeServer/internal/types"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type AddApplicationLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- // 添加应用凭证
- func NewAddApplicationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddApplicationLogic {
- return &AddApplicationLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx,
- }
- }
- func (l *AddApplicationLogic) AddApplication(req *types.ApplicationCredentialsAddReq) (*types.ApplicationCredentialsAddResp, error) {
- if strings.TrimSpace(req.Name) == "" {
- return nil, apierr.BadRequest("应用名称不能为空")
- }
- // 状态缺省启用;只接受 1(启用)/2(禁用)
- if req.Status == 0 {
- req.Status = 1
- }
- if req.Status != 1 && req.Status != 2 {
- return nil, apierr.BadRequest("应用状态只允许 1(启用) 或 2(禁用)")
- }
- exists, err := l.svcCtx.ApplicationCredModel.ExistsByName(l.ctx, req.Name)
- if err != nil {
- log.RecordError(l.ctx, err)
- return nil, apierr.New(500, "添加应用凭证失败")
- }
- if exists {
- return nil, apierr.BadRequest("已存在同名[%s]的应用凭证", req.Name)
- }
- credIden := audit.NewUUID()
- data := &model.TApplicationCredentials{
- CredIden: credIden,
- Name: req.Name,
- ApiKey: GenerateApiKey(),
- ApiSecret: GenerateApiSecret(),
- HmacAlgo: HmacAlgo,
- Status: int64(req.Status),
- Description: sql.NullString{
- String: req.Description,
- Valid: req.Description != "",
- },
- CreatedBy: operatorName,
- UpdatedBy: operatorName,
- }
- if _, err := l.svcCtx.ApplicationCredModel.Insert(l.ctx, data); err != nil {
- log.RecordError(l.ctx, err)
- return nil, apierr.New(500, "添加应用凭证失败")
- }
- return &types.ApplicationCredentialsAddResp{Iden: credIden}, nil
- }
|