2025-03-05 13:10:55 +05:00

57 lines
1.4 KiB
Go

package files
import (
"context"
"donat-widget/internal/model"
"donat-widget/pkg/validator"
"github.com/labstack/echo/v4"
"log/slog"
"net/http"
)
// AddNewFile godoc
// @Summary Add new File
// @Description Add new File
// @Tags Files
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param new_file formData file false "New file"
// @Success 200 {string} string "Widget has been uploaded successfully!"
// @Failure 400 {object} echo.HTTPError "Bad request"
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
// @Failure 422 {object} echo.HTTPError "Validation error"
// @Router /files [post]
func AddNewFile(fileService model.FileService) echo.HandlerFunc {
return func(request echo.Context) error {
ctx := context.Background()
var body model.CreateWidgetBody
err := validator.ParseAndValidate(&body, request)
authData, err := fileService.CheckToken(request)
if err != nil {
slog.Error(err.Error())
return echo.NewHTTPError(http.StatusUnauthorized, err.Error())
}
newFile, err := request.FormFile("new_file")
if err != nil {
slog.Error(err.Error())
return echo.NewHTTPError(http.StatusBadRequest, "Can't upload file")
}
_, err = fileService.AddNewFile(
ctx,
*newFile,
authData.AccountID,
)
if err != nil {
slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, err.Error())
}
return request.JSON(http.StatusOK, "")
}
}