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
|
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 {
|
func (pg *Postgres) Update(ctx context.Context, query string, args ...interface{}) error {
|
||||||
_, err := pg.db.Query(ctx, query, args...)
|
_, err := pg.db.Query(ctx, query, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -21,7 +21,7 @@ import (
|
|||||||
// @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"
|
||||||
// @Router /widget [post]
|
// @Router /widgets [post]
|
||||||
func CreateWidget(widgetService model.WidgetService) echo.HandlerFunc {
|
func CreateWidget(widgetService model.WidgetService) echo.HandlerFunc {
|
||||||
return func(request echo.Context) error {
|
return func(request echo.Context) error {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
@ -84,7 +84,7 @@ func IncludeWidgetHandlers(
|
|||||||
server *echo.Echo,
|
server *echo.Echo,
|
||||||
widgetService model.WidgetService,
|
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+"/html/:streamerID", model.GetWidgetHTML(widgetService))
|
||||||
//server.GET(PREFIX+"/info/:widgetID", model.GetWidgetInfo(widgetService))
|
//server.GET(PREFIX+"/info/:widgetID", model.GetWidgetInfo(widgetService))
|
||||||
|
@ -508,7 +508,7 @@ const docTemplate = `{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/widget": {
|
"/widgets": {
|
||||||
"post": {
|
"post": {
|
||||||
"security": [
|
"security": [
|
||||||
{
|
{
|
||||||
|
@ -501,7 +501,7 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"/widget": {
|
"/widgets": {
|
||||||
"post": {
|
"post": {
|
||||||
"security": [
|
"security": [
|
||||||
{
|
{
|
||||||
|
@ -464,7 +464,7 @@ paths:
|
|||||||
summary: Update donat voice settings.
|
summary: Update donat voice settings.
|
||||||
tags:
|
tags:
|
||||||
- Donate
|
- Donate
|
||||||
/widget:
|
/widgets:
|
||||||
post:
|
post:
|
||||||
consumes:
|
consumes:
|
||||||
- application/json
|
- application/json
|
||||||
|
@ -44,6 +44,7 @@ type WidgetRepo interface {
|
|||||||
audio string,
|
audio string,
|
||||||
name string,
|
name string,
|
||||||
) (WidgetID, error)
|
) (WidgetID, error)
|
||||||
|
CheckWidgetName(ctx context.Context, streamerID int, name string) (bool, error)
|
||||||
|
|
||||||
//GetWidgetByStreamerID(ctx context.Context, streamerID StreamerID) ([]*Widget, error)
|
//GetWidgetByStreamerID(ctx context.Context, streamerID StreamerID) ([]*Widget, error)
|
||||||
//GetWidgetByID(ctx context.Context, widgetID WidgetID) ([]*Widget, error)
|
//GetWidgetByID(ctx context.Context, widgetID WidgetID) ([]*Widget, error)
|
||||||
@ -128,6 +129,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)
|
||||||
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)
|
||||||
Delete(ctx context.Context, query string, args ...interface{}) error
|
Delete(ctx context.Context, query string, args ...interface{}) error
|
||||||
Update(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,
|
file multipart.FileHeader,
|
||||||
extension, fileType string,
|
extension, fileType string,
|
||||||
) (string, error)
|
) (string, error)
|
||||||
CheckFileExists(
|
|
||||||
ctx context.Context,
|
|
||||||
streamerID int,
|
|
||||||
fileName, extension, fileType string,
|
|
||||||
) (bool, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type FileService interface {
|
type FileService interface {
|
||||||
|
@ -6,8 +6,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var CreateWidget = `
|
var CreateWidget = `
|
||||||
INSERT INTO widgets (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, @background, @image, @audio, @duration, @min_amount, @name)
|
VALUES (@streamer_id, @template_id, @image, @audio, @duration, @min_amount, @name)
|
||||||
RETURNING id;
|
RETURNING id;
|
||||||
`
|
`
|
||||||
|
|
||||||
@ -120,9 +120,9 @@ INSERT INTO files (streamer_id, file_name, file_type, extension)
|
|||||||
VALUES
|
VALUES
|
||||||
(@streamer_id, @file_name, @file_type, @extension)
|
(@streamer_id, @file_name, @file_type, @extension)
|
||||||
RETURNING id;
|
RETURNING id;
|
||||||
;`
|
`
|
||||||
|
|
||||||
var GetFileExists = `SELECT id FROM files WHERE (
|
var GetWidgetByName = `SELECT id FROM widgets WHERE (
|
||||||
streamer_id = @streamer_id AND
|
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"
|
||||||
"donat-widget/internal/model/sql"
|
"donat-widget/internal/model/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/georgysavva/scany/v2/pgxscan"
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
@ -57,30 +56,3 @@ func (fileRepo *RepoFile) AddNew(
|
|||||||
|
|
||||||
return fileID, nil
|
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 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"
|
"context"
|
||||||
"donat-widget/internal/model"
|
"donat-widget/internal/model"
|
||||||
"donat-widget/internal/model/api"
|
"donat-widget/internal/model/api"
|
||||||
|
"fmt"
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -38,6 +39,16 @@ func (widgetService *ServiceWidget) CreateWidget(
|
|||||||
audio string,
|
audio string,
|
||||||
name string,
|
name string,
|
||||||
) (model.WidgetID, error) {
|
) (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(
|
widgetID, err := widgetService.widgetRepo.CreateWidget(
|
||||||
ctx,
|
ctx,
|
||||||
streamerID,
|
streamerID,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user