From e94777b54b09a93ce447332191aba736dee7ef25 Mon Sep 17 00:00:00 2001 From: Akex Date: Tue, 9 Apr 2024 00:27:11 +0500 Subject: [PATCH 1/2] make controller --- src/Controller/HowDaysToNYController.php | 30 ++++++++++++++++++++++++ src/Repository/HowDaysToNYRepository.php | 19 +++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/Controller/HowDaysToNYController.php create mode 100644 src/Repository/HowDaysToNYRepository.php diff --git a/src/Controller/HowDaysToNYController.php b/src/Controller/HowDaysToNYController.php new file mode 100644 index 0000000..2166b8b --- /dev/null +++ b/src/Controller/HowDaysToNYController.php @@ -0,0 +1,30 @@ +toArray()['date']; + + $return = howDaysToNy(new DateTimeImmutable($date, null)); + + return new JsonResponse($return, Response::HTTP_OK); + } +} diff --git a/src/Repository/HowDaysToNYRepository.php b/src/Repository/HowDaysToNYRepository.php new file mode 100644 index 0000000..90ab47a --- /dev/null +++ b/src/Repository/HowDaysToNYRepository.php @@ -0,0 +1,19 @@ +format('Y') + 1; + $dateOfNY = (new DateTime()) + ->setDate($nextYear, 01, 01); + + return $dateOfNY->diff($date)->format('%a'); +} -- GitLab From b89d0c56b38630706a7a26b2f09b2682bbaa0fff Mon Sep 17 00:00:00 2001 From: "Alex. Plokhikh" Date: Tue, 16 Apr 2024 02:06:52 +0500 Subject: [PATCH 2/2] add action request and controller --- src/Actions/HowDaysToNYAction.php | 20 ++++++++++++++++++++ src/Controller/HowDaysToNYController.php | 17 ++++++----------- src/Controller/SortPriceController.php | 10 ++-------- src/Repository/HowDaysToNYRepository.php | 19 ------------------- src/Requests/BeforeNYDateRequest.php | 19 +++++++++++++++++++ 5 files changed, 47 insertions(+), 38 deletions(-) create mode 100644 src/Actions/HowDaysToNYAction.php delete mode 100644 src/Repository/HowDaysToNYRepository.php create mode 100644 src/Requests/BeforeNYDateRequest.php diff --git a/src/Actions/HowDaysToNYAction.php b/src/Actions/HowDaysToNYAction.php new file mode 100644 index 0000000..3c133ed --- /dev/null +++ b/src/Actions/HowDaysToNYAction.php @@ -0,0 +1,20 @@ +modify('first day of Jan +1 year'); + + return (int) $dateOfNY->diff($date)->format('%a'); + } +} \ No newline at end of file diff --git a/src/Controller/HowDaysToNYController.php b/src/Controller/HowDaysToNYController.php index 2166b8b..8311dce 100644 --- a/src/Controller/HowDaysToNYController.php +++ b/src/Controller/HowDaysToNYController.php @@ -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())); } } diff --git a/src/Controller/SortPriceController.php b/src/Controller/SortPriceController.php index 313d3dc..0f1d250 100644 --- a/src/Controller/SortPriceController.php +++ b/src/Controller/SortPriceController.php @@ -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())); } } diff --git a/src/Repository/HowDaysToNYRepository.php b/src/Repository/HowDaysToNYRepository.php deleted file mode 100644 index 90ab47a..0000000 --- a/src/Repository/HowDaysToNYRepository.php +++ /dev/null @@ -1,19 +0,0 @@ -format('Y') + 1; - $dateOfNY = (new DateTime()) - ->setDate($nextYear, 01, 01); - - return $dateOfNY->diff($date)->format('%a'); -} diff --git a/src/Requests/BeforeNYDateRequest.php b/src/Requests/BeforeNYDateRequest.php new file mode 100644 index 0000000..445bc20 --- /dev/null +++ b/src/Requests/BeforeNYDateRequest.php @@ -0,0 +1,19 @@ +date); + } +} -- GitLab