update_user_logic.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package user
  2. import (
  3. "context"
  4. "encoding/json"
  5. "git.i2edu.net/i2/i2-bill-api/internal/svc"
  6. "git.i2edu.net/i2/i2-bill-api/internal/types"
  7. "git.i2edu.net/i2/i2-bill-api/model"
  8. "io/ioutil"
  9. "net/http"
  10. "git.i2edu.net/i2/go-zero/core/logx"
  11. )
  12. type UpdateUserLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) UpdateUserLogic {
  18. return UpdateUserLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx,
  22. }
  23. }
  24. func (l *UpdateUserLogic) UpdateUser(r *http.Request) (*types.Response, error) {
  25. // todo: add your logic here and delete this line
  26. userId := l.svcCtx.GetUserIdByJwt(l.ctx)
  27. user := new(model.UserXorm)
  28. bodyByte, err := ioutil.ReadAll(r.Body)
  29. if err != nil {
  30. logx.Error(err.Error())
  31. return &types.Response{500, err.Error(), nil}, nil
  32. }
  33. err = json.Unmarshal(bodyByte, user)
  34. if err != nil {
  35. logx.Error(err.Error())
  36. return &types.Response{500, err.Error(), nil}, nil
  37. }
  38. user.Id = userId
  39. _, err = l.svcCtx.DB.ID(user.Id).Cols("avatar").Update(user)
  40. if err != nil {
  41. logx.Error(err.Error())
  42. return &types.Response{500, err.Error(), nil}, nil
  43. }
  44. return &types.Response{200, "", nil}, nil
  45. }