update_user_logic.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. defer r.Body.Close()
  30. if err != nil {
  31. logx.Error(err.Error())
  32. return &types.Response{500, err.Error(), nil}, nil
  33. }
  34. err = json.Unmarshal(bodyByte, user)
  35. if err != nil {
  36. logx.Error(err.Error())
  37. return &types.Response{500, err.Error(), nil}, nil
  38. }
  39. user.Id = userId
  40. _, err = l.svcCtx.DB.ID(user.Id).Cols("avatar").Update(user)
  41. if err != nil {
  42. logx.Error(err.Error())
  43. return &types.Response{500, err.Error(), nil}, nil
  44. }
  45. return &types.Response{200, "", nil}, nil
  46. }