Skip to content
Snippets Groups Projects
Commit b89d0c56 authored by Александр Плохих's avatar Александр Плохих :waxing_gibbous_moon:
Browse files

add action request and controller

parent e94777b5
No related branches found
No related tags found
1 merge request!5make controller
<?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
...@@ -2,29 +2,24 @@ ...@@ -2,29 +2,24 @@
namespace App\Controller; namespace App\Controller;
use DateTimeImmutable; use App\Actions\HowDaysToNYAction;
use App\Requests\BeforeNYDateRequest;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
use function App\Repository\howDaysToNy;
class HowDaysToNYController extends AbstractController class HowDaysToNYController extends AbstractController
{ {
/** /**
* Контролер рассчитывает кол-во дней до нового года используюя howDaysToNy * Контролер рассчитывает кол-во дней до нового года используюя howDaysToNy
* @param Request $request * @param BeforeNYDateRequest $request
* @param HowDaysToNYAction $action
* @return Response * @return Response
* @throws \Exception
*/ */
#[Route('/howdaystony', name: 'app_how_days_to_n_y')] #[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 new JsonResponse($action->act($request->serialise()));
$return = howDaysToNy(new DateTimeImmutable($date, null));
return new JsonResponse($return, Response::HTTP_OK);
} }
} }
...@@ -3,28 +3,22 @@ ...@@ -3,28 +3,22 @@
namespace App\Controller; namespace App\Controller;
use App\Actions\SortPriceAction; use App\Actions\SortPriceAction;
use App\Entity\PricesEntity;
use App\Requests\PricesRequest; use App\Requests\PricesRequest;
use App\Service\ValidationService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Attribute\Route;
class SortPriceController extends AbstractController class SortPriceController extends AbstractController
{ {
/** /**
* Контроллер волняет сортировку массива по убыванию цены используя sortPrice * Контроллер волняет сортировку массива по убыванию цены используя sortPrice
* @param Request $request * @param PricesRequest $request
* @param SortPriceAction $action * @param SortPriceAction $action
* @return JsonResponse * @return JsonResponse
*/ */
#[Route('/sort/price', name: 'app_sort_price', methods: ['POST'])] #[Route('/sort/price', name: 'app_sort_price', methods: ['POST'])]
public function index(PricesRequest $request, SortPriceAction $action): JsonResponse public function index(PricesRequest $request, SortPriceAction $action): JsonResponse
{ {
return new JsonResponse( return new JsonResponse($action->act($request->serialise()));
$action->act($request->serialise()),
200
);
} }
} }
<?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');
}
<?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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment