89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
package target
|
|
|
|
import (
|
|
"context"
|
|
"donat-widget/internal/model"
|
|
"github.com/labstack/echo/v4"
|
|
"net/http"
|
|
)
|
|
|
|
func CreateTarget(targetService model.TargetService) echo.HandlerFunc {
|
|
type CreateTargetBody struct {
|
|
StreamerID model.StreamerID `json:"streamerID"`
|
|
Amount model.DonatAmount `json:"amount"`
|
|
Text string `json:"text"`
|
|
}
|
|
return func(request echo.Context) error {
|
|
ctx := context.Background()
|
|
var body CreateTargetBody
|
|
if err := request.Bind(&body); err != nil {
|
|
return echo.NewHTTPError(400, err.Error())
|
|
}
|
|
if err := request.Validate(&body); err != nil {
|
|
|
|
return echo.NewHTTPError(400, err.Error())
|
|
}
|
|
|
|
err := targetService.CreateTarget(ctx, body.StreamerID, body.Amount, body.Text)
|
|
if err != nil {
|
|
return request.JSON(http.StatusInternalServerError, err)
|
|
}
|
|
return request.String(http.StatusOK, "Created target successfully")
|
|
}
|
|
}
|
|
|
|
func GetAllTarget(targetService model.TargetService) echo.HandlerFunc {
|
|
type GetAllTargetBody struct {
|
|
StreamerID model.StreamerID `json:"streamer_id"`
|
|
}
|
|
return func(request echo.Context) error {
|
|
ctx := context.Background()
|
|
var body GetAllTargetBody
|
|
if err := request.Bind(&body); err != nil {
|
|
return echo.NewHTTPError(400, err.Error())
|
|
}
|
|
if err := request.Validate(&body); err != nil {
|
|
|
|
return echo.NewHTTPError(400, err.Error())
|
|
}
|
|
|
|
targets, err := targetService.GetAllTarget(ctx, body.StreamerID)
|
|
if err != nil {
|
|
return request.JSON(http.StatusInternalServerError, err)
|
|
}
|
|
|
|
return request.JSON(http.StatusOK, targets)
|
|
}
|
|
}
|
|
|
|
func AddAmountTarget(
|
|
targetService model.TargetService,
|
|
donatService model.DonatService,
|
|
) echo.HandlerFunc {
|
|
type AddAmountTargetBody struct {
|
|
OrderID model.OrderID `json:"order_id"`
|
|
}
|
|
return func(request echo.Context) error {
|
|
ctx := context.Background()
|
|
var body AddAmountTargetBody
|
|
if err := request.Bind(&body); err != nil {
|
|
return echo.NewHTTPError(400, err.Error())
|
|
}
|
|
if err := request.Validate(&body); err != nil {
|
|
|
|
return echo.NewHTTPError(400, err.Error())
|
|
}
|
|
|
|
donat, err := donatService.GetDonatByOrderID(ctx, body.OrderID)
|
|
if err != nil {
|
|
return request.JSON(http.StatusInternalServerError, err)
|
|
}
|
|
|
|
err = targetService.AddAmountToTarget(ctx, donat.TargetID, donat.Amount)
|
|
if err != nil {
|
|
return request.JSON(http.StatusInternalServerError, err)
|
|
}
|
|
return request.JSON(http.StatusOK, "Added target successfully")
|
|
}
|
|
}
|