350 lines
11 KiB
Go
350 lines
11 KiB
Go
package donat
|
|
|
|
import (
|
|
"context"
|
|
"donat-widget/internal/model"
|
|
"donat-widget/pkg/custom_response"
|
|
"donat-widget/pkg/validator"
|
|
"fmt"
|
|
"github.com/google/uuid"
|
|
"github.com/labstack/echo/v4"
|
|
"log/slog"
|
|
"mime/multipart"
|
|
"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")
|
|
}
|
|
}
|
|
|
|
// GetInnerDonatePage godoc
|
|
// @Summary Get inner donate page info
|
|
// @Description Get inner donate page info
|
|
// @Tags Donate
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Success 200 {object} model.InnerDonatePageResponse "Current donate page state"
|
|
// @Failure 400 {object} echo.HTTPError "Bad request"
|
|
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
|
|
// @Failure 422 {object} echo.HTTPError "Validation error"
|
|
// @Router /inner-donate-page [get]
|
|
func GetInnerDonatePage(donatService model.DonatService) echo.HandlerFunc {
|
|
return func(request echo.Context) error {
|
|
authData, err := donatService.CheckToken(request)
|
|
if err != nil {
|
|
slog.Error("Unauthorized")
|
|
return echo.NewHTTPError(http.StatusUnauthorized, err.Error())
|
|
}
|
|
ctx := context.Background()
|
|
|
|
var innerPage model.InnerDonatePageResponse
|
|
innerPage, err = donatService.GetInnerDonatPage(ctx, model.StreamerID(authData.AccountID))
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
return request.JSON(http.StatusOK, innerPage)
|
|
}
|
|
}
|
|
|
|
// GetOuterDonatePage godoc
|
|
// @Summary Get outer donate page info
|
|
// @Description Get outer donate page info
|
|
// @Tags Donate
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param streamer-login path string true "Login стримера"
|
|
// @Success 200 {object} model.OuterDonatePageResponse "Current donate page state"
|
|
// @Failure 400 {object} echo.HTTPError "Bad request"
|
|
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
|
|
// @Failure 422 {object} echo.HTTPError "Validation error"
|
|
// @Router /outer-donate-page/{streamer-login} [get]
|
|
func GetOuterDonatePage(donatService model.DonatService) echo.HandlerFunc {
|
|
return func(request echo.Context) error {
|
|
ctx := context.Background()
|
|
streamerLogin := request.Param("streamer-login")
|
|
|
|
outerPageResponse, err := donatService.GetOuterDonatPage(
|
|
ctx, streamerLogin,
|
|
)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, custom_response.InternalError)
|
|
}
|
|
return request.JSON(http.StatusOK, outerPageResponse)
|
|
}
|
|
}
|
|
|
|
// UpdateDonatePage godoc
|
|
// @Summary Update personal streamer donate page
|
|
// @Description Update personal streamer donate page with optional background and head image files.
|
|
// @Tags Donate
|
|
// @Consumes multipart/form-data
|
|
// @Produces json
|
|
// @Security BearerAuth
|
|
// @Param metadata formData model.UpdateDonatPage false "Update fields"
|
|
// @Param background formData file false "Background image"
|
|
// @Param head_img formData file false "Head 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, fileService model.FileService) echo.HandlerFunc {
|
|
return func(request echo.Context) error {
|
|
ctx := context.Background()
|
|
|
|
var body model.UpdateDonatPage
|
|
err := validator.ParseAndValidate(&body, request)
|
|
if err != nil {
|
|
slog.Error(err.Error())
|
|
return echo.NewHTTPError(http.StatusUnprocessableEntity, "Unprocessable Entity")
|
|
}
|
|
|
|
authData, err := donatService.CheckToken(request)
|
|
if err != nil {
|
|
slog.Error(err.Error())
|
|
return echo.NewHTTPError(http.StatusUnauthorized, custom_response.Unauthorized)
|
|
}
|
|
|
|
bgFile, err := request.FormFile("background")
|
|
if err != nil {
|
|
bgFile = &multipart.FileHeader{
|
|
Filename: "",
|
|
Size: 0,
|
|
Header: nil,
|
|
}
|
|
}
|
|
|
|
headImgFile, err := request.FormFile("head_img")
|
|
if err != nil {
|
|
headImgFile = &multipart.FileHeader{
|
|
Filename: "",
|
|
Size: 0,
|
|
Header: nil,
|
|
}
|
|
}
|
|
|
|
// Передаем файлы только если они были переданы
|
|
err = donatService.UpdateDonatePage(
|
|
ctx,
|
|
authData.AccountID,
|
|
body,
|
|
*bgFile,
|
|
*headImgFile,
|
|
)
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, custom_response.InternalError)
|
|
}
|
|
return request.JSON(http.StatusOK, "Success")
|
|
}
|
|
}
|
|
|
|
// GetVoiceSettings godoc
|
|
// @Summary Get donat voice settings
|
|
// @Description Get donat voice settings
|
|
// @Tags Donate
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} model.VoiceSettingsResponse "Current voice settings"
|
|
// @Failure 400 {object} echo.HTTPError "Bad request"
|
|
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
|
|
// @Failure 422 {object} echo.HTTPError "Validation error"
|
|
// @Router /voice-settings [get]
|
|
func GetVoiceSettings(donatService model.DonatService) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// UpdateVoiceSettings godoc
|
|
// @Summary Update donat voice settings.
|
|
// @Description Update donat voice settings.
|
|
// @Tags Donate
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body model.UpdateVoiceSettings true "Update fields"
|
|
// @Param background formData file false "Background image"
|
|
// @Success 200 {string} string "Voice settings 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 /voice-settings [patch]
|
|
func UpdateVoiceSettings(donatService model.DonatService) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// GetFiltersSettings godoc
|
|
// @Summary Get donat filters settings
|
|
// @Description Get donat filters settings
|
|
// @Tags Donate
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} model.FilterSettingResponse "Current filters settings"
|
|
// @Failure 400 {object} echo.HTTPError "Bad request"
|
|
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
|
|
// @Failure 422 {object} echo.HTTPError "Validation error"
|
|
// @Router /filters-settings [get]
|
|
func GetFiltersSettings(donatService model.DonatService) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// UpdateFiltersSettings godoc
|
|
// @Summary Update donat filters settings.
|
|
// @Description Update donat filters settings.
|
|
// @Tags Donate
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body model.UpdateFilterSettings true "Update fields"
|
|
// @Param background formData file false "Background image"
|
|
// @Success 200 {string} string "Voice settings 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 /filters-settings [patch]
|
|
func UpdateFiltersSettings(donatService model.DonatService) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// GetModerationSettings godoc
|
|
// @Summary Get donat moderation settings
|
|
// @Description Get donat moderation settings
|
|
// @Tags Donate
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} model.ModerationResponse "Current moderation settings"
|
|
// @Failure 400 {object} echo.HTTPError "Bad request"
|
|
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
|
|
// @Failure 422 {object} echo.HTTPError "Validation error"
|
|
// @Router /moderation-settings [get]
|
|
func GetModerationSettings(donatService model.DonatService) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// UpdateModerationSettings godoc
|
|
// @Summary Update donat moderation settings
|
|
// @Description Update donat moderation settings
|
|
// @Tags Donate
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} model.UpdateModeration "Update moderation settings"
|
|
// @Failure 400 {object} echo.HTTPError "Bad request"
|
|
// @Failure 401 {object} echo.HTTPError "Unauthorized or expired token"
|
|
// @Failure 422 {object} echo.HTTPError "Validation error"
|
|
// @Router /moderation-settings [patch]
|
|
func UpdateModerationSettings(donatService model.DonatService) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
return nil
|
|
}
|
|
}
|