110 lines
3.2 KiB
Go
110 lines
3.2 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 model.DonatAmount `json:"amount"`
|
|
Text string `json:"text"`
|
|
}
|
|
type CreateTargetResponse struct {
|
|
TargetID model.TargetID `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 := authClient.CheckToken(request)
|
|
if err != nil {
|
|
slog.Error(err.Error())
|
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
targetID, err := targetService.CreateTarget(ctx, model.StreamerID(authData.StreamerID), 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 model.StreamerID `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 model.OrderID `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")
|
|
}
|
|
}
|