Выполнение lab4 на 4
This commit is contained in:
@@ -8,7 +8,10 @@ import (
|
||||
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/service"
|
||||
)
|
||||
|
||||
type persistentAPI struct{ messenger *service.PersistentMessenger }
|
||||
type persistentAPI struct {
|
||||
messenger *service.PersistentMessenger
|
||||
attachments *service.Attachments
|
||||
}
|
||||
|
||||
func (api *persistentAPI) chatRoutes(w http.ResponseWriter, r *http.Request) {
|
||||
actor := userFromContext(r.Context())
|
||||
@@ -53,6 +56,10 @@ func (api *persistentAPI) chatRoutes(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
path := strings.TrimPrefix(r.URL.Path, "/api/v1/chats/")
|
||||
parts := strings.Split(path, "/")
|
||||
if len(parts) == 2 && parts[0] != "" && parts[1] == "attachments" {
|
||||
createAttachment(w, r, api.attachments, parts[0])
|
||||
return
|
||||
}
|
||||
|
||||
if len(parts) == 1 && parts[0] != "" {
|
||||
if !requireMethod(w, r, http.MethodGet) {
|
||||
@@ -83,12 +90,12 @@ func (api *persistentAPI) chatRoutes(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
var body createMessageRequest
|
||||
if !readJSON(w, r, &body, "text") {
|
||||
if !readJSONFields(w, r, &body, []string{"text"}, []string{"attachment_ids"}) {
|
||||
return
|
||||
}
|
||||
|
||||
message, err := api.messenger.CreateMessage(r.Context(),
|
||||
actor.ID, chat.ID, body.Text, r.Header.Get("Idempotency-Key"),
|
||||
actor.ID, chat.ID, body.Text, r.Header.Get("Idempotency-Key"), body.AttachmentIDs...,
|
||||
)
|
||||
if err != nil {
|
||||
writeAppError(w, err)
|
||||
|
||||
Reference in New Issue
Block a user