2025-04-04 11:48:44 +05:00

114 lines
3.1 KiB
Go

package target
import (
"context"
"donat-widget/internal/model"
"github.com/labstack/echo/v4"
"log/slog"
"net/http"
)
func CreateTarget(targetService model.TargetService, authClient model.AuthClient) echo.HandlerFunc {
type CreateTargetBody struct {
Amount int `json:"amount"`
Text string `json:"text"`
}
type CreateTargetResponse struct {
TargetID int `json:"targetID"`
}
return func(request echo.Context) error {
ctx := context.Background()
var body CreateTargetBody
if err := request.Bind(&body); err != nil {
slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, err.Error())
}
if err := request.Validate(&body); err != nil {
slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, err.Error())
}
authData, err := targetService.CheckToken(request)
if err != nil {
slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, err.Error())
}
targetID, err := targetService.CreateTarget(ctx,
authData.AccountID,
body.Amount,
body.Text,
)
if err != nil {
slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, err.Error())
}
return request.JSON(http.StatusOK, CreateTargetResponse{TargetID: targetID})
}
}
func GetAllTarget(targetService model.TargetService) echo.HandlerFunc {
type GetAllTargetBody struct {
StreamerID int `json:"streamerID"`
}
return func(request echo.Context) error {
ctx := context.Background()
var body GetAllTargetBody
if err := request.Bind(&body); err != nil {
slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, err.Error())
}
if err := request.Validate(&body); err != nil {
slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, err.Error())
}
targets, err := targetService.GetAllTarget(ctx, body.StreamerID)
if err != nil {
slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, err.Error())
}
if len(targets) == 0 {
slog.Error("target not found")
return request.JSON(http.StatusInternalServerError, "target not found")
}
return request.JSON(http.StatusOK, targets)
}
}
func AddAmountTarget(
targetService model.TargetService,
donatService model.DonatService,
) echo.HandlerFunc {
type AddAmountTargetBody struct {
OrderID string `json:"orderID"`
}
return func(request echo.Context) error {
ctx := context.Background()
var body AddAmountTargetBody
if err := request.Bind(&body); err != nil {
slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, err.Error())
}
if err := request.Validate(&body); err != nil {
slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, err.Error())
}
donat, err := donatService.GetDonatByOrderID(ctx, body.OrderID)
if err != nil {
slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, err.Error())
}
err = targetService.AddAmountToTarget(ctx, *donat.TargetID, donat.Amount)
if err != nil {
slog.Error(err.Error())
return request.JSON(http.StatusInternalServerError, err.Error())
}
return request.JSON(http.StatusOK, "Added target successfully")
}
}