chore: port lab 1 solution
This commit is contained in:
@@ -0,0 +1,168 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
"mtusi2026-messenger/internal/httpx"
|
||||
"mtusi2026-messenger/internal/middleware"
|
||||
"mtusi2026-messenger/internal/service"
|
||||
)
|
||||
|
||||
type ChatHandler struct {
|
||||
service *service.ChatService
|
||||
}
|
||||
|
||||
func NewChatHandler(service *service.ChatService) *ChatHandler {
|
||||
return &ChatHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *ChatHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
var req dto.CreateChatRequest
|
||||
if !httpx.DecodeJSONBody(w, r, requestID, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
if !req.Validate() {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid request body",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
chat, err := h.service.Create(userID, req.Title, req.MemberIDs)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrNotChatMember):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"caller must be a member of member_ids",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case errors.Is(err, service.ErrDuplicateMember):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"member_ids contains duplicates",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case errors.Is(err, service.ErrUnknownMember):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"member_ids contains unknown user",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusInternalServerError,
|
||||
dto.ErrorCodeInternalError,
|
||||
"internal server error",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusCreated, dto.ToChatResponse(chat))
|
||||
}
|
||||
|
||||
func (h *ChatHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
chatID, err := uuid.Parse(chi.URLParam(r, "chat_id"))
|
||||
if err != nil {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid chat_id",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
chat, err := h.service.Get(userID, chatID)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrChatNotFound):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusNotFound,
|
||||
dto.ErrorCodeNotFound,
|
||||
"chat not found",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusInternalServerError,
|
||||
dto.ErrorCodeInternalError,
|
||||
"internal server error",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusOK, dto.ToChatResponse(chat))
|
||||
}
|
||||
|
||||
func (h *ChatHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
limit, cursor, ok := httpx.ParsePagination(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
chats, nextCursor, err := h.service.List(userID, limit, cursor)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrInvalidCursor):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid cursor",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusInternalServerError,
|
||||
dto.ErrorCodeInternalError,
|
||||
"internal server error",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
items := make([]dto.ChatResponse, 0, len(chats))
|
||||
for _, c := range chats {
|
||||
items = append(items, dto.ToChatResponse(c))
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusOK, dto.ChatPageResponse{
|
||||
Items: items,
|
||||
NextCursor: nextCursor,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user