193 lines
5.5 KiB
Go

package widget
import (
"context"
"donat-widget/internal/model"
"donat-widget/pkg/validator"
"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!")
}
}