forked from mtusi2026/mtusi2026lab5
lab5 na4
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"mtusi2026/internal/idgen"
|
||||
"mtusi2026/internal/model"
|
||||
"mtusi2026/internal/store"
|
||||
)
|
||||
|
||||
var idemKeyRe = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
|
||||
func (a *api) handleCreateMessage(w http.ResponseWriter, r *http.Request, u model.User, requestID string) {
|
||||
chatID, ok := uuidParam(w, r, requestID, "chat_id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
key := r.Header.Get("Idempotency-Key")
|
||||
if !idemKeyRe.MatchString(key) {
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "missing or invalid Idempotency-Key")
|
||||
return
|
||||
}
|
||||
|
||||
var req model.CreateMessageRequest
|
||||
if err := decodeJSONStrict(r, &req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "malformed request body")
|
||||
return
|
||||
}
|
||||
|
||||
ids := make([]string, 0, len(req.AttachmentIDs))
|
||||
seen := map[string]bool{}
|
||||
for _, id := range req.AttachmentIDs {
|
||||
id = strings.ToLower(id)
|
||||
if !idgen.IsValidUUID(id) || seen[id] {
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "invalid attachment_ids")
|
||||
return
|
||||
}
|
||||
seen[id] = true
|
||||
ids = append(ids, id)
|
||||
}
|
||||
if len(ids) > 10 || !store.ValidMessageBody(req.Text, len(ids) > 0) {
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "invalid text or attachment_ids")
|
||||
return
|
||||
}
|
||||
|
||||
m, err := a.store.CreateMessage(r.Context(), chatID, u.ID, key, req.Text, ids)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, store.ErrChatNotFound), errors.Is(err, store.ErrAttachmentNotFound):
|
||||
writeError(w, http.StatusNotFound, requestID, codeNotFound, "chat or attachment not found")
|
||||
case errors.Is(err, store.ErrAttachmentFailed):
|
||||
writeError(w, http.StatusConflict, requestID, codeConflict, "attachment processing failed")
|
||||
case errors.Is(err, store.ErrIdempotencyConflict):
|
||||
writeError(w, http.StatusConflict, requestID, codeConflict, "Idempotency-Key was used with a different request body")
|
||||
case errors.Is(err, store.ErrValidation):
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "invalid text")
|
||||
default:
|
||||
writeError(w, http.StatusInternalServerError, requestID, codeInternal, "internal error")
|
||||
}
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, requestID, m)
|
||||
}
|
||||
|
||||
func (a *api) handleListMessages(w http.ResponseWriter, r *http.Request, u model.User, requestID string) {
|
||||
chatID, ok := chatIDParam(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
limit, ok := parseLimit(r)
|
||||
if !ok {
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "invalid limit")
|
||||
return
|
||||
}
|
||||
cursorStr, ok := parseCursor(r)
|
||||
if !ok {
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "invalid cursor")
|
||||
return
|
||||
}
|
||||
|
||||
items, next, err := a.store.ListMessages(r.Context(), chatID, u.ID, cursorStr, limit)
|
||||
if err != nil {
|
||||
switch {
|
||||
case errors.Is(err, store.ErrChatNotFound):
|
||||
writeError(w, http.StatusNotFound, requestID, codeNotFound, "chat not found")
|
||||
case errors.Is(err, store.ErrValidation):
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "invalid cursor")
|
||||
default:
|
||||
writeError(w, http.StatusInternalServerError, requestID, codeInternal, "internal error")
|
||||
}
|
||||
return
|
||||
}
|
||||
if items == nil {
|
||||
items = []model.Message{}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, requestID, model.MessagePage{Items: items, NextCursor: next})
|
||||
}
|
||||
Reference in New Issue
Block a user