me_service.go 937 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package service
  2. import (
  3. "context"
  4. "net/http"
  5. "cmr-backend/internal/apperr"
  6. "cmr-backend/internal/store/postgres"
  7. )
  8. type MeService struct {
  9. store *postgres.Store
  10. }
  11. type MeResult struct {
  12. ID string `json:"id"`
  13. PublicID string `json:"publicId"`
  14. Status string `json:"status"`
  15. Nickname *string `json:"nickname,omitempty"`
  16. AvatarURL *string `json:"avatarUrl,omitempty"`
  17. }
  18. func NewMeService(store *postgres.Store) *MeService {
  19. return &MeService{store: store}
  20. }
  21. func (s *MeService) GetMe(ctx context.Context, userID string) (*MeResult, error) {
  22. user, err := s.store.GetUserByID(ctx, s.store.Pool(), userID)
  23. if err != nil {
  24. return nil, err
  25. }
  26. if user == nil {
  27. return nil, apperr.New(http.StatusNotFound, "user_not_found", "user not found")
  28. }
  29. return &MeResult{
  30. ID: user.ID,
  31. PublicID: user.PublicID,
  32. Status: user.Status,
  33. Nickname: user.Nickname,
  34. AvatarURL: user.AvatarURL,
  35. }, nil
  36. }