forked from mtusi2026/mtusi2026lab3
37 lines
1.1 KiB
Go
37 lines
1.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"mtusi2026lab3/internal/domain"
|
|
"mtusi2026lab3/internal/transport/http/dto"
|
|
"mtusi2026lab3/internal/transport/http/middleware"
|
|
"mtusi2026lab3/internal/transport/http/response"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func (h *Handler) GetChat(w http.ResponseWriter, r *http.Request) {
|
|
requestID := middleware.GetRequestID(r.Context())
|
|
currentUser := middleware.GetCurrentUser(r.Context())
|
|
chatID := r.PathValue("chat_id")
|
|
if _, err := uuid.Parse(chatID); err != nil {
|
|
response.WriteError(w, http.StatusBadRequest, response.CodeInvalidRequest,
|
|
"invalid chat_id uuid", requestID)
|
|
return
|
|
}
|
|
|
|
chat, err := h.service.GetChat(r.Context(), currentUser.ID, chatID)
|
|
if errors.Is(err, domain.ErrNotFound) {
|
|
response.WriteError(w, http.StatusNotFound, response.CodeNotFound, "chat not found", requestID)
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.WriteError(w, http.StatusServiceUnavailable, response.CodeUnavailable,
|
|
"service is temporarily unavailable", requestID)
|
|
return
|
|
}
|
|
response.WriteJSON(w, http.StatusOK, dto.ChatFromDomain(chat), requestID)
|
|
}
|