72 lines
2.0 KiB
Go
72 lines
2.0 KiB
Go
package controller
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gofiber/fiber/v2"
|
|
"gitea.pena/SQuiz/shutterstock/internal/client"
|
|
"gitea.pena/SQuiz/shutterstock/internal/model"
|
|
)
|
|
|
|
type ShutterStockController struct {
|
|
shutterStockClient *client.ShutterStockClient
|
|
}
|
|
|
|
func NewShutterStockController(shutterStockClient *client.ShutterStockClient) *ShutterStockController {
|
|
return &ShutterStockController{
|
|
shutterStockClient: shutterStockClient,
|
|
}
|
|
}
|
|
|
|
func (s *ShutterStockController) Register(router fiber.Router) {
|
|
router.Get("/", s.GetShutterStockData)
|
|
}
|
|
|
|
func (s *ShutterStockController) Name() string {
|
|
return ""
|
|
}
|
|
|
|
func (s *ShutterStockController) GetShutterStockData(ctx *fiber.Ctx) error {
|
|
var request model.ImgSearchReq
|
|
if err := ctx.BodyParser(&request); err != nil {
|
|
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
|
|
params := make(map[model.ShutterStockParams]string)
|
|
|
|
if request.Query != "" {
|
|
params[model.Query] = request.Query
|
|
}
|
|
if request.AspectRatio > 0 {
|
|
params[model.AspectRatio] = fmt.Sprintf("%f", request.AspectRatio)
|
|
}
|
|
if request.AspectRatioMax > 0 {
|
|
params[model.AspectRatioMax] = fmt.Sprintf("%f", request.AspectRatioMax)
|
|
}
|
|
if request.AspectRatioMin > 0 {
|
|
params[model.AspectRatioMin] = fmt.Sprintf("%f", request.AspectRatioMin)
|
|
}
|
|
if request.WidthFrom > 0 {
|
|
params[model.WidthFrom] = fmt.Sprintf("%d", request.WidthFrom)
|
|
}
|
|
if request.WidthTo > 0 {
|
|
params[model.WidthTo] = fmt.Sprintf("%d", request.WidthTo)
|
|
}
|
|
if request.HeightFrom > 0 {
|
|
params[model.HeightFrom] = fmt.Sprintf("%d", request.HeightFrom)
|
|
}
|
|
if request.HeightTo > 0 {
|
|
params[model.HeightTo] = fmt.Sprintf("%d", request.HeightTo)
|
|
}
|
|
|
|
if len(params) == 0 {
|
|
return ctx.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "need one or more params in request"})
|
|
}
|
|
|
|
resp, err := s.shutterStockClient.ImgSearch(params)
|
|
if err != nil {
|
|
return ctx.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
|
|
return ctx.Status(fiber.StatusOK).JSON(resp)
|
|
}
|