add checker for already exists widget in user
This commit is contained in:
parent
5287f61c44
commit
8e7336b2ca
@ -49,6 +49,19 @@ func (pg *Postgres) Select(ctx context.Context, query string, args ...interface{
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (pg *Postgres) SelectOne(ctx context.Context, query string, args ...interface{}) (pgx.Row, error) {
|
||||
rows, err := pg.Select(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !rows.Next() {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
func (pg *Postgres) Update(ctx context.Context, query string, args ...interface{}) error {
|
||||
_, err := pg.db.Query(ctx, query, args...)
|
||||
if err != nil {
|
||||
|
@ -21,7 +21,7 @@ import (
|
||||
// @Failure 400 {object} echo.HTTPError "Bad request"
|
||||
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
|
||||
// @Failure 422 {object} echo.HTTPError "Validation error"
|
||||
// @Router /widget [post]
|
||||
// @Router /widgets [post]
|
||||
func CreateWidget(widgetService model.WidgetService) echo.HandlerFunc {
|
||||
return func(request echo.Context) error {
|
||||
ctx := context.Background()
|
||||
|
@ -84,7 +84,7 @@ func IncludeWidgetHandlers(
|
||||
server *echo.Echo,
|
||||
widgetService model.WidgetService,
|
||||
) {
|
||||
server.POST(PREFIX+"/files", widget.CreateWidget(widgetService))
|
||||
server.POST(PREFIX+"/widgets", widget.CreateWidget(widgetService))
|
||||
|
||||
//server.GET(PREFIX+"/html/:streamerID", model.GetWidgetHTML(widgetService))
|
||||
//server.GET(PREFIX+"/info/:widgetID", model.GetWidgetInfo(widgetService))
|
||||
|
@ -508,7 +508,7 @@ const docTemplate = `{
|
||||
}
|
||||
}
|
||||
},
|
||||
"/widget": {
|
||||
"/widgets": {
|
||||
"post": {
|
||||
"security": [
|
||||
{
|
||||
|
@ -501,7 +501,7 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"/widget": {
|
||||
"/widgets": {
|
||||
"post": {
|
||||
"security": [
|
||||
{
|
||||
|
@ -464,7 +464,7 @@ paths:
|
||||
summary: Update donat voice settings.
|
||||
tags:
|
||||
- Donate
|
||||
/widget:
|
||||
/widgets:
|
||||
post:
|
||||
consumes:
|
||||
- application/json
|
||||
|
@ -44,6 +44,7 @@ type WidgetRepo interface {
|
||||
audio string,
|
||||
name string,
|
||||
) (WidgetID, error)
|
||||
CheckWidgetName(ctx context.Context, streamerID int, name string) (bool, error)
|
||||
|
||||
//GetWidgetByStreamerID(ctx context.Context, streamerID StreamerID) ([]*Widget, error)
|
||||
//GetWidgetByID(ctx context.Context, widgetID WidgetID) ([]*Widget, error)
|
||||
@ -128,6 +129,7 @@ type PaymentClient interface {
|
||||
type Db interface {
|
||||
Insert(ctx context.Context, query string, args ...interface{}) (any, error)
|
||||
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
|
||||
Update(ctx context.Context, query string, args ...interface{}) error
|
||||
|
||||
@ -147,11 +149,6 @@ type FileRepo interface {
|
||||
file multipart.FileHeader,
|
||||
extension, fileType string,
|
||||
) (string, error)
|
||||
CheckFileExists(
|
||||
ctx context.Context,
|
||||
streamerID int,
|
||||
fileName, extension, fileType string,
|
||||
) (bool, error)
|
||||
}
|
||||
|
||||
type FileService interface {
|
||||
|
@ -6,8 +6,8 @@ import (
|
||||
)
|
||||
|
||||
var CreateWidget = `
|
||||
INSERT INTO widgets (streamer_id, template_id, background, image, audio, duration, min_amount, name)
|
||||
VALUES (@streamer_id, @template_id, @background, @image, @audio, @duration, @min_amount, @name)
|
||||
INSERT INTO widgets (streamer_id, template_id, image, audio, duration, min_amount, name)
|
||||
VALUES (@streamer_id, @template_id, @image, @audio, @duration, @min_amount, @name)
|
||||
RETURNING id;
|
||||
`
|
||||
|
||||
@ -120,9 +120,9 @@ INSERT INTO files (streamer_id, file_name, file_type, extension)
|
||||
VALUES
|
||||
(@streamer_id, @file_name, @file_type, @extension)
|
||||
RETURNING id;
|
||||
;`
|
||||
`
|
||||
|
||||
var GetFileExists = `SELECT id FROM files WHERE (
|
||||
var GetWidgetByName = `SELECT id FROM widgets WHERE (
|
||||
streamer_id = @streamer_id AND
|
||||
file_name = @file_name AND file_type = @file_type AND extension = @extension
|
||||
;`
|
||||
name = @name);
|
||||
`
|
||||
|
@ -5,7 +5,6 @@ import (
|
||||
"donat-widget/internal/model"
|
||||
"donat-widget/internal/model/sql"
|
||||
"fmt"
|
||||
"github.com/georgysavva/scany/v2/pgxscan"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"mime/multipart"
|
||||
@ -57,30 +56,3 @@ func (fileRepo *RepoFile) AddNew(
|
||||
|
||||
return fileID, nil
|
||||
}
|
||||
|
||||
func (fileRepo *RepoFile) CheckFileExists(
|
||||
ctx context.Context,
|
||||
streamerID int,
|
||||
fileName, extension, fileType string,
|
||||
) (bool, error) {
|
||||
args := pgx.NamedArgs{
|
||||
"streamer_id": streamerID,
|
||||
"file_name": fileName,
|
||||
"file_type": fileType,
|
||||
"extension": extension,
|
||||
}
|
||||
|
||||
rows, err := fileRepo.db.Select(ctx, sql.GetFileExists, args)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
var fileId [][16]uint8
|
||||
err = pgxscan.ScanOne(fileId, rows)
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return true, nil
|
||||
|
||||
}
|
||||
|
@ -50,7 +50,29 @@ func (widgetRepo *RepoWidget) CreateWidget(
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return model.WidgetID(widgetID.(int)), nil
|
||||
return model.WidgetID(widgetID.(int32)), nil
|
||||
}
|
||||
|
||||
func (widgetRepo *RepoWidget) CheckWidgetName(
|
||||
ctx context.Context,
|
||||
streamerID int,
|
||||
name string,
|
||||
) (bool, error) {
|
||||
args := pgx.NamedArgs{
|
||||
"streamer_id": streamerID,
|
||||
"name": name,
|
||||
}
|
||||
row, err := widgetRepo.db.SelectOne(ctx, sql.GetWidgetByName, args)
|
||||
if err != nil {
|
||||
slog.Error(err.Error())
|
||||
return false, err
|
||||
}
|
||||
|
||||
if row == nil {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"donat-widget/internal/model"
|
||||
"donat-widget/internal/model/api"
|
||||
"fmt"
|
||||
"github.com/labstack/echo/v4"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
@ -38,6 +39,16 @@ func (widgetService *ServiceWidget) CreateWidget(
|
||||
audio string,
|
||||
name string,
|
||||
) (model.WidgetID, error) {
|
||||
exists, err := widgetService.widgetRepo.CheckWidgetName(ctx, streamerID, name)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
fmt.Println(exists)
|
||||
if exists == true {
|
||||
slog.Error("Widget with name %s already exists", name)
|
||||
return 0, fmt.Errorf("widget with name %s already exists", name)
|
||||
}
|
||||
|
||||
widgetID, err := widgetService.widgetRepo.CreateWidget(
|
||||
ctx,
|
||||
streamerID,
|
||||
|
Loading…
x
Reference in New Issue
Block a user