|
|
|
@ -1,226 +1,209 @@
|
|
|
|
|
//package widget
|
|
|
|
|
//
|
|
|
|
|
//import (
|
|
|
|
|
// "context"
|
|
|
|
|
// "donat-widget/internal/model"
|
|
|
|
|
// "github.com/labstack/echo/v4"
|
|
|
|
|
// "log/slog"
|
|
|
|
|
// "net/http"
|
|
|
|
|
// "strconv"
|
|
|
|
|
//)
|
|
|
|
|
//
|
|
|
|
|
//func CreateWidget(widgetService model.WidgetService, authClient model.AuthClient) echo.HandlerFunc {
|
|
|
|
|
// type CreateWidgetBody struct {
|
|
|
|
|
// TemplateID model.TemplateID `json:"templateID"`
|
|
|
|
|
// MinAmount model.DonatAmount `json:"minAmount"`
|
|
|
|
|
// Duration model.Duration `json:"duration"`
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// type CreateWidgetResponse struct {
|
|
|
|
|
// WidgetID model.WidgetID `json:"widgetID"`
|
|
|
|
|
// }
|
|
|
|
|
// return func(request echo.Context) error {
|
|
|
|
|
// ctx := context.Background()
|
|
|
|
|
// var body CreateWidgetBody
|
|
|
|
|
// 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())
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// if authData.StreamerID == 0 {
|
|
|
|
|
// slog.Error("Unauthorized account")
|
|
|
|
|
// return request.JSON(http.StatusUnauthorized, "Unauthorized")
|
|
|
|
|
// }
|
|
|
|
|
// if authData.StreamerID == -1 {
|
|
|
|
|
// slog.Error("Expired token")
|
|
|
|
|
// return request.JSON(http.StatusUnauthorized, "Expired token")
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// widgetID, err := widgetService.CreateWidget(
|
|
|
|
|
// ctx,
|
|
|
|
|
// model.StreamerID(authData.StreamerID),
|
|
|
|
|
// body.TemplateID,
|
|
|
|
|
// body.Duration,
|
|
|
|
|
// body.MinAmount,
|
|
|
|
|
// )
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// slog.Error(err.Error())
|
|
|
|
|
// return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// response := CreateWidgetResponse{
|
|
|
|
|
// WidgetID: widgetID,
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// return request.JSON(200, response)
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//func GetWidgetHTML(widgetService model.WidgetService) echo.HandlerFunc {
|
|
|
|
|
// return func(request echo.Context) error {
|
|
|
|
|
// ctx := context.Background()
|
|
|
|
|
//
|
|
|
|
|
// streamerID, err := strconv.Atoi(request.Param("streamerID"))
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// slog.Error(err.Error())
|
|
|
|
|
// return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// widgetHTML, err := widgetService.GetWidgetHTML(
|
|
|
|
|
// ctx,
|
|
|
|
|
// model.StreamerID(streamerID),
|
|
|
|
|
// )
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// slog.Error(err.Error())
|
|
|
|
|
// return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// slog.Info("Get widget HTML successfully")
|
|
|
|
|
// return request.HTML(200, string(widgetHTML))
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//func GetWidgetInfo(widgetService model.WidgetService) echo.HandlerFunc {
|
|
|
|
|
// type Response struct {
|
|
|
|
|
// AudioUrl model.MediaUrl `json:"audioUrl"`
|
|
|
|
|
// ImageUrl model.MediaUrl `json:"imageUrl"`
|
|
|
|
|
// Duration model.Duration `json:"duration"`
|
|
|
|
|
// }
|
|
|
|
|
// return func(request echo.Context) error {
|
|
|
|
|
// ctx := context.Background()
|
|
|
|
|
//
|
|
|
|
|
// widgetID, err := strconv.Atoi(request.Param("widgetID"))
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// slog.Error(err.Error())
|
|
|
|
|
// return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// widget, err := widgetService.GetWidgetByID(ctx, model.WidgetID(widgetID))
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// slog.Error(err.Error())
|
|
|
|
|
// return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
// }
|
|
|
|
|
// if len(widget) == 0 {
|
|
|
|
|
// return request.JSON(http.StatusNotFound, "widget not found")
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// return request.JSON(200, widget)
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//func SetMediaFile(widgetService model.WidgetService) echo.HandlerFunc {
|
|
|
|
|
// return func(request echo.Context) error {
|
|
|
|
|
// ctx := context.Background()
|
|
|
|
|
//
|
|
|
|
|
// widgetID, err := strconv.Atoi(request.FormValue("widgetID"))
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// slog.Error(err.Error())
|
|
|
|
|
// return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// mediaType := request.Param("mediaType")
|
|
|
|
|
//
|
|
|
|
|
// file, err := request.FormFile("file")
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// slog.Error(err.Error())
|
|
|
|
|
// return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// src, err := file.Open()
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// slog.Error(err.Error())
|
|
|
|
|
// return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// err = widgetService.SetMediaFile(
|
|
|
|
|
// ctx,
|
|
|
|
|
// model.MediaType(mediaType),
|
|
|
|
|
// model.WidgetID(widgetID),
|
|
|
|
|
// &src,
|
|
|
|
|
// file.Filename,
|
|
|
|
|
// file.Size,
|
|
|
|
|
// "",
|
|
|
|
|
// )
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// slog.Error(err.Error())
|
|
|
|
|
// return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
// }
|
|
|
|
|
// slog.Info("set " + mediaType + " file successfully")
|
|
|
|
|
// return request.String(200, "File successfully uploaded")
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//func GetMediaFile(widgetService model.WidgetService) echo.HandlerFunc {
|
|
|
|
|
// return func(request echo.Context) error {
|
|
|
|
|
// ctx := context.Background()
|
|
|
|
|
//
|
|
|
|
|
// mediaType := request.Param("mediaType")
|
|
|
|
|
// if mediaType != "background" && mediaType != "image" && mediaType != "audio" {
|
|
|
|
|
// slog.Error("Path parameter 'mediaType' is invalid")
|
|
|
|
|
// return echo.NewHTTPError(400, "Path parameter 'mediaType' is invalid")
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// widgetID, err := strconv.Atoi(request.Param("widgetID"))
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// slog.Error(err.Error())
|
|
|
|
|
// return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// file, err := widgetService.GetMediaFile(
|
|
|
|
|
// ctx,
|
|
|
|
|
// model.WidgetID(widgetID),
|
|
|
|
|
// model.MediaType(mediaType),
|
|
|
|
|
// )
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// slog.Error(err.Error())
|
|
|
|
|
// return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// slog.Info("get " + mediaType + " file successfully")
|
|
|
|
|
// return request.Blob(200, "application/octet-stream", file)
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
//
|
|
|
|
|
//func SetMediaUrl(widgetService model.WidgetService) echo.HandlerFunc {
|
|
|
|
|
// type Body struct {
|
|
|
|
|
// WidgetID model.WidgetID `json:"widgetID" validate:"required"`
|
|
|
|
|
// MediaUrl model.MediaUrl `json:"mediaUrl" validate:"required"`
|
|
|
|
|
// }
|
|
|
|
|
// return func(request echo.Context) error {
|
|
|
|
|
// ctx := context.Background()
|
|
|
|
|
// var body Body
|
|
|
|
|
// 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())
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// mediaType := request.Param("mediaType")
|
|
|
|
|
//
|
|
|
|
|
// err := widgetService.SetMediaUrl(
|
|
|
|
|
// ctx,
|
|
|
|
|
// model.MediaType(mediaType),
|
|
|
|
|
// body.WidgetID,
|
|
|
|
|
// body.MediaUrl,
|
|
|
|
|
// )
|
|
|
|
|
// if err != nil {
|
|
|
|
|
// slog.Error(err.Error())
|
|
|
|
|
// return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// return request.String(200, "Media URL successfully set")
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
package widget
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"donat-widget/internal/model"
|
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
|
"log/slog"
|
|
|
|
|
"net/http"
|
|
|
|
|
"strconv"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func CreateWidget(widgetService model.WidgetService, authClient model.AuthClient) echo.HandlerFunc {
|
|
|
|
|
return func(request echo.Context) error {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
var body model.CreateWidgetBody
|
|
|
|
|
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 := widgetService.CheckToken(request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error(err.Error())
|
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
widgetID, err := widgetService.CreateWidget(
|
|
|
|
|
ctx,
|
|
|
|
|
model.StreamerID(authData.AccountID),
|
|
|
|
|
body.TemplateID,
|
|
|
|
|
body.Duration,
|
|
|
|
|
body.MinAmount,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error(err.Error())
|
|
|
|
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response := model.CreateWidgetResponse{
|
|
|
|
|
WidgetID: widgetID,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return request.JSON(http.StatusOK, response)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetWidgetHTML(widgetService model.WidgetService) echo.HandlerFunc {
|
|
|
|
|
return func(request echo.Context) error {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
streamerID, err := strconv.Atoi(request.Param("streamerID"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error(err.Error())
|
|
|
|
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
widgetHTML, err := widgetService.GetWidgetHTML(
|
|
|
|
|
ctx,
|
|
|
|
|
model.StreamerID(streamerID),
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error(err.Error())
|
|
|
|
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slog.Info("Get widget HTML successfully")
|
|
|
|
|
return request.HTML(200, string(widgetHTML))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetWidgetInfo(widgetService model.WidgetService) echo.HandlerFunc {
|
|
|
|
|
return func(request echo.Context) error {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
widgetID, err := strconv.Atoi(request.Param("widgetID"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error(err.Error())
|
|
|
|
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
widget, err := widgetService.GetWidgetByID(ctx, model.WidgetID(widgetID))
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error(err.Error())
|
|
|
|
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
}
|
|
|
|
|
if len(widget) == 0 {
|
|
|
|
|
return request.JSON(http.StatusNotFound, "widget not found")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var ResponseWidget = model.WidgetInfoResponse{
|
|
|
|
|
AudioUrl: widget[0].AudioUrl,
|
|
|
|
|
ImageUrl: widget[0].ImageUrl,
|
|
|
|
|
Duration: widget[0].Duration,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return request.JSON(http.StatusOK, ResponseWidget)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SetMediaFile(widgetService model.WidgetService) echo.HandlerFunc {
|
|
|
|
|
return func(request echo.Context) error {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
widgetID, err := strconv.Atoi(request.FormValue("widgetID"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error(err.Error())
|
|
|
|
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mediaType := request.Param("mediaType")
|
|
|
|
|
|
|
|
|
|
file, err := request.FormFile("file")
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error(err.Error())
|
|
|
|
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
src, err := file.Open()
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error(err.Error())
|
|
|
|
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = widgetService.SetMediaFile(
|
|
|
|
|
ctx,
|
|
|
|
|
model.MediaType(mediaType),
|
|
|
|
|
model.WidgetID(widgetID),
|
|
|
|
|
&src,
|
|
|
|
|
file.Filename,
|
|
|
|
|
file.Size,
|
|
|
|
|
"",
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error(err.Error())
|
|
|
|
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
}
|
|
|
|
|
slog.Info("set " + mediaType + " file successfully")
|
|
|
|
|
return request.String(200, "File successfully uploaded")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetMediaFile(widgetService model.WidgetService) echo.HandlerFunc {
|
|
|
|
|
return func(request echo.Context) error {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
mediaType := request.Param("mediaType")
|
|
|
|
|
if mediaType != "background" && mediaType != "image" && mediaType != "audio" {
|
|
|
|
|
slog.Error("Path parameter 'mediaType' is invalid")
|
|
|
|
|
return echo.NewHTTPError(400, "Path parameter 'mediaType' is invalid")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
widgetID, err := strconv.Atoi(request.Param("widgetID"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error(err.Error())
|
|
|
|
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file, err := widgetService.GetMediaFile(
|
|
|
|
|
ctx,
|
|
|
|
|
model.WidgetID(widgetID),
|
|
|
|
|
model.MediaType(mediaType),
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error(err.Error())
|
|
|
|
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slog.Info("get " + mediaType + " file successfully")
|
|
|
|
|
return request.Blob(200, "application/octet-stream", file)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SetMediaUrl(widgetService model.WidgetService) echo.HandlerFunc {
|
|
|
|
|
type Body struct {
|
|
|
|
|
WidgetID model.WidgetID `json:"widgetID" validate:"required"`
|
|
|
|
|
MediaUrl model.MediaUrl `json:"mediaUrl" validate:"required"`
|
|
|
|
|
}
|
|
|
|
|
return func(request echo.Context) error {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
var body Body
|
|
|
|
|
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())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mediaType := request.Param("mediaType")
|
|
|
|
|
|
|
|
|
|
err := widgetService.SetMediaUrl(
|
|
|
|
|
ctx,
|
|
|
|
|
model.MediaType(mediaType),
|
|
|
|
|
body.WidgetID,
|
|
|
|
|
body.MediaUrl,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.Error(err.Error())
|
|
|
|
|
return request.JSON(http.StatusInternalServerError, err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return request.String(200, "Media URL successfully set")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|