update_api_session_logic.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package chatsession
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "github.com/zeromicro/go-zero/core/errorx"
  6. "wechat-api/ent/chatsession"
  7. "wechat-api/internal/utils/dberrorhandler"
  8. "wechat-api/internal/svc"
  9. "wechat-api/internal/types"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type UpdateApiSessionLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewUpdateApiSessionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateApiSessionLogic {
  18. return &UpdateApiSessionLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx}
  22. }
  23. func (l *UpdateApiSessionLogic) UpdateApiSession(req *types.ChatSessionInfo) (*types.BaseMsgResp, error) {
  24. userId := l.ctx.Value("userId").(uint64)
  25. var id uint64
  26. var name string
  27. if req.Id != nil && *req.Id > 0 {
  28. id = *req.Id
  29. }
  30. if req.Name != nil && *req.Name != "" {
  31. name = *req.Name
  32. }
  33. if id == 0 || name == "" {
  34. return nil, errorx.NewInvalidArgumentError("参数错误")
  35. }
  36. _, err := l.svcCtx.DB.ChatSession.Update().Where(chatsession.ID(id), chatsession.UserID(userId)).SetName(name).Save(l.ctx)
  37. if err != nil {
  38. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  39. }
  40. return &types.BaseMsgResp{
  41. Code: 0,
  42. Msg: errormsg.Success,
  43. }, nil
  44. }