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"` } 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()) } 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.String(http.StatusOK, "Created target successfully") } } 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") } }