100 lines
2.2 KiB
Go
100 lines
2.2 KiB
Go
package donat
|
|
|
|
import (
|
|
"context"
|
|
"donat-widget/internal/model"
|
|
"github.com/labstack/echo/v4"
|
|
"log/slog"
|
|
"strconv"
|
|
)
|
|
|
|
type donatSetter interface {
|
|
SetDonat(
|
|
ctx context.Context,
|
|
widgetID model.WidgetID,
|
|
text string,
|
|
amount model.DonatAmount,
|
|
donatUser string,
|
|
) error
|
|
}
|
|
type SetDonatRequest struct {
|
|
WidgetID model.WidgetID `json:"widgetID" validate:"required"`
|
|
Text string `json:"text" validate:"required"`
|
|
Amount model.DonatAmount `json:"amount" validate:"required"`
|
|
DonatUser string `json:"donatUser" validate:"required"`
|
|
}
|
|
|
|
// SetDonat
|
|
//
|
|
// @Description Set donat
|
|
// @Tags Donat
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param RegisterData body SetDonatRequest true "Set donat"
|
|
// @Router /api/widget/donat/set [post]
|
|
func SetDonat(donatService donatSetter) echo.HandlerFunc {
|
|
return func(request echo.Context) error {
|
|
ctx := context.Background()
|
|
|
|
var donatData SetDonatRequest
|
|
if err := request.Bind(&donatData); err != nil {
|
|
slog.Error(err.Error())
|
|
return echo.NewHTTPError(400, err.Error())
|
|
}
|
|
|
|
err := request.Validate(&donatData)
|
|
if err != nil {
|
|
slog.Error(err.Error())
|
|
return echo.NewHTTPError(400, err.Error())
|
|
}
|
|
|
|
err = donatService.SetDonat(
|
|
ctx,
|
|
donatData.WidgetID,
|
|
donatData.Text,
|
|
donatData.Amount,
|
|
donatData.DonatUser,
|
|
)
|
|
if err != nil {
|
|
return request.JSON(500, "Set donat error")
|
|
}
|
|
slog.Info("donat set success")
|
|
return request.String(200, "Set donat success")
|
|
}
|
|
}
|
|
|
|
type donatDeleter interface {
|
|
DeleteDonat(
|
|
ctx context.Context,
|
|
DonatID model.DonatID,
|
|
) error
|
|
}
|
|
|
|
// DeleteDonat
|
|
//
|
|
// @Description Delete donat
|
|
// @Tags Donat
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Router /api/widget/donat/delete/{donatID} [post]
|
|
func DeleteDonat(donatService donatDeleter) echo.HandlerFunc {
|
|
return func(request echo.Context) error {
|
|
ctx := context.Background()
|
|
|
|
donatID, err := strconv.Atoi(request.Param("donatID"))
|
|
if err != nil {
|
|
return echo.NewHTTPError(400, "Path parameter 'donatID' is invalid")
|
|
}
|
|
|
|
err = donatService.DeleteDonat(
|
|
ctx,
|
|
model.DonatID(donatID),
|
|
)
|
|
if err != nil {
|
|
return echo.NewHTTPError(500, "Delete donat error")
|
|
}
|
|
slog.Info("Delete donat success")
|
|
return request.String(200, "Delete donat success")
|
|
}
|
|
}
|