Skip to content
HomeController.php 2.28 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,
    MenuRequest
};
Адлан Шамавов'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/{day}/{month}/{year}',
        name: 'howDaysToNy',
        requirements: [
            'day' => '(?<!-)(?<!\d)\d+',
            'month' => '(?<!-)(?<!\d)\d+',
            'year' => '(?<!-)(?<!\d)\d+'
        ],
        methods: ['GET']
    )]
    public function howDaysToNy(int $day, int $month, int $year): Response
Адлан Шамавов's avatar
Адлан Шамавов committed
    {
        $dateAsString = $year . "-" . $month . "-" . $day;
        try {
            $result = $this->functions->howDaysToNy(new DateTimeImmutable($dateAsString));
        } catch (\Exception $e) {
            return new Response($e->getMessage());
        }
        return $this->json(["Days before NY:" => $result]);
Адлан Шамавов's avatar
Адлан Шамавов committed
    }
}