You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.3 KiB
60 lines
1.3 KiB
// Code scaffolded by goctl. Safe to edit.
|
|
// goctl 1.9.2
|
|
|
|
package ai
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/youruser/base/internal/svc"
|
|
"github.com/youruser/base/internal/types"
|
|
"github.com/youruser/base/model"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type AiApiKeyDeleteLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// 删除API Key
|
|
func NewAiApiKeyDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AiApiKeyDeleteLogic {
|
|
return &AiApiKeyDeleteLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *AiApiKeyDeleteLogic) AiApiKeyDelete(req *types.AIApiKeyDeleteRequest) (resp *types.Response, err error) {
|
|
userId, _ := l.ctx.Value("userId").(int64)
|
|
|
|
// Find existing key
|
|
existing, err := model.AIApiKeyFindOne(l.ctx, l.svcCtx.DB, req.Id)
|
|
if err != nil {
|
|
if err == model.ErrNotFound {
|
|
return nil, fmt.Errorf("API Key不存在")
|
|
}
|
|
return nil, fmt.Errorf("查询API Key失败: %v", err)
|
|
}
|
|
|
|
// Verify ownership
|
|
if existing.UserId != userId && existing.UserId != 0 {
|
|
return nil, fmt.Errorf("无权操作此API Key")
|
|
}
|
|
|
|
if err = model.AIApiKeyDelete(l.ctx, l.svcCtx.DB, req.Id); err != nil {
|
|
return nil, fmt.Errorf("删除API Key失败: %v", err)
|
|
}
|
|
|
|
resp = &types.Response{
|
|
Code: 0,
|
|
Message: "success",
|
|
Success: true,
|
|
}
|
|
|
|
return resp, nil
|
|
}
|
|
|