Files
donat-widget/internal/api/http/handlers/widget/widget.go
2025-03-21 02:37:28 +05:00

229 lines
6.8 KiB
Go

package widget
import (
"context"
"donat-widget/internal/model"
"donat-widget/pkg/validator"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"log/slog"
"net/http"
"strconv"
)
// CreateWidget godoc
// @Summary Create new widget
// @Description Create new widget
// @Tags Widget
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param request body model.CreateWidgetBody true "Create widget"
// @Success 200 {object} model.GetWidgetDb "GetWidgetDb has been created successfully!"
// @Failure 400 {object} echo.HTTPError "Bad request"
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
// @Failure 422 {object} echo.HTTPError "Validation error"
// @Router /widgets [post]
func CreateWidget(widgetService model.WidgetService) echo.HandlerFunc {
return func(request echo.Context) error {
ctx := context.Background()
var body model.CreateWidgetBody
err := validator.ParseAndValidate(&body, request)
authData, err := widgetService.CheckToken(request)
if err != nil {
slog.Error(err.Error())
return echo.NewHTTPError(http.StatusUnauthorized, err.Error())
}
widgetDb, err := widgetService.CreateWidget(
ctx,
authData.AccountID,
body.TemplateID,
body.Duration,
body.MinAmount,
body.MaxAmount,
body.Image,
body.Audio,
body.Name,
body.IsActive,
)
if err != nil {
slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, err.Error())
}
return request.JSON(http.StatusOK, widgetDb)
}
}
// GetStreamersWidgets godoc
// @Summary Get all streamer's widgets
// @Description Get all streamer's widgets
// @Tags Widget
// @Accept json
// @Produce json
// @Security BearerAuth
// @Success 200 {object} model.AllWidgets "Success widgets response"
// @Failure 400 {object} echo.HTTPError "Bad request"
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
// @Failure 422 {object} echo.HTTPError "Validation error"
// @Router /widgets [get]
func GetStreamersWidgets(widgetService model.WidgetService) echo.HandlerFunc {
return func(request echo.Context) error {
ctx := context.Background()
var body model.CreateWidgetBody
err := validator.ParseAndValidate(&body, request)
authData, err := widgetService.CheckToken(request)
if err != nil {
slog.Error(err.Error())
return echo.NewHTTPError(http.StatusUnauthorized, err.Error())
}
widgets, err := widgetService.GetWidgetsByStreamer(
ctx,
authData.AccountID,
)
if err != nil {
slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, err.Error())
}
return request.JSON(http.StatusOK, widgets)
}
}
// UpdateWidget godoc
// @Summary Update an existing widget
// @Description Update an existing widget
// @Tags Widget
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param widget_id path int true "Widget ID"
// @Param request body model.UpdateWidget true "Update widget"
// @Success 200 {string} string "Widget has been updated successfully!"
// @Failure 400 {object} echo.HTTPError "Bad request"
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
// @Failure 422 {object} echo.HTTPError "Validation error"
// @Failure 404 {object} echo.HTTPError "Widget not found"
// @Router /widgets/{widget_id} [patch]
func UpdateWidget(widgetService model.WidgetService) echo.HandlerFunc {
return func(request echo.Context) error {
ctx := context.Background()
widgetIDStr := request.Param("widgetID")
widgetID, err := strconv.Atoi(widgetIDStr)
if err != nil {
return echo.NewHTTPError(http.StatusUnprocessableEntity, "Invalid widget ID")
}
var body model.UpdateWidget
err = validator.ParseAndValidate(&body, request)
if err != nil {
return echo.NewHTTPError(http.StatusUnprocessableEntity, err.Error())
}
authData, err := widgetService.CheckToken(request)
if err != nil {
slog.Error(err.Error())
return echo.NewHTTPError(http.StatusUnauthorized, err.Error())
}
err = widgetService.UpdateWidget(
ctx,
body,
widgetID,
authData.AccountID,
)
if err != nil {
slog.Error(err.Error())
return echo.NewHTTPError(http.StatusInternalServerError, "Internal Server Error")
}
return request.JSON(http.StatusOK, "Widget has been updated successfully!")
}
}
// DeleteWidget godoc
// @Summary Update an existing widget
// @Description Update an existing widget
// @Tags Widget
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param widget_id path int true "Widget ID"
// @Success 200 {string} string "Widget has been deleted successfully!"
// @Failure 400 {object} echo.HTTPError "Bad request"
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
// @Failure 422 {object} echo.HTTPError "Validation error"
// @Failure 404 {object} echo.HTTPError "Widget not found"
// @Router /widgets/{widget_id} [delete]
func DeleteWidget(widgetService model.WidgetService) echo.HandlerFunc {
return func(request echo.Context) error {
ctx := context.Background()
widgetIDStr := request.Param("widgetID")
widgetID, err := strconv.Atoi(widgetIDStr)
if err != nil {
return echo.NewHTTPError(http.StatusUnprocessableEntity, "Invalid widget ID")
}
authData, err := widgetService.CheckToken(request)
if err != nil {
slog.Error(err.Error())
return echo.NewHTTPError(http.StatusUnauthorized, err.Error())
}
err = widgetService.DeleteWidget(
ctx,
authData.AccountID,
widgetID,
)
if err != nil {
slog.Error(err.Error())
return echo.NewHTTPError(http.StatusInternalServerError, "Internal Server Error")
}
return request.JSON(http.StatusOK, "Widget has been deleted successfully!")
}
}
// GetWidgetHTML godoc
// @Summary Get widget HTML content
// @Description Get generated HTML widget for streamer
// @Tags Widget
// @Accept json
// @Produce html
// @Param widget-page-id path string true "Widget Page ID" format:"uuid"
// @Success 200 {string} string "HTML widget content"
// @Failure 400 {object} echo.HTTPError "Invalid streamer ID or background URL"
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
// @Failure 500 {object} echo.HTTPError "Internal server error"
// @Router /get-widget-html/{widget-page-id} [get]
func GetWidgetHTML(widgetService model.WidgetService) echo.HandlerFunc {
return func(c echo.Context) error {
ctx := context.Background()
widgetPageID := c.Param("widget-page-id")
if widgetPageID == "" {
return echo.NewHTTPError(http.StatusBadRequest, "Streamer ID is required")
}
widgetPageUUID, err := uuid.Parse(widgetPageID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Streamer ID is invalid")
}
htmlPage, err := widgetService.GetWidgetHTML(ctx, widgetPageUUID)
if err != nil {
slog.Error(err.Error())
return echo.NewHTTPError(http.StatusInternalServerError, "Internal Server Error")
}
return c.HTML(http.StatusOK, htmlPage)
}
}