Skip to content
HomeController.php 2.08 KiB
Newer Older
Адлан Шамавов's avatar
Адлан Шамавов committed
<?php

namespace App\Controller;

Адлан Шамавов's avatar
Адлан Шамавов committed
use App\Action\Functions;
Адлан Шамавов's avatar
Адлан Шамавов committed
use App\Requests\{
    SortPriceRequest,
    SearchRequest,
    UniqElementsRequest,
Адлан Шамавов's avatar
Адлан Шамавов committed
    MenuRequest,
    HowDaysToNyRequest
Адлан Шамавов's avatar
Адлан Шамавов committed
};
Адлан Шамавов's avatar
Адлан Шамавов committed
use DateTimeImmutable;
Адлан Шамавов's avatar
Адлан Шамавов committed
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class HomeController extends AbstractController
{
Адлан Шамавов's avatar
Адлан Шамавов committed
    public function __construct(private Functions $functions) {}
Адлан Шамавов's avatar
Адлан Шамавов committed

Адлан Шамавов's avatar
Адлан Шамавов committed
    #[Route('/sortPrice', name: 'sortPrice', methods: ['POST'])]
    public function sortPrice(SortPriceRequest $request): Response
Адлан Шамавов's avatar
Адлан Шамавов committed
    {
Адлан Шамавов's avatar
Адлан Шамавов committed
        $array = $this->functions->sortPrice($request->getRequest()->toArray()['items']);
Адлан Шамавов's avatar
Адлан Шамавов committed
        return $this->json($array);
Адлан Шамавов's avatar
Адлан Шамавов committed
    }
Адлан Шамавов's avatar
Адлан Шамавов committed
    #[Route('/search', name: 'search', methods: ['POST'])]
    public function search(SearchRequest $request): Response
Адлан Шамавов's avatar
Адлан Шамавов committed
    {
Адлан Шамавов's avatar
Адлан Шамавов committed
        $array = $request->getRequest()->toArray()['items'];
        $id = $request->getRequest()->query->get('id');
Адлан Шамавов's avatar
Адлан Шамавов committed
        $result = $this->functions->search($array, $id);
        return $this->json($result);
Адлан Шамавов's avatar
Адлан Шамавов committed
    }
Адлан Шамавов's avatar
Адлан Шамавов committed
    #[Route('/uniqElements', name: 'uniqElements', methods: ['POST'])]
    public function uniqElements(UniqElementsRequest $request): Response
Адлан Шамавов's avatar
Адлан Шамавов committed
    {
Адлан Шамавов's avatar
Адлан Шамавов committed
        $result = $this->functions->uniqElements($request->getRequest()->toArray()['items']);
Адлан Шамавов's avatar
Адлан Шамавов committed
        return $this->json($result);
    }

Адлан Шамавов's avatar
Адлан Шамавов committed
    #[Route('/prepareMenu', name: 'prepareMenu', methods: ['POST'])]
    public function prepareMenu(MenuRequest $request): Response
Адлан Шамавов's avatar
Адлан Шамавов committed
    {
Адлан Шамавов's avatar
Адлан Шамавов committed
        $result = $this->functions->prepareMenu($request->getRequest()->toArray()['items']);
Адлан Шамавов's avatar
Адлан Шамавов committed
        return $this->json($result);
Адлан Шамавов's avatar
Адлан Шамавов committed
    }
Адлан Шамавов's avatar
Адлан Шамавов committed
    #[Route('/howDaysToNy', name: 'howDaysToNy', methods: ['GET'])]
    public function howDaysToNy(HowDaysToNyRequest $request): Response
Адлан Шамавов's avatar
Адлан Шамавов committed
    {
Адлан Шамавов's avatar
Адлан Шамавов committed
        $date = $request->getRequest()->get('date');
Адлан Шамавов's avatar
Адлан Шамавов committed
        try {
Адлан Шамавов's avatar
Адлан Шамавов committed
            $result = $this->functions->howDaysToNy(new DateTimeImmutable($date));
Адлан Шамавов's avatar
Адлан Шамавов committed
        } catch (\Exception $e) {
            return new Response($e->getMessage());
        }
        return $this->json(["Days before NY:" => $result]);
Адлан Шамавов's avatar
Адлан Шамавов committed
    }
}