213 lines
5.3 KiB
Go
213 lines
5.3 KiB
Go
package widget
|
|
|
|
import (
|
|
"context"
|
|
"donat-widget/internal/model"
|
|
"github.com/labstack/echo/v4"
|
|
"log/slog"
|
|
"strconv"
|
|
)
|
|
|
|
type widgetCreator interface {
|
|
CreateWidget(
|
|
ctx context.Context,
|
|
streamerID model.StreamerID,
|
|
templateID model.TemplateID,
|
|
) (model.WidgetID, error)
|
|
}
|
|
type CreateWidgetRequest struct {
|
|
StreamerID model.StreamerID `json:"streamerID" validate:"required"`
|
|
TemplateID model.TemplateID `json:"templateID" validate:"required"`
|
|
}
|
|
type CreateWidgetResponse struct {
|
|
WidgetID model.WidgetID `json:"widgetID"`
|
|
}
|
|
|
|
// CreateWidget
|
|
//
|
|
// @Description Create widget
|
|
// @Tags Widget
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param RegisterData body CreateWidgetRequest true "Create widget"
|
|
// @Success 200 {object} CreateWidgetResponse
|
|
// @Router /api/widget/create [post]
|
|
func CreateWidget(widgetService widgetCreator) echo.HandlerFunc {
|
|
return func(request echo.Context) error {
|
|
ctx := context.Background()
|
|
|
|
var widgetData CreateWidgetRequest
|
|
if err := request.Bind(&widgetData); err != nil {
|
|
return echo.NewHTTPError(400, err.Error())
|
|
}
|
|
|
|
err := request.Validate(&widgetData)
|
|
if err != nil {
|
|
return echo.NewHTTPError(400, err.Error())
|
|
}
|
|
|
|
widgetID, err := widgetService.CreateWidget(
|
|
ctx,
|
|
widgetData.StreamerID,
|
|
widgetData.TemplateID,
|
|
)
|
|
if err != nil {
|
|
return request.JSON(422, "Create widget error")
|
|
}
|
|
|
|
response := CreateWidgetResponse{
|
|
WidgetID: widgetID,
|
|
}
|
|
slog.Info("Widget created")
|
|
return request.JSON(200, response)
|
|
}
|
|
}
|
|
|
|
type widgetHTMLGetter interface {
|
|
GetWidgetHTML(
|
|
ctx context.Context,
|
|
widgetID model.WidgetID,
|
|
) (model.WidgetHTML, error)
|
|
}
|
|
|
|
// GetWidgetHTML @Description Get widget
|
|
//
|
|
// @Tags Widget
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200
|
|
// @Router /api/widget/html/{widgetID} [get]
|
|
func GetWidgetHTML(widgetService widgetHTMLGetter) echo.HandlerFunc {
|
|
return func(request echo.Context) error {
|
|
ctx := context.Background()
|
|
|
|
widgetID, err := strconv.Atoi(request.Param("widgetID"))
|
|
if err != nil {
|
|
return echo.NewHTTPError(400, "Path parameter 'widgetID' is invalid")
|
|
}
|
|
|
|
widgetHTML, err := widgetService.GetWidgetHTML(
|
|
ctx,
|
|
model.WidgetID(widgetID),
|
|
)
|
|
if err != nil {
|
|
return request.JSON(500, "Get widget HTML error")
|
|
}
|
|
slog.Info("Get widget HTML successfully")
|
|
return request.HTML(200, string(widgetHTML))
|
|
}
|
|
}
|
|
|
|
type widgetInfoGetter interface {
|
|
GetWidgetInfo(
|
|
ctx context.Context,
|
|
widgetID model.WidgetID,
|
|
) (*model.DonatAndWidget, error)
|
|
}
|
|
|
|
type GetInfoResponse struct {
|
|
AudioUrl model.MediaUrl `json:"audioUrl"`
|
|
ImageUrl model.MediaUrl `json:"imageUrl"`
|
|
Text string `json:"text"`
|
|
Amount model.DonatAmount `json:"amount"`
|
|
DonatUser string `json:"donatUser"`
|
|
Display model.Display `json:"display"`
|
|
Duration model.Duration `json:"duration"`
|
|
DonatID model.DonatID `json:"donatID"`
|
|
}
|
|
|
|
// GetWidgetInfo
|
|
//
|
|
// @Description Widget Info
|
|
// @Tags Widget
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param widgetID path int true "Widget ID"
|
|
// @Router /api/widget/info/{widgetID} [get]
|
|
func GetWidgetInfo(widgetService widgetInfoGetter) echo.HandlerFunc {
|
|
return func(request echo.Context) error {
|
|
ctx := context.Background()
|
|
|
|
widgetID, err := strconv.Atoi(request.Param("widgetID"))
|
|
if err != nil {
|
|
return echo.NewHTTPError(400, "Path parameter 'widgetID' is invalid")
|
|
}
|
|
|
|
donatAndWidget, err := widgetService.GetWidgetInfo(ctx, model.WidgetID(widgetID))
|
|
if err != nil {
|
|
return request.JSON(422, "Get widget info error")
|
|
}
|
|
|
|
if !donatAndWidget.Display {
|
|
response := GetInfoResponse{
|
|
Display: donatAndWidget.Display,
|
|
}
|
|
slog.Info("Get widget info successfully")
|
|
return request.JSON(200, response)
|
|
}
|
|
|
|
response := GetInfoResponse{
|
|
AudioUrl: donatAndWidget.Widget.AudioUrl,
|
|
ImageUrl: donatAndWidget.Widget.ImageUrl,
|
|
Text: donatAndWidget.Donat.Text,
|
|
Display: donatAndWidget.Display,
|
|
Duration: donatAndWidget.Widget.Duration,
|
|
DonatUser: donatAndWidget.Donat.DonatUser,
|
|
Amount: donatAndWidget.Donat.Amount,
|
|
DonatID: donatAndWidget.Donat.ID,
|
|
}
|
|
slog.Info("Get widget info successfully")
|
|
return request.JSON(200, response)
|
|
}
|
|
}
|
|
|
|
type widgetDurationUpdater interface {
|
|
UpdateWidgetDuration(
|
|
ctx context.Context,
|
|
widgetID model.WidgetID,
|
|
duration model.Duration,
|
|
) error
|
|
}
|
|
|
|
type UpdateDurationRequest struct {
|
|
WidgetID model.WidgetID `json:"widgetID" validate:"required"`
|
|
Duration model.Duration `json:"duration" validate:"required"`
|
|
}
|
|
|
|
// UpdateDuration
|
|
//
|
|
// @Description UpdateDuration
|
|
// @Tags Widget
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param UpdateData body UpdateDurationRequest true "UpdateDuration"
|
|
// @Success 200 {string}
|
|
// @Router /api/widget/duration/update [post]
|
|
func UpdateDuration(widgetService widgetDurationUpdater) echo.HandlerFunc {
|
|
return func(request echo.Context) error {
|
|
ctx := context.Background()
|
|
|
|
var widgetData UpdateDurationRequest
|
|
if err := request.Bind(&widgetData); err != nil {
|
|
return echo.NewHTTPError(400, err.Error())
|
|
}
|
|
|
|
err := request.Validate(&widgetData)
|
|
if err != nil {
|
|
return echo.NewHTTPError(400, err.Error())
|
|
}
|
|
|
|
err = widgetService.UpdateWidgetDuration(
|
|
ctx,
|
|
widgetData.WidgetID,
|
|
widgetData.Duration,
|
|
)
|
|
if err != nil {
|
|
return request.JSON(422, "Update duration error")
|
|
}
|
|
|
|
slog.Info("Duration updated")
|
|
return request.JSON(200, "Update duration successfully")
|
|
}
|
|
}
|