add new entity for upload file

This commit is contained in:
harold 2025-04-06 10:41:39 +05:00
parent 05e85e077d
commit 8b0f0e7648
4 changed files with 88 additions and 39 deletions

View File

@ -9,47 +9,61 @@ import (
)
// 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 {object} model.DataFile "GetWidgetDb 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]
// @Summary Add new File
// @Description Add new File. The entity type defaults to 'widget' if not specified.
// @Tags Files
// @Accept multipart/form-data
// @Produce json
// @Security BearerAuth
// @Param new_file formData file true "New file to upload"
// @Param entity formData string false "Optional entity type (defaults to 'widget')"
// @Success 200 {object} model.DataFile "File has been uploaded successfully!"
// @Failure 400 {object} echo.HTTPError "Bad request (e.g., missing file, invalid form data)"
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
// @Failure 422 {object} echo.HTTPError "Validation error (specific cases, might overlap with 400/500)"
// @Failure 500 {object} echo.HTTPError "Internal server error (e.g., failed to save file, DB error)"
// @Router /files [post]
func AddNewFile(fileService model.FileService) echo.HandlerFunc {
return func(request echo.Context) error {
ctx := context.Background()
authData, err := fileService.CheckToken(request)
if err != nil {
slog.Error(err.Error())
return echo.NewHTTPError(http.StatusUnauthorized, err.Error())
slog.Error("Authentication failed", "error", err)
return echo.NewHTTPError(http.StatusUnauthorized, "Unauthorized: "+err.Error())
}
entity := request.FormValue("entity")
if entity == "" {
entity = "widget"
slog.Info("Entity parameter not provided, defaulting to 'widget'")
}
newFile, err := request.FormFile("new_file")
if err != nil {
slog.Error(err.Error())
return echo.NewHTTPError(http.StatusBadRequest, "Can't upload file")
if err == http.ErrMissingFile {
slog.Error("Missing 'new_file' in form data", "error", err)
return echo.NewHTTPError(http.StatusBadRequest, "Required form field 'new_file' is missing")
}
slog.Error("Failed to get file from form", "error", err)
return echo.NewHTTPError(http.StatusBadRequest, "Cannot process uploaded file: "+err.Error())
}
fileId, err := fileService.AddNewFile(
ctx,
*newFile,
authData.AccountID,
"widget",
entity,
)
if err != nil {
slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, err.Error())
slog.Error("Failed to add new file via service", "entity", entity, "accountID", authData.AccountID, "error", err)
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to save file: "+err.Error())
}
slog.Info("File added successfully", "fileId", fileId, "entity", entity, "accountID", authData.AccountID)
fileResponse, err := fileService.GetFileInfo(ctx, fileId)
if err != nil {
slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, err.Error())
slog.Error("Failed to get file info after adding", "fileId", fileId, "error", err)
return echo.NewHTTPError(http.StatusInternalServerError, "File saved, but failed to retrieve info: "+err.Error())
}
return request.JSON(http.StatusOK, fileResponse)

View File

@ -484,9 +484,9 @@ const docTemplate = `{
"BearerAuth": []
}
],
"description": "Add new File",
"description": "Add new File. The entity type defaults to 'widget' if not specified.",
"consumes": [
"application/json"
"multipart/form-data"
],
"produces": [
"application/json"
@ -498,20 +498,27 @@ const docTemplate = `{
"parameters": [
{
"type": "file",
"description": "New file",
"description": "New file to upload",
"name": "new_file",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "Optional entity type (defaults to 'widget')",
"name": "entity",
"in": "formData"
}
],
"responses": {
"200": {
"description": "GetWidgetDb has been uploaded successfully!",
"description": "File has been uploaded successfully!",
"schema": {
"$ref": "#/definitions/donat-widget_internal_model.DataFile"
}
},
"400": {
"description": "Bad request",
"description": "Bad request (e.g., missing file, invalid form data)",
"schema": {
"$ref": "#/definitions/echo.HTTPError"
}
@ -523,7 +530,13 @@ const docTemplate = `{
}
},
"422": {
"description": "Validation error",
"description": "Validation error (specific cases, might overlap with 400/500)",
"schema": {
"$ref": "#/definitions/echo.HTTPError"
}
},
"500": {
"description": "Internal server error (e.g., failed to save file, DB error)",
"schema": {
"$ref": "#/definitions/echo.HTTPError"
}

View File

@ -477,9 +477,9 @@
"BearerAuth": []
}
],
"description": "Add new File",
"description": "Add new File. The entity type defaults to 'widget' if not specified.",
"consumes": [
"application/json"
"multipart/form-data"
],
"produces": [
"application/json"
@ -491,20 +491,27 @@
"parameters": [
{
"type": "file",
"description": "New file",
"description": "New file to upload",
"name": "new_file",
"in": "formData",
"required": true
},
{
"type": "string",
"description": "Optional entity type (defaults to 'widget')",
"name": "entity",
"in": "formData"
}
],
"responses": {
"200": {
"description": "GetWidgetDb has been uploaded successfully!",
"description": "File has been uploaded successfully!",
"schema": {
"$ref": "#/definitions/donat-widget_internal_model.DataFile"
}
},
"400": {
"description": "Bad request",
"description": "Bad request (e.g., missing file, invalid form data)",
"schema": {
"$ref": "#/definitions/echo.HTTPError"
}
@ -516,7 +523,13 @@
}
},
"422": {
"description": "Validation error",
"description": "Validation error (specific cases, might overlap with 400/500)",
"schema": {
"$ref": "#/definitions/echo.HTTPError"
}
},
"500": {
"description": "Internal server error (e.g., failed to save file, DB error)",
"schema": {
"$ref": "#/definitions/echo.HTTPError"
}

View File

@ -829,22 +829,27 @@ paths:
/files:
post:
consumes:
- application/json
description: Add new File
- multipart/form-data
description: Add new File. The entity type defaults to 'widget' if not specified.
parameters:
- description: New file
- description: New file to upload
in: formData
name: new_file
required: true
type: file
- description: Optional entity type (defaults to 'widget')
in: formData
name: entity
type: string
produces:
- application/json
responses:
"200":
description: GetWidgetDb has been uploaded successfully!
description: File has been uploaded successfully!
schema:
$ref: '#/definitions/donat-widget_internal_model.DataFile'
"400":
description: Bad request
description: Bad request (e.g., missing file, invalid form data)
schema:
$ref: '#/definitions/echo.HTTPError'
"401":
@ -852,7 +857,11 @@ paths:
schema:
$ref: '#/definitions/echo.HTTPError'
"422":
description: Validation error
description: Validation error (specific cases, might overlap with 400/500)
schema:
$ref: '#/definitions/echo.HTTPError'
"500":
description: Internal server error (e.g., failed to save file, DB error)
schema:
$ref: '#/definitions/echo.HTTPError'
security: