forked from mtusi2026/mtusi2026lab4
feat: moved code from lab3
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
type createChatRequest struct {
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
}
|
||||
|
||||
func createChatHandler(chats *services.ChatService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
|
||||
writeError(w, 415, "unsupported_media_type", "content type must be application/json", requestID)
|
||||
return
|
||||
}
|
||||
var request createChatRequest
|
||||
if err := decodeJSONBody(r, &request); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid JSON body", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := chats.Create(r.Context(), userID, request.Title, request.MemberIDs)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, chat)
|
||||
}
|
||||
}
|
||||
|
||||
func getChatHandler(chats *services.ChatService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
chatID := strings.TrimPrefix(r.URL.Path, "/api/v1/chats/")
|
||||
if chatID == "" || strings.Contains(chatID, "/") {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err := uuid.Parse(chatID); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := chats.Get(r.Context(), userID, chatID)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, chat)
|
||||
}
|
||||
}
|
||||
|
||||
func listChatsHandler(chats *services.ChatService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
limit, cursor, ok := pageParams(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
page, err := chats.List(r.Context(), userID, limit, cursor)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, page)
|
||||
}
|
||||
}
|
||||
|
||||
func chatCollectionHandler(chats *services.ChatService) http.HandlerFunc {
|
||||
create := createChatHandler(chats)
|
||||
list := listChatsHandler(chats)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
list(w, r)
|
||||
} else {
|
||||
create(w, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func chatSubresourceHandler(chats *services.ChatService, messages *services.MessageService) http.HandlerFunc {
|
||||
get := getChatHandler(chats)
|
||||
createMessage := createMessageHandler(chats, messages)
|
||||
listMessages := listMessagesHandler(chats, messages)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasSuffix(r.URL.Path, "/messages") {
|
||||
if r.Method == http.MethodGet {
|
||||
listMessages(w, r)
|
||||
} else {
|
||||
createMessage(w, r)
|
||||
}
|
||||
return
|
||||
}
|
||||
get(w, r)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user