129 lines
2.6 KiB
Go
129 lines
2.6 KiB
Go
package widget
|
|
|
|
import (
|
|
"context"
|
|
"donat-widget/internal/model"
|
|
"errors"
|
|
"github.com/georgysavva/scany/v2/pgxscan"
|
|
"github.com/jackc/pgx/v5"
|
|
"log/slog"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
func New(
|
|
db model.Db,
|
|
) *RepoWidget {
|
|
return &RepoWidget{db}
|
|
}
|
|
|
|
type RepoWidget struct {
|
|
model.Db
|
|
}
|
|
|
|
func (widgetRepo *RepoWidget) CreateWidget(
|
|
ctx context.Context,
|
|
streamerID model.StreamerID,
|
|
templateID model.TemplateID,
|
|
) (model.WidgetID, error) {
|
|
query := `
|
|
INSERT INTO widgets (streamer_id, template_id)
|
|
VALUES (@streamer_id, @template_id);
|
|
`
|
|
|
|
args := pgx.NamedArgs{
|
|
"streamer_id": streamerID,
|
|
"template_id": templateID,
|
|
}
|
|
|
|
_, err := widgetRepo.Query(ctx, query, args)
|
|
if err != nil {
|
|
slog.Error("widgetRepo.Query: " + err.Error())
|
|
return 0, err
|
|
}
|
|
|
|
widgetID := 9
|
|
return model.WidgetID(widgetID), nil
|
|
}
|
|
|
|
func (widgetRepo *RepoWidget) GetWidget(
|
|
ctx context.Context,
|
|
widgetID model.WidgetID,
|
|
) (*model.Widget, error) {
|
|
query := `
|
|
SELECT * FROM widgets
|
|
WHERE id = (@id);
|
|
`
|
|
args := pgx.NamedArgs{
|
|
"id": widgetID,
|
|
}
|
|
|
|
var widgets []*model.Widget
|
|
rows, err := widgetRepo.Query(ctx, query, args)
|
|
if err != nil {
|
|
slog.Error("widgetRepo.Query: " + err.Error())
|
|
return nil, err
|
|
}
|
|
|
|
err = pgxscan.ScanAll(&widgets, rows)
|
|
if err != nil {
|
|
slog.Error(err.Error())
|
|
return nil, err
|
|
}
|
|
|
|
if len(widgets) == 0 {
|
|
slog.Error("Widget not found")
|
|
return nil, errors.New("widget not found")
|
|
}
|
|
|
|
widget := widgets[0]
|
|
|
|
selfDomain := "http://localhost:8002/api/widget/media"
|
|
strWidgetID := strconv.Itoa(int(widgetID))
|
|
if !strings.Contains(string(widget.ImageUrl), "http") && widget.ImageUrl != "" {
|
|
widget.ImageUrl = model.MediaUrl(selfDomain + "/image/get/" + strWidgetID)
|
|
}
|
|
|
|
if !strings.Contains(string(widget.BackgroundUrl), "http") && widget.BackgroundUrl != "" {
|
|
widget.BackgroundUrl = model.MediaUrl(selfDomain + "/background/get/" + strWidgetID)
|
|
}
|
|
|
|
if !strings.Contains(string(widget.AudioUrl), "http") && widget.AudioUrl != "" {
|
|
widget.AudioUrl = model.MediaUrl(selfDomain + "/audio/get/" + strWidgetID)
|
|
}
|
|
|
|
return widget, nil
|
|
}
|
|
|
|
func (widgetRepo *RepoWidget) DeleteWidget(
|
|
ctx context.Context,
|
|
widgetID model.WidgetID,
|
|
) error {
|
|
panic("implement me")
|
|
}
|
|
|
|
func (widgetRepo *RepoWidget) UpdateWidgetDuration(
|
|
ctx context.Context,
|
|
widgetID model.WidgetID,
|
|
duration model.Duration,
|
|
) error {
|
|
query := `
|
|
UPDATE widgets
|
|
SET duration = (@duration)
|
|
WHERE id = (@id)
|
|
`
|
|
|
|
args := pgx.NamedArgs{
|
|
"id": widgetID,
|
|
"duration": duration,
|
|
}
|
|
|
|
_, err := widgetRepo.Query(ctx, query, args)
|
|
if err != nil {
|
|
slog.Error("widgetRepo.Query: " + err.Error())
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|