forked from mtusi2026/mtusi2026lab3
Implement lab 3 level 4 failover and idempotency
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"highload/internal/service"
|
||||
"highload/internal/transport/http/httputils"
|
||||
"highload/internal/transport/http/middleware"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ChatHandler struct {
|
||||
chatService *service.ChatService
|
||||
}
|
||||
|
||||
func NewChatHandler(chatService *service.ChatService) *ChatHandler {
|
||||
return &ChatHandler{chatService: chatService}
|
||||
}
|
||||
|
||||
func (h *ChatHandler) CreateChat(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
requestID := middleware.RequestIDFromContext(r.Context())
|
||||
userID := middleware.UserIDFromContext(r.Context())
|
||||
|
||||
req, code := httputils.Decode[chatCreateRequest](r)
|
||||
|
||||
if code != http.StatusOK {
|
||||
if code == http.StatusUnsupportedMediaType {
|
||||
httputils.WriteError(w, code, httputils.ErrorDetails{Code: "unsupported_media_type", Message: "Неверный media type", RequestID: requestID})
|
||||
return
|
||||
}
|
||||
httputils.WriteError(w, code, httputils.ErrorDetails{Code: "invalid_request", Message: "Неверный запрос", RequestID: requestID})
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.chatService.Create(r.Context(), req.Title, req.MemberIDs, userID)
|
||||
|
||||
if err != nil {
|
||||
httputils.WriteServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
|
||||
response := chatResponse{
|
||||
Title: result.Title,
|
||||
MemberIDs: result.MemberIDs,
|
||||
ID: result.ID,
|
||||
CreatedAt: httputils.FormatTime(result.CreatedAt),
|
||||
}
|
||||
|
||||
_ = httputils.WriteJSON[chatResponse](w, http.StatusCreated, response)
|
||||
|
||||
}
|
||||
|
||||
func (h *ChatHandler) GetChatsByUserID(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.RequestIDFromContext(r.Context())
|
||||
|
||||
userID := middleware.UserIDFromContext(r.Context())
|
||||
|
||||
limit, cursor, ok := httputils.GetLimitAndCursor(r)
|
||||
|
||||
if !ok {
|
||||
httputils.WriteError(w, http.StatusBadRequest, httputils.ErrorDetails{Code: "invalid_request", Message: "Неверный запрос", RequestID: requestID})
|
||||
return
|
||||
}
|
||||
|
||||
chats, nextCursor, err := h.chatService.GetChatsByUserID(r.Context(), userID, limit, cursor)
|
||||
|
||||
if err != nil {
|
||||
httputils.WriteServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
|
||||
chatResponses := make([]chatResponse, len(chats))
|
||||
for index, chat := range chats {
|
||||
chatResponses[index] = *convertChatModelToResponse(chat)
|
||||
}
|
||||
|
||||
response := chatGetByUserIDResponse{
|
||||
Items: chatResponses,
|
||||
NextCursor: nextCursor,
|
||||
}
|
||||
|
||||
_ = httputils.WriteJSON[chatGetByUserIDResponse](w, http.StatusOK, response)
|
||||
|
||||
}
|
||||
|
||||
func (h *ChatHandler) GetChatByChatID(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.RequestIDFromContext(r.Context())
|
||||
|
||||
userID := middleware.UserIDFromContext(r.Context())
|
||||
|
||||
chatID := r.PathValue("chat_id")
|
||||
|
||||
chat, err := h.chatService.GetChatByID(r.Context(), chatID, userID)
|
||||
|
||||
if err != nil {
|
||||
httputils.WriteServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
|
||||
response := convertChatModelToResponse(chat)
|
||||
_ = httputils.WriteJSON[chatResponse](w, http.StatusOK, *response)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user