forked from mtusi2026/mtusi2026lab3
132 lines
3.6 KiB
Go
132 lines
3.6 KiB
Go
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) {
|
|
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 || raw != strconv.Itoa(n) {
|
|
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
|
|
}
|
|
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 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)
|
|
}
|
|
|
|
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, 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 errors.Is(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, ok := chatIDParam(w, r, requestID)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
if a.chatCache != nil {
|
|
if c, hit, err := a.chatCache.Get(r.Context(), u.ID, chatID); err == nil && hit {
|
|
writeJSON(w, http.StatusOK, requestID, c)
|
|
return
|
|
}
|
|
}
|
|
|
|
result, err, _ := a.chatFlight.Do(u.ID+":"+chatID, func() (any, error) {
|
|
ctx, cancel := context.WithTimeout(context.WithoutCancel(r.Context()), 5*time.Second)
|
|
defer cancel()
|
|
|
|
if a.chatCache != nil {
|
|
if c, hit, err := a.chatCache.Get(ctx, u.ID, chatID); err == nil && hit {
|
|
return c, nil
|
|
}
|
|
}
|
|
c, err := a.store.GetChat(ctx, chatID, u.ID)
|
|
if err != nil {
|
|
return model.Chat{}, err
|
|
}
|
|
if a.chatCache != nil {
|
|
_ = a.chatCache.Set(ctx, u.ID, chatID, c)
|
|
}
|
|
return c, nil
|
|
})
|
|
if err != nil {
|
|
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)
|
|
}
|