forked from mtusi2026/mtusi2026lab6
ЛР 6 — БПИ2302 — Нурлыгаянова — на 3
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/api/http/middleware"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/service"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
// Optional extension keeps the mandatory proxy contract unchanged.
|
||||
type directAttachments interface {
|
||||
InitDirect(context.Context, string, string, service.DirectInput) (service.DirectUpload, error)
|
||||
CompleteDirect(context.Context, string, string, string) (domain.Attachment, error)
|
||||
}
|
||||
|
||||
func (h *attachmentsHandler) directInit(c echo.Context) error {
|
||||
svc, ok := h.attachments.(directAttachments)
|
||||
if !ok {
|
||||
return WriteError(c, domain.ErrUnavailable)
|
||||
}
|
||||
var in service.DirectInput
|
||||
if err := decodeJSON(c.Request().Body, &in); err != nil {
|
||||
return WriteError(c, domain.ErrInvalidArgument)
|
||||
}
|
||||
out, err := svc.InitDirect(c.Request().Context(), c.Get(middleware.AuthUserIDKey).(string), c.Param("chat_id"), in)
|
||||
if err != nil {
|
||||
return WriteError(c, err)
|
||||
}
|
||||
c.Response().Header().Set("Cache-Control", "no-store")
|
||||
return c.JSON(http.StatusCreated, out)
|
||||
}
|
||||
|
||||
func (h *attachmentsHandler) directComplete(c echo.Context) error {
|
||||
svc, ok := h.attachments.(directAttachments)
|
||||
if !ok {
|
||||
return WriteError(c, domain.ErrUnavailable)
|
||||
}
|
||||
var in struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
if err := decodeJSON(c.Request().Body, &in); err != nil {
|
||||
return WriteError(c, domain.ErrInvalidArgument)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(c.Request().Context(), transferTimeout)
|
||||
defer cancel()
|
||||
att, err := svc.CompleteDirect(ctx, c.Get(middleware.AuthUserIDKey).(string), c.Param("chat_id"), in.Token)
|
||||
if err != nil {
|
||||
return WriteError(c, err)
|
||||
}
|
||||
return c.JSON(http.StatusCreated, toAttachmentResponse(att))
|
||||
}
|
||||
Reference in New Issue
Block a user