Commit b89d0c56 authored by Александр Плохих's avatar Александр Плохих 🌔
Browse files

add action request and controller

parent e94777b5
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
<?php

namespace App\Actions;

use DateTimeImmutable;

class HowDaysToNYAction
{
    /**
     * Функция рассчитывает кол-во дней до нового года
     * @param DateTime $date дата от которой, необходимо рассчитать кол-во дней
     * @return int
     */
    public function act(DateTimeImmutable $date): int
    {
        $dateOfNY = $date->modify('first day of Jan +1 year');

        return (int) $dateOfNY->diff($date)->format('%a');
    }
}
 No newline at end of file
+6 −11
Original line number Diff line number Diff line
@@ -2,29 +2,24 @@

namespace App\Controller;

use DateTimeImmutable;
use App\Actions\HowDaysToNYAction;
use App\Requests\BeforeNYDateRequest;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use function App\Repository\howDaysToNy;

class HowDaysToNYController extends AbstractController
{
    /**
     * Контролер рассчитывает кол-во дней до нового года используюя howDaysToNy
     * @param Request $request
     * @param BeforeNYDateRequest $request
     * @param HowDaysToNYAction $action
     * @return Response
     * @throws \Exception
     */
    #[Route('/howdaystony', name: 'app_how_days_to_n_y')]
    public function index(Request $request): Response
    public function index(BeforeNYDateRequest $request, HowDaysToNYAction $action): Response
    {
        $date = $request->toArray()['date'];

        $return = howDaysToNy(new DateTimeImmutable($date, null));

        return new JsonResponse($return, Response::HTTP_OK);
        return new JsonResponse($action->act($request->serialise()));
    }
}
+2 −8
Original line number Diff line number Diff line
@@ -3,28 +3,22 @@
namespace App\Controller;

use App\Actions\SortPriceAction;
use App\Entity\PricesEntity;
use App\Requests\PricesRequest;
use App\Service\ValidationService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;

class SortPriceController extends AbstractController
{
    /**
     * Контроллер волняет сортировку массива по убыванию цены используя sortPrice
     * @param Request $request
     * @param PricesRequest $request
     * @param SortPriceAction $action
     * @return JsonResponse
     */
    #[Route('/sort/price', name: 'app_sort_price', methods: ['POST'])]
    public function index(PricesRequest $request, SortPriceAction $action): JsonResponse
    {
        return new JsonResponse(
            $action->act($request->serialise()),
            200
        );
        return new JsonResponse($action->act($request->serialise()));
    }
}
+0 −19
Original line number Diff line number Diff line
<?php
namespace App\Repository;

use DateTime;
use DateTimeImmutable;

/**
 * Функция рассчитывает кол-во дней до нового года
 * @param DateTimeImmutable $date дата от которой, необходимо рассчитать кол-во дней
 * @return int
 */
function howDaysToNy(DateTimeImmutable $date): int {

    $nextYear = (int)$date->format('Y') + 1;
    $dateOfNY = (new DateTime())
        ->setDate($nextYear, 01, 01);

    return $dateOfNY->diff($date)->format('%a');
}
+19 −0
Original line number Diff line number Diff line
<?php

namespace App\Requests;

use DateTimeImmutable;
use Symfony\Component\Validator\Constraints\Date;
use Symfony\Component\Validator\Constraints\Type;

class BeforeNYDateRequest extends BaseRequest
{
    #[Date()]
    #[Type('string')]
    public $date;

    public function serialise(): mixed
    {
        return new DateTimeImmutable($this->date);
    }
}