profile_handler.go 836 B

12345678910111213141516171819202122232425262728293031323334
  1. package handlers
  2. import (
  3. "net/http"
  4. "cmr-backend/internal/apperr"
  5. "cmr-backend/internal/httpapi/middleware"
  6. "cmr-backend/internal/httpx"
  7. "cmr-backend/internal/service"
  8. )
  9. type ProfileHandler struct {
  10. profileService *service.ProfileService
  11. }
  12. func NewProfileHandler(profileService *service.ProfileService) *ProfileHandler {
  13. return &ProfileHandler{profileService: profileService}
  14. }
  15. func (h *ProfileHandler) Get(w http.ResponseWriter, r *http.Request) {
  16. auth := middleware.GetAuthContext(r.Context())
  17. if auth == nil {
  18. httpx.WriteError(w, apperr.New(http.StatusUnauthorized, "unauthorized", "missing auth context"))
  19. return
  20. }
  21. result, err := h.profileService.GetProfile(r.Context(), auth.UserID)
  22. if err != nil {
  23. httpx.WriteError(w, err)
  24. return
  25. }
  26. httpx.WriteJSON(w, http.StatusOK, map[string]any{"data": result})
  27. }