update_api_key_logic.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package api_key
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "wechat-api/internal/utils/dberrorhandler"
  6. "wechat-api/internal/svc"
  7. "wechat-api/internal/types"
  8. "github.com/zeromicro/go-zero/core/logx"
  9. )
  10. type UpdateApiKeyLogic struct {
  11. logx.Logger
  12. ctx context.Context
  13. svcCtx *svc.ServiceContext
  14. }
  15. func NewUpdateApiKeyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateApiKeyLogic {
  16. return &UpdateApiKeyLogic{
  17. Logger: logx.WithContext(ctx),
  18. ctx: ctx,
  19. svcCtx: svcCtx}
  20. }
  21. func (l *UpdateApiKeyLogic) UpdateApiKey(req *types.ApiKeyInfo) (resp *types.BaseMsgResp, err error) {
  22. err = l.svcCtx.DB.ApiKey.UpdateOneID(*req.Id).
  23. SetNotNilTitle(req.Title).
  24. SetNotNilAgentID(req.AgentId).
  25. SetNotNilCustomAgentBase(req.CustomAgentBase).
  26. SetNotNilCustomAgentKey(req.CustomAgentKey).
  27. SetNotNilOpenaiBase(req.OpenaiBase).
  28. SetNotNilOpenaiKey(req.OpenaiKey).
  29. Exec(l.ctx)
  30. if err != nil {
  31. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  32. }
  33. l.svcCtx.Rds.Del(l.ctx, "api_key")
  34. return &types.BaseMsgResp{Msg: errormsg.UpdateSuccess}, nil
  35. }