150 lines
4.1 KiB
Go
150 lines
4.1 KiB
Go
package donat
|
|
|
|
import (
|
|
"context"
|
|
"donat-widget/internal/model"
|
|
"fmt"
|
|
"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 request.JSON(http.StatusInternalServerError, err.Error())
|
|
}
|
|
if err := request.Validate(&body); err != nil {
|
|
slog.Error(err.Error())
|
|
return request.JSON(http.StatusInternalServerError, 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(http.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
return request.JSON(http.StatusCreated, order)
|
|
}
|
|
}
|
|
|
|
func GetDonat(donatService model.DonatService) echo.HandlerFunc {
|
|
return func(request echo.Context) error {
|
|
ctx := context.Background()
|
|
streamerID, err := strconv.Atoi(request.Param("streamerID"))
|
|
fmt.Println(streamerID)
|
|
if err != nil {
|
|
slog.Error(err.Error())
|
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
|
}
|
|
donats, err := donatService.GetDonatByStreamerID(ctx, model.StreamerID(streamerID))
|
|
if err != nil {
|
|
slog.Error(err.Error())
|
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
slog.Info("get donat successfully")
|
|
return request.JSON(200, donats)
|
|
|
|
}
|
|
}
|
|
|
|
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 request.JSON(http.StatusInternalServerError, err.Error())
|
|
}
|
|
if err := request.Validate(&body); err != nil {
|
|
slog.Error(err.Error())
|
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
|
}
|
|
|
|
err := donatService.MarkDonatPaid(ctx, body.OrderID)
|
|
if err != nil {
|
|
slog.Error(err.Error())
|
|
return request.JSON(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 request.JSON(400, err.Error())
|
|
}
|
|
|
|
err = donatService.MarkDonatView(
|
|
ctx,
|
|
model.DonatID(donatID),
|
|
)
|
|
if err != nil {
|
|
return request.JSON(500, err.Error())
|
|
}
|
|
slog.Info("Delete donat success")
|
|
return request.String(200, "donat view success")
|
|
}
|
|
}
|
|
|
|
func GetInnerDonatePage(donatService model.DonatService) echo.HandlerFunc {
|
|
return func(request echo.Context) error {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func GetOuterDonatePage(donatService model.DonatService) echo.HandlerFunc {
|
|
return func(request echo.Context) error {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// UpdateDonatePage godoc
|
|
// @Summary Update personal streamer donate page.
|
|
// @Description Update personal streamer donate page.
|
|
// @Tags Donate
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body model.UpdateDonatPage true "Update fields"
|
|
// @Param background formData file false "Background image"
|
|
// @Success 200 {string} string "Donat page updated successfully"
|
|
// @Failure 400 {object} echo.HTTPError "Bad request"
|
|
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
|
|
// @Failure 422 {object} echo.HTTPError "Validation error"
|
|
// @Router /donat-page [patch]
|
|
func UpdateDonatePage(donatService model.DonatService) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
return nil
|
|
}
|
|
}
|