forked from mtusi2026/mtusi2026lab3
внес правки
This commit is contained in:
+43
-23
@@ -1,20 +1,26 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"mtusi2026/internal/idgen"
|
||||
"mtusi2026/internal/model"
|
||||
"mtusi2026/internal/store"
|
||||
)
|
||||
|
||||
func parseLimit(r *http.Request) (int, bool) {
|
||||
raw := r.URL.Query().Get("limit")
|
||||
if raw == "" {
|
||||
q := r.URL.Query()
|
||||
if !q.Has("limit") {
|
||||
return 50, true
|
||||
}
|
||||
raw := q.Get("limit")
|
||||
n, err := strconv.Atoi(raw)
|
||||
if err != nil || n < 1 || n > 100 {
|
||||
if err != nil || n < 1 || n > 100 || raw != strconv.Itoa(n) {
|
||||
return 0, false
|
||||
}
|
||||
return n, true
|
||||
@@ -26,17 +32,27 @@ func (a *api) handleCreateChat(w http.ResponseWriter, r *http.Request, u model.U
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "malformed request body")
|
||||
return
|
||||
}
|
||||
if !validText(req.Title, 100) || len(req.MemberIDs) == 0 {
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "invalid title or member_ids")
|
||||
return
|
||||
}
|
||||
for i, id := range req.MemberIDs {
|
||||
if !idgen.IsValidUUID(id) {
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "invalid title or member_ids")
|
||||
return
|
||||
}
|
||||
req.MemberIDs[i] = strings.ToLower(id)
|
||||
}
|
||||
|
||||
c, err := a.store.CreateChat(r.Context(), u.ID, req.Title, req.MemberIDs)
|
||||
if err != nil {
|
||||
if err == store.ErrValidation {
|
||||
if errors.Is(err, store.ErrValidation) {
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "invalid title or member_ids")
|
||||
return
|
||||
}
|
||||
writeError(w, http.StatusInternalServerError, requestID, codeInternal, "internal error")
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusCreated, requestID, c)
|
||||
}
|
||||
|
||||
@@ -46,11 +62,15 @@ func (a *api) handleListChats(w http.ResponseWriter, r *http.Request, u model.Us
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "invalid limit")
|
||||
return
|
||||
}
|
||||
cursorStr := r.URL.Query().Get("cursor")
|
||||
cursorStr, ok := parseCursor(r)
|
||||
if !ok {
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "invalid cursor")
|
||||
return
|
||||
}
|
||||
|
||||
items, next, err := a.store.ListChatsForUser(r.Context(), u.ID, cursorStr, limit)
|
||||
if err != nil {
|
||||
if err == store.ErrValidation {
|
||||
if errors.Is(err, store.ErrValidation) {
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "invalid cursor")
|
||||
return
|
||||
}
|
||||
@@ -64,48 +84,48 @@ func (a *api) handleListChats(w http.ResponseWriter, r *http.Request, u model.Us
|
||||
}
|
||||
|
||||
func (a *api) handleGetChat(w http.ResponseWriter, r *http.Request, u model.User, requestID string) {
|
||||
chatID := r.PathValue("chat_id")
|
||||
chatID, ok := chatIDParam(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if a.chatCache != nil {
|
||||
c, hit, err := a.chatCache.Get(r.Context(), u.ID, chatID)
|
||||
if err == nil && hit {
|
||||
if c, hit, err := a.chatCache.Get(r.Context(), u.ID, chatID); err == nil && hit {
|
||||
writeJSON(w, http.StatusOK, requestID, c)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
flightKey := u.ID + ":" + chatID
|
||||
result, err, _ := a.chatFlight.Do(u.ID+":"+chatID, func() (any, error) {
|
||||
ctx, cancel := context.WithTimeout(context.WithoutCancel(r.Context()), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result, err, _ := a.chatFlight.Do(flightKey, func() (any, error) {
|
||||
if a.chatCache != nil {
|
||||
c, hit, err := a.chatCache.Get(r.Context(), u.ID, chatID)
|
||||
if err == nil && hit {
|
||||
if c, hit, err := a.chatCache.Get(ctx, u.ID, chatID); err == nil && hit {
|
||||
return c, nil
|
||||
}
|
||||
}
|
||||
|
||||
c, err := a.store.GetChat(r.Context(), chatID, u.ID)
|
||||
c, err := a.store.GetChat(ctx, chatID, u.ID)
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
|
||||
if a.chatCache != nil {
|
||||
_ = a.chatCache.Set(r.Context(), u.ID, chatID, c)
|
||||
_ = a.chatCache.Set(ctx, u.ID, chatID, c)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
writeError(w, http.StatusNotFound, requestID, codeNotFound, "chat not found")
|
||||
if errors.Is(err, store.ErrChatNotFound) {
|
||||
writeError(w, http.StatusNotFound, requestID, codeNotFound, "chat not found")
|
||||
} else {
|
||||
writeError(w, http.StatusServiceUnavailable, requestID, codeUnavailable, "database unavailable")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
c, ok := result.(model.Chat)
|
||||
if !ok {
|
||||
writeError(w, http.StatusInternalServerError, requestID, codeInternal, "internal error")
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, requestID, c)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user