Перенес основы lab3 в lab4
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service"
|
||||
)
|
||||
|
||||
type createChatRequest struct {
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
}
|
||||
|
||||
type createMessageRequest struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
func (api *API) chatRoutes(w http.ResponseWriter, r *http.Request) {
|
||||
actor := userFromContext(r.Context())
|
||||
|
||||
if r.URL.Path == "/api/v1/chats" {
|
||||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
var body createChatRequest
|
||||
if !readJSON(w, r, &body, "title", "member_ids") {
|
||||
return
|
||||
}
|
||||
|
||||
chat, err := api.messenger.CreateChat(
|
||||
actor.ID, body.Title, body.MemberIDs,
|
||||
)
|
||||
if err != nil {
|
||||
writeAppError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusCreated, chat)
|
||||
|
||||
case http.MethodGet:
|
||||
limit, cursor, ok := pageParams(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
page, err := api.messenger.ListChats(actor.ID, limit, cursor)
|
||||
if err != nil {
|
||||
writeAppError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, page)
|
||||
|
||||
default:
|
||||
requireMethod(w, r, "GET, POST")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
path := strings.TrimPrefix(r.URL.Path, "/api/v1/chats/")
|
||||
parts := strings.Split(path, "/")
|
||||
|
||||
if len(parts) == 1 && parts[0] != "" {
|
||||
if !requireMethod(w, r, http.MethodGet) {
|
||||
return
|
||||
}
|
||||
|
||||
chat, err := api.messenger.GetChat(actor.ID, parts[0])
|
||||
if err != nil {
|
||||
writeAppError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, chat)
|
||||
return
|
||||
}
|
||||
|
||||
if len(parts) != 2 || parts[0] == "" || parts[1] != "messages" {
|
||||
writeError(w, http.StatusNotFound, "not_found", "Resource not found")
|
||||
return
|
||||
}
|
||||
|
||||
chat, err := api.messenger.GetChat(actor.ID, parts[0])
|
||||
if err != nil {
|
||||
writeAppError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
var body createMessageRequest
|
||||
if !readJSON(w, r, &body, "text") {
|
||||
return
|
||||
}
|
||||
|
||||
message, err := api.messenger.CreateMessage(
|
||||
actor.ID, chat.ID, body.Text,
|
||||
)
|
||||
if err != nil {
|
||||
writeAppError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusCreated, message)
|
||||
|
||||
case http.MethodGet:
|
||||
limit, cursor, ok := pageParams(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
page, err := api.messenger.ListMessages(
|
||||
actor.ID, chat.ID, limit, cursor,
|
||||
)
|
||||
if err != nil {
|
||||
writeAppError(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, page)
|
||||
|
||||
default:
|
||||
requireMethod(w, r, "GET, POST")
|
||||
}
|
||||
}
|
||||
|
||||
func pageParams(
|
||||
w http.ResponseWriter,
|
||||
r *http.Request,
|
||||
) (int, string, bool) {
|
||||
invalid := func() (int, string, bool) {
|
||||
writeAppError(w, service.ErrInvalidRequest)
|
||||
return 0, "", false
|
||||
}
|
||||
|
||||
query := r.URL.Query()
|
||||
limit := 50
|
||||
|
||||
if values, exists := query["limit"]; exists {
|
||||
if len(values) != 1 || values[0] == "" {
|
||||
return invalid()
|
||||
}
|
||||
|
||||
for _, char := range values[0] {
|
||||
if char < '0' || char > '9' {
|
||||
return invalid()
|
||||
}
|
||||
}
|
||||
|
||||
value, err := strconv.Atoi(values[0])
|
||||
if err != nil || value < 1 || value > 100 {
|
||||
return invalid()
|
||||
}
|
||||
|
||||
limit = value
|
||||
}
|
||||
|
||||
cursor := ""
|
||||
|
||||
if values, exists := query["cursor"]; exists {
|
||||
if len(values) != 1 || values[0] == "" {
|
||||
return invalid()
|
||||
}
|
||||
cursor = values[0]
|
||||
}
|
||||
|
||||
return limit, cursor, true
|
||||
}
|
||||
Reference in New Issue
Block a user