97 lines
2.4 KiB
Go
97 lines
2.4 KiB
Go
package donat
|
|
|
|
import (
|
|
"context"
|
|
"donat-widget/internal/model"
|
|
"github.com/google/uuid"
|
|
"github.com/labstack/echo/v4"
|
|
"log/slog"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
func CreateDonat(donatService model.DonatService) echo.HandlerFunc {
|
|
type CreateDonatBody struct {
|
|
StreamerID model.StreamerID `json:"streamerID"`
|
|
TargetID model.TargetID `json:"targetID"`
|
|
Amount model.DonatAmount `json:"amount"`
|
|
Text string `json:"text"`
|
|
DonatUser string `json:"donatUser"`
|
|
}
|
|
return func(request echo.Context) error {
|
|
ctx := context.Background()
|
|
var body CreateDonatBody
|
|
if err := request.Bind(&body); err != nil {
|
|
slog.Error(err.Error())
|
|
return echo.NewHTTPError(400, err.Error())
|
|
}
|
|
if err := request.Validate(&body); err != nil {
|
|
slog.Error(err.Error())
|
|
return echo.NewHTTPError(400, err.Error())
|
|
}
|
|
|
|
orderID := model.OrderID(uuid.New().String())
|
|
|
|
order, err := donatService.CreateDonat(
|
|
ctx,
|
|
body.StreamerID,
|
|
orderID,
|
|
body.TargetID,
|
|
body.Amount,
|
|
body.Text,
|
|
body.DonatUser,
|
|
)
|
|
if err != nil {
|
|
return request.JSON(500, "Create donat error")
|
|
}
|
|
|
|
return request.JSON(http.StatusCreated, order)
|
|
}
|
|
}
|
|
|
|
func MarkDonatPaid(donatService model.DonatService) echo.HandlerFunc {
|
|
type MarkDonatPaidBody struct {
|
|
OrderID model.OrderID `json:"orderID"`
|
|
}
|
|
return func(request echo.Context) error {
|
|
ctx := context.Background()
|
|
var body MarkDonatPaidBody
|
|
if err := request.Bind(&body); err != nil {
|
|
slog.Error(err.Error())
|
|
return echo.NewHTTPError(400, err.Error())
|
|
}
|
|
if err := request.Validate(&body); err != nil {
|
|
slog.Error(err.Error())
|
|
return echo.NewHTTPError(400, err.Error())
|
|
}
|
|
|
|
err := donatService.MarkDonatPaid(ctx, body.OrderID)
|
|
if err != nil {
|
|
slog.Error("donatService.MarkDonatPaid error: " + err.Error())
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return request.String(200, "Donat paid success")
|
|
}
|
|
}
|
|
|
|
func MarkDonatView(donatService model.DonatService) 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.MarkDonatView(
|
|
ctx,
|
|
model.DonatID(donatID),
|
|
)
|
|
if err != nil {
|
|
return echo.NewHTTPError(500, "Delete donat error")
|
|
}
|
|
slog.Info("Delete donat success")
|
|
return request.String(200, "donat view success")
|
|
}
|
|
}
|