add field for crete widget file

This commit is contained in:
harold 2025-03-12 12:29:53 +05:00
parent aa993278b9
commit f46205d2ba
9 changed files with 64 additions and 7 deletions

View File

@ -41,6 +41,11 @@ func (pg *Postgres) Insert(ctx context.Context, query string, args ...interface{
return id, nil 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) { func (pg *Postgres) Select(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error) {
result, err := pg.db.Query(ctx, query, args...) result, err := pg.db.Query(ctx, query, args...)
if err != nil { if err != nil {

View File

@ -17,7 +17,7 @@ import (
// @Produce json // @Produce json
// @Security BearerAuth // @Security BearerAuth
// @Param new_file formData file false "New file" // @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 400 {object} echo.HTTPError "Bad request"
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token" // @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
// @Failure 422 {object} echo.HTTPError "Validation error" // @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") return echo.NewHTTPError(http.StatusBadRequest, "Can't upload file")
} }
_, err = fileService.AddNewFile( fileId, err := fileService.AddNewFile(
ctx, ctx,
*newFile, *newFile,
authData.AccountID, authData.AccountID,
@ -51,8 +51,13 @@ func AddNewFile(fileService model.FileService) echo.HandlerFunc {
slog.Error(err.Error()) slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, 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)
} }
} }

View File

@ -421,7 +421,7 @@ const docTemplate = `{
"200": { "200": {
"description": "GetWidgetDb has been uploaded successfully!", "description": "GetWidgetDb has been uploaded successfully!",
"schema": { "schema": {
"type": "string" "$ref": "#/definitions/donat-widget_internal_model.DataFile"
} }
}, },
"400": { "400": {

View File

@ -414,7 +414,7 @@
"200": { "200": {
"description": "GetWidgetDb has been uploaded successfully!", "description": "GetWidgetDb has been uploaded successfully!",
"schema": { "schema": {
"type": "string" "$ref": "#/definitions/donat-widget_internal_model.DataFile"
} }
}, },
"400": { "400": {

View File

@ -732,7 +732,7 @@ paths:
"200": "200":
description: GetWidgetDb has been uploaded successfully! description: GetWidgetDb has been uploaded successfully!
schema: schema:
type: string $ref: '#/definitions/donat-widget_internal_model.DataFile'
"400": "400":
description: Bad request description: Bad request
schema: schema:

View File

@ -192,6 +192,7 @@ type PaymentClient interface {
} }
type Db interface { type Db interface {
Insert(ctx context.Context, query string, args ...interface{}) (any, error) 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) Select(ctx context.Context, query string, args ...interface{}) (pgx.Rows, error)
SelectOne(ctx context.Context, query string, args ...interface{}) (pgx.Row, error) SelectOne(ctx context.Context, query string, args ...interface{}) (pgx.Row, error)
Delete(ctx context.Context, query string, args ...interface{}) error Delete(ctx context.Context, query string, args ...interface{}) error
@ -216,6 +217,7 @@ type FileRepo interface {
) (string, error) ) (string, error)
GetByID(ctx context.Context, fileID string) ([]byte, string, error) GetByID(ctx context.Context, fileID string) ([]byte, string, error)
WidgetsFiles(ctx context.Context, fileType string, streamerID int) ([]*DataFile, error) WidgetsFiles(ctx context.Context, fileType string, streamerID int) ([]*DataFile, error)
GetFileInfoById(ctx context.Context, fileID string) (DataFile, error)
} }
type FileService interface { type FileService interface {
@ -229,4 +231,5 @@ type FileService interface {
) (string, error) ) (string, error)
GetByID(ctx context.Context, fileID string) ([]byte, string, error) GetByID(ctx context.Context, fileID string) ([]byte, string, error)
WidgetsFiles(ctx context.Context, fileType string, streamerID int) ([]*DataFile, error) WidgetsFiles(ctx context.Context, fileType string, streamerID int) ([]*DataFile, error)
GetFileInfo(ctx context.Context, fileID string) (DataFile, error)
} }

View File

@ -6,7 +6,7 @@ import (
) )
var CreateWidget = ` 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) VALUES (@streamer_id, @template_id, @image, @audio, @duration, @min_amount, @max_amount, @name)
RETURNING id; 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 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 = ` var AudioFilesWidgets = `
SELECT * SELECT *
FROM files FROM files

View File

@ -126,3 +126,31 @@ func (fileRepo *RepoFile) WidgetsFiles(
return files, nil 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
}

View File

@ -59,6 +59,19 @@ func (fileService *ServiceFile) AddNewFile(
return fileID, nil 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( func (fileService *ServiceFile) GetByID(
ctx context.Context, ctx context.Context,
fileID string, fileID string,