forked from mtusi2026/mtusi2026lab3
внес правки
This commit is contained in:
@@ -12,8 +12,10 @@ import (
|
||||
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 := r.PathValue("chat_id")
|
||||
|
||||
chatID, ok := chatIDParam(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
key := r.Header.Get("Idempotency-Key")
|
||||
if !idemKeyRe.MatchString(key) {
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "missing or invalid Idempotency-Key")
|
||||
@@ -44,21 +46,27 @@ func (a *api) handleCreateMessage(w http.ResponseWriter, r *http.Request, u mode
|
||||
}
|
||||
|
||||
func (a *api) handleListMessages(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
|
||||
}
|
||||
limit, ok := parseLimit(r)
|
||||
if !ok {
|
||||
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.ListMessages(r.Context(), chatID, u.ID, cursorStr, limit)
|
||||
if err != nil {
|
||||
switch err {
|
||||
case store.ErrChatNotFound:
|
||||
switch {
|
||||
case errors.Is(err, store.ErrChatNotFound):
|
||||
writeError(w, http.StatusNotFound, requestID, codeNotFound, "chat not found")
|
||||
case store.ErrValidation:
|
||||
case errors.Is(err, store.ErrValidation):
|
||||
writeError(w, http.StatusBadRequest, requestID, codeInvalidRequest, "invalid cursor")
|
||||
default:
|
||||
writeError(w, http.StatusInternalServerError, requestID, codeInternal, "internal error")
|
||||
|
||||
Reference in New Issue
Block a user