diff --git a/infrastructure/pg/connection.go b/infrastructure/pg/connection.go index 7b3eeff..737cc8f 100644 --- a/infrastructure/pg/connection.go +++ b/infrastructure/pg/connection.go @@ -41,6 +41,11 @@ func (pg *Postgres) Insert(ctx context.Context, query string, args ...interface{ return id, nil } +func (pg *Postgres) InsertReturningObj(ctx context.Context, query string, args ...interface{}) pgx.Row { + row := pg.db.QueryRow(ctx, query, args...) + return row +} + func (pg *Postgres) Select(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error) { result, err := pg.db.Query(ctx, query, args...) if err != nil { diff --git a/internal/api/http/handlers/files/files.go b/internal/api/http/handlers/files/files.go index 6a44987..5347141 100644 --- a/internal/api/http/handlers/files/files.go +++ b/internal/api/http/handlers/files/files.go @@ -17,7 +17,7 @@ import ( // @Produce json // @Security BearerAuth // @Param new_file formData file false "New file" -// @Success 200 {string} string "GetWidgetDb has been uploaded successfully!" +// @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" @@ -41,7 +41,7 @@ func AddNewFile(fileService model.FileService) echo.HandlerFunc { return echo.NewHTTPError(http.StatusBadRequest, "Can't upload file") } - _, err = fileService.AddNewFile( + fileId, err := fileService.AddNewFile( ctx, *newFile, authData.AccountID, @@ -51,8 +51,13 @@ func AddNewFile(fileService model.FileService) echo.HandlerFunc { slog.Error(err.Error()) return request.JSON(http.StatusInternalServerError, err.Error()) } + fileResponse, err := fileService.GetFileInfo(ctx, fileId) + if err != nil { + slog.Error(err.Error()) + return request.JSON(http.StatusInternalServerError, err.Error()) + } - return request.JSON(http.StatusOK, "") + return request.JSON(http.StatusOK, fileResponse) } } diff --git a/internal/docs/docs.go b/internal/docs/docs.go index 7926a00..de01d12 100644 --- a/internal/docs/docs.go +++ b/internal/docs/docs.go @@ -421,7 +421,7 @@ const docTemplate = `{ "200": { "description": "GetWidgetDb has been uploaded successfully!", "schema": { - "type": "string" + "$ref": "#/definitions/donat-widget_internal_model.DataFile" } }, "400": { diff --git a/internal/docs/swagger.json b/internal/docs/swagger.json index b8401d8..cac5b5a 100644 --- a/internal/docs/swagger.json +++ b/internal/docs/swagger.json @@ -414,7 +414,7 @@ "200": { "description": "GetWidgetDb has been uploaded successfully!", "schema": { - "type": "string" + "$ref": "#/definitions/donat-widget_internal_model.DataFile" } }, "400": { diff --git a/internal/docs/swagger.yaml b/internal/docs/swagger.yaml index 485469d..39a71f6 100644 --- a/internal/docs/swagger.yaml +++ b/internal/docs/swagger.yaml @@ -732,7 +732,7 @@ paths: "200": description: GetWidgetDb has been uploaded successfully! schema: - type: string + $ref: '#/definitions/donat-widget_internal_model.DataFile' "400": description: Bad request schema: diff --git a/internal/model/interfaces.go b/internal/model/interfaces.go index 7f3f53d..cca280e 100644 --- a/internal/model/interfaces.go +++ b/internal/model/interfaces.go @@ -192,6 +192,7 @@ type PaymentClient interface { } type Db interface { Insert(ctx context.Context, query string, args ...interface{}) (any, error) + InsertReturningObj(ctx context.Context, query string, args ...interface{}) pgx.Row Select(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error) SelectOne(ctx context.Context, query string, args ...interface{}) (pgx.Row, error) Delete(ctx context.Context, query string, args ...interface{}) error @@ -216,6 +217,7 @@ type FileRepo interface { ) (string, error) GetByID(ctx context.Context, fileID string) ([]byte, string, error) WidgetsFiles(ctx context.Context, fileType string, streamerID int) ([]*DataFile, error) + GetFileInfoById(ctx context.Context, fileID string) (DataFile, error) } type FileService interface { @@ -229,4 +231,5 @@ type FileService interface { ) (string, error) GetByID(ctx context.Context, fileID string) ([]byte, string, error) WidgetsFiles(ctx context.Context, fileType string, streamerID int) ([]*DataFile, error) + GetFileInfo(ctx context.Context, fileID string) (DataFile, error) } diff --git a/internal/model/sql/query.go b/internal/model/sql/query.go index a74eb13..4906b0e 100644 --- a/internal/model/sql/query.go +++ b/internal/model/sql/query.go @@ -6,7 +6,7 @@ import ( ) var CreateWidget = ` -INSERT INTO widgets (streamer_id, template_id, image, audio, duration, min_amount, max_amount name) +INSERT INTO widgets (streamer_id, template_id, image, audio, duration, min_amount, max_amount, name) VALUES (@streamer_id, @template_id, @image, @audio, @duration, @min_amount, @max_amount, @name) RETURNING id; ` @@ -190,6 +190,9 @@ var GetWidgetByName = `SELECT id FROM widgets WHERE ( var FileById = `SELECT file_name, streamer_id, file_type FROM files WHERE id = (@id);` +var FileInfoById = `SELECT id, file_type, file_name, extension, streamer_id, entity, created_at, size +FROM files WHERE id = (@id);` + var AudioFilesWidgets = ` SELECT * FROM files diff --git a/internal/repository/file/file.go b/internal/repository/file/file.go index 52ca6a6..1bf0990 100644 --- a/internal/repository/file/file.go +++ b/internal/repository/file/file.go @@ -126,3 +126,31 @@ func (fileRepo *RepoFile) WidgetsFiles( return files, nil } + +func (fileRepo *RepoFile) GetFileInfoById( + ctx context.Context, + fileID string, +) (model.DataFile, error) { + args := pgx.NamedArgs{ + "id": fileID, + } + row, err := fileRepo.db.SelectOne(ctx, sql.FileInfoById, args) + if err != nil { + return model.DataFile{}, fmt.Errorf("error retrieving file info: %v", err) + } + var dataFile model.DataFile + err = row.Scan( + &dataFile.ID, + &dataFile.FileType, + &dataFile.FileName, + &dataFile.Extension, + &dataFile.StreamerID, + &dataFile.Entity, + &dataFile.CreatedAt, + &dataFile.Size, + ) + if err != nil { + return model.DataFile{}, fmt.Errorf("error retrieving file info: %v", err) + } + return dataFile, nil +} diff --git a/internal/service/file/file.go b/internal/service/file/file.go index e2274d3..c831b77 100644 --- a/internal/service/file/file.go +++ b/internal/service/file/file.go @@ -59,6 +59,19 @@ func (fileService *ServiceFile) AddNewFile( return fileID, nil } +func (fileService *ServiceFile) GetFileInfo( + ctx context.Context, + fileID string, +) (model.DataFile, error) { + fileData, err := fileService.fileRepo.GetFileInfoById(ctx, fileID) + if err != nil { + return model.DataFile{}, err + } + fileData.FileLink = fileService.storage.DownloadLink(fileData.ID) + + return fileData, nil +} + func (fileService *ServiceFile) GetByID( ctx context.Context, fileID string,