forked from mtusi2026/mtusi2026lab2
уровень 5 трек 1
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"mtusi2026/internal/model"
|
||||
"mtusi2026/internal/store"
|
||||
)
|
||||
|
||||
func parseLimit(r *http.Request) (int, bool) {
|
||||
raw := r.URL.Query().Get("limit")
|
||||
if raw == "" {
|
||||
return 50, true
|
||||
}
|
||||
n, err := strconv.Atoi(raw)
|
||||
if err != nil || n < 1 || n > 100 {
|
||||
return 0, false
|
||||
}
|
||||
return n, true
|
||||
}
|
||||
|
||||
func (a *api) handleCreateChat(w http.ResponseWriter, r *http.Request, u model.User, requestID string) {
|
||||
var req model.CreateChatRequest
|
||||
if err := decodeJSONStrict(r, &req); err != nil {
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "malformed request body")
|
||||
return
|
||||
}
|
||||
|
||||
c, err := a.store.CreateChat(r.Context(), u.ID, req.Title, req.MemberIDs)
|
||||
if err != nil {
|
||||
if 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)
|
||||
}
|
||||
|
||||
func (a *api) handleListChats(w http.ResponseWriter, r *http.Request, u model.User, requestID string) {
|
||||
limit, ok := parseLimit(r)
|
||||
if !ok {
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "invalid limit")
|
||||
return
|
||||
}
|
||||
cursorStr := r.URL.Query().Get("cursor")
|
||||
|
||||
items, next, err := a.store.ListChatsForUser(r.Context(), u.ID, cursorStr, limit)
|
||||
if err != nil {
|
||||
if err == store.ErrValidation {
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "invalid cursor")
|
||||
return
|
||||
}
|
||||
writeError(w, http.StatusInternalServerError, requestID, codeInternal, "internal error")
|
||||
return
|
||||
}
|
||||
if items == nil {
|
||||
items = []model.Chat{}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, requestID, model.ChatPage{Items: items, NextCursor: next})
|
||||
}
|
||||
|
||||
func (a *api) handleGetChat(w http.ResponseWriter, r *http.Request, u model.User, requestID string) {
|
||||
chatID := r.PathValue("chat_id")
|
||||
|
||||
if a.chatCache != nil {
|
||||
c, hit, err := a.chatCache.Get(r.Context(), u.ID, chatID)
|
||||
if err == nil && hit {
|
||||
writeJSON(w, http.StatusOK, requestID, c)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
flightKey := u.ID + ":" + chatID
|
||||
|
||||
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 {
|
||||
return c, nil
|
||||
}
|
||||
}
|
||||
|
||||
c, err := a.store.GetChat(r.Context(), chatID, u.ID)
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
|
||||
if a.chatCache != nil {
|
||||
_ = a.chatCache.Set(r.Context(), u.ID, chatID, c)
|
||||
}
|
||||
|
||||
return c, nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
writeError(w, http.StatusNotFound, requestID, codeNotFound, "chat not found")
|
||||
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