fix: address lab2 review feedback

This commit is contained in:
Fedoroff
2026-10-27 20:03:08 +03:00
parent c251f2cb7d
commit f08011385a
11 changed files with 280 additions and 82 deletions
+64 -13
View File
@@ -1,9 +1,11 @@
package handler
import (
"encoding/json"
"encoding/base64"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"unicode/utf8"
@@ -42,10 +44,7 @@ func (h *ChatHandler) Create(w http.ResponseWriter, r *http.Request) {
}
var req createChatRequest
decoder := json.NewDecoder(r.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&req); err != nil {
if err := decodeOneObject(r, &req); err != nil {
writeError(w, r, http.StatusBadRequest,
"invalid_request", "invalid request body")
return
@@ -132,17 +131,45 @@ func (h *ChatHandler) List(w http.ResponseWriter, r *http.Request) {
return
}
chats, err := h.store.ListChatsForUser(r.Context(), currentUser.ID)
limit := 50
if values, exists := r.URL.Query()["limit"]; exists {
if len(values) != 1 {
writeError(w, r, 400, "invalid_request", "invalid limit")
return
}
n, err := strconv.Atoi(values[0])
if err != nil || n < 1 || n > 100 {
writeError(w, r, 400, "invalid_request", "limit must be between 1 and 100")
return
}
limit = n
}
var cursorTime *time.Time
var cursorID *uuid.UUID
if values, exists := r.URL.Query()["cursor"]; exists {
if len(values) != 1 || values[0] == "" {
writeError(w, r, 400, "invalid_request", "invalid cursor")
return
}
t, id, err := decodeChatCursor(values[0], currentUser.ID)
if err != nil {
writeError(w, r, 400, "invalid_request", "invalid cursor")
return
}
cursorTime = &t
cursorID = &id
}
chats, more, err := h.store.ListChatsPage(r.Context(), currentUser.ID, limit, cursorTime, cursorID)
if err != nil {
writeError(w, r, http.StatusServiceUnavailable,
"unavailable", "service unavailable")
writeError(w, r, 503, "unavailable", "service unavailable")
return
}
writeJSON(w, http.StatusOK, chatPage{
Items: chats,
NextCursor: nil,
})
var next *string
if more && len(chats) > 0 {
c := encodeChatCursor(currentUser.ID, chats[len(chats)-1])
next = &c
}
writeJSON(w, 200, chatPage{Items: chats, NextCursor: next})
}
func (h *ChatHandler) Get(w http.ResponseWriter, r *http.Request) {
@@ -176,3 +203,27 @@ func (h *ChatHandler) Get(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, chat)
}
func encodeChatCursor(userID uuid.UUID, chat model.Chat) string {
value := fmt.Sprintf("%s|%s|%s", userID, chat.CreatedAt.Format(time.RFC3339Nano), chat.ID)
return base64.RawURLEncoding.EncodeToString([]byte(value))
}
func decodeChatCursor(cursor string, userID uuid.UUID) (time.Time, uuid.UUID, error) {
data, err := base64.RawURLEncoding.DecodeString(cursor)
if err != nil {
return time.Time{}, uuid.Nil, err
}
parts := strings.Split(string(data), "|")
if len(parts) != 3 || parts[0] != userID.String() {
return time.Time{}, uuid.Nil, fmt.Errorf("invalid cursor")
}
t, err := time.Parse(time.RFC3339Nano, parts[1])
if err != nil {
return time.Time{}, uuid.Nil, err
}
id, err := uuid.Parse(parts[2])
if err != nil {
return time.Time{}, uuid.Nil, err
}
return t, id, nil
}