From 9d38d4590ee69597bac91e2912043bb854058346 Mon Sep 17 00:00:00 2001 From: "Alex. Plokhikh" Date: Mon, 15 Apr 2024 12:32:12 +0500 Subject: [PATCH 1/6] create interfaces | create VlidationService --- src/Actions/InterfaceAction.php | 14 ++++++++++ src/Entity/InterfaceDataEntity.php | 15 +++++++++++ src/Service/ValidationService.php | 41 ++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 src/Actions/InterfaceAction.php create mode 100644 src/Entity/InterfaceDataEntity.php create mode 100644 src/Service/ValidationService.php diff --git a/src/Actions/InterfaceAction.php b/src/Actions/InterfaceAction.php new file mode 100644 index 0000000..f4dcce0 --- /dev/null +++ b/src/Actions/InterfaceAction.php @@ -0,0 +1,14 @@ +validate($model); + + $messages = [ + 'message' => 'validation_fail', + 'errors' => [], + ]; + + foreach ($errors as $error) { + $messages['errors'][] = [ + 'property' => $error->getPropertyPath(), + 'value' => $error->getInvalidValue(), + 'message' => $error->getMessage(), + ]; + } + + if (count($messages['errors']) > 0) { + $response = new JsonResponse($messages, 400); + $response->send(); + + exit; + } + } +} \ No newline at end of file -- GitLab From a430fd5a1942cad015efbcb49e226343f6616163 Mon Sep 17 00:00:00 2001 From: "Alex. Plokhikh" Date: Mon, 15 Apr 2024 12:41:06 +0500 Subject: [PATCH 2/6] create prices model sort action and controller --- src/Actions/SortPriceAction.php | 31 ++++++++++++++++++++ src/Controller/SortPriceController.php | 32 +++++++++++++++++++++ src/Entity/PricesEntity.php | 39 ++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/Actions/SortPriceAction.php create mode 100644 src/Controller/SortPriceController.php create mode 100644 src/Entity/PricesEntity.php diff --git a/src/Actions/SortPriceAction.php b/src/Actions/SortPriceAction.php new file mode 100644 index 0000000..13d2023 --- /dev/null +++ b/src/Actions/SortPriceAction.php @@ -0,0 +1,31 @@ +prices; + + $priceColumn = array_column($array, "price"); + $countColumn = array_column($array, "count"); + + array_multisort( + $priceColumn, + SORT_DESC, + $countColumn, + SORT_ASC, + $array, + ); + + return $array; + } +} diff --git a/src/Controller/SortPriceController.php b/src/Controller/SortPriceController.php new file mode 100644 index 0000000..417b461 --- /dev/null +++ b/src/Controller/SortPriceController.php @@ -0,0 +1,32 @@ +serialise($request); + + $validation = new ValidationService(); + $validation->validate($priceEntity); + + return new JsonResponse($action->act($priceEntity)); + } +} diff --git a/src/Entity/PricesEntity.php b/src/Entity/PricesEntity.php new file mode 100644 index 0000000..2b3257f --- /dev/null +++ b/src/Entity/PricesEntity.php @@ -0,0 +1,39 @@ + [ + new NotBlank(), + new Type('integer'), + ], + 'count' => [ + new NotBlank(), + new Type('integer'), + ], + ], + ) + ] + )] + public $prices; + + public function serialise(Request $request): void + { + $this->prices = $request->toArray()['prices']; + } +} -- GitLab From 2b3fa0476b632a2daaa670ff5c466677e9a100d1 Mon Sep 17 00:00:00 2001 From: "Alex. Plokhikh" Date: Mon, 15 Apr 2024 15:29:30 +0500 Subject: [PATCH 3/6] sumplify solution --- src/Actions/InterfaceAction.php | 14 ---------- src/Entity/InterfaceDataEntity.php | 15 ----------- src/Entity/PricesEntity.php | 39 ---------------------------- src/Service/ValidationService.php | 41 ------------------------------ 4 files changed, 109 deletions(-) delete mode 100644 src/Actions/InterfaceAction.php delete mode 100644 src/Entity/InterfaceDataEntity.php delete mode 100644 src/Entity/PricesEntity.php delete mode 100644 src/Service/ValidationService.php diff --git a/src/Actions/InterfaceAction.php b/src/Actions/InterfaceAction.php deleted file mode 100644 index f4dcce0..0000000 --- a/src/Actions/InterfaceAction.php +++ /dev/null @@ -1,14 +0,0 @@ - [ - new NotBlank(), - new Type('integer'), - ], - 'count' => [ - new NotBlank(), - new Type('integer'), - ], - ], - ) - ] - )] - public $prices; - - public function serialise(Request $request): void - { - $this->prices = $request->toArray()['prices']; - } -} diff --git a/src/Service/ValidationService.php b/src/Service/ValidationService.php deleted file mode 100644 index cd9df8c..0000000 --- a/src/Service/ValidationService.php +++ /dev/null @@ -1,41 +0,0 @@ -validate($model); - - $messages = [ - 'message' => 'validation_fail', - 'errors' => [], - ]; - - foreach ($errors as $error) { - $messages['errors'][] = [ - 'property' => $error->getPropertyPath(), - 'value' => $error->getInvalidValue(), - 'message' => $error->getMessage(), - ]; - } - - if (count($messages['errors']) > 0) { - $response = new JsonResponse($messages, 400); - $response->send(); - - exit; - } - } -} \ No newline at end of file -- GitLab From 4ebf14cec56410c9a9779f69f957ef60a5e141bc Mon Sep 17 00:00:00 2001 From: "Alex. Plokhikh" Date: Mon, 15 Apr 2024 15:29:54 +0500 Subject: [PATCH 4/6] symplify controller --- src/Actions/SortPriceAction.php | 10 ++-- src/Controller/SortPriceController.php | 14 +++-- src/Requests/BaseRequest.php | 71 ++++++++++++++++++++++++++ src/Requests/PricesRequest.php | 42 +++++++++++++++ templates/id_search/index.html.twig | 20 ++++++++ 5 files changed, 142 insertions(+), 15 deletions(-) create mode 100644 src/Requests/BaseRequest.php create mode 100644 src/Requests/PricesRequest.php create mode 100644 templates/id_search/index.html.twig diff --git a/src/Actions/SortPriceAction.php b/src/Actions/SortPriceAction.php index 13d2023..067f67e 100644 --- a/src/Actions/SortPriceAction.php +++ b/src/Actions/SortPriceAction.php @@ -2,19 +2,15 @@ namespace App\Actions; -use App\Entity\InterfaceDataEntity; - -class SortPriceAction implements InterfaceAction +class SortPriceAction { /** * Выполняет сортировку массива по убыванию цены - * @param InterfaceDataEntity $model + * @param array $array * @return array отсортированный */ - public function act(InterfaceDataEntity $model): array + public function act(array $array): array { - $array = $model->prices; - $priceColumn = array_column($array, "price"); $countColumn = array_column($array, "count"); diff --git a/src/Controller/SortPriceController.php b/src/Controller/SortPriceController.php index 417b461..313d3dc 100644 --- a/src/Controller/SortPriceController.php +++ b/src/Controller/SortPriceController.php @@ -4,6 +4,7 @@ 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; @@ -19,14 +20,11 @@ class SortPriceController extends AbstractController * @return JsonResponse */ #[Route('/sort/price', name: 'app_sort_price', methods: ['POST'])] - public function index(Request $request, SortPriceAction $action): JsonResponse + public function index(PricesRequest $request, SortPriceAction $action): JsonResponse { - $priceEntity = new PricesEntity(); - $priceEntity->serialise($request); - - $validation = new ValidationService(); - $validation->validate($priceEntity); - - return new JsonResponse($action->act($priceEntity)); + return new JsonResponse( + $action->act($request->serialise()), + 200 + ); } } diff --git a/src/Requests/BaseRequest.php b/src/Requests/BaseRequest.php new file mode 100644 index 0000000..b2fb49e --- /dev/null +++ b/src/Requests/BaseRequest.php @@ -0,0 +1,71 @@ +populate(); + + if (self::AUTO_VALIDATE) { + $this->validate(); + } + } + + protected function populate(): void + { + foreach ($this->getRequest()->toArray() as $property => $value) { + if (property_exists($this, $property)) { + $this->{$property} = $value; + } + } + } + + /** + * валидация и выброкса ошибки при валидации + * @return void + */ + public function validate() + { + $errors = $this->validator->validate($this); + + $messages = [ + 'message' => 'validation_failed', + 'errors' => [] + ]; + + foreach ($errors as $error) { + $messages['errors'][] = [ + 'property' => $error->getPropertyPath(), + 'value' => $error->getInvalidValue(), + 'message' => $error->getMessage(), + ]; + } + + if (count($messages['errors']) > 0) { + $response = new JsonResponse($messages, 201); + $response->send(); + + throw new ValidatorException('Validation failed', $messages); + } + } + + public function getRequest(): Request + { + return Request::createFromGlobals(); + } + + abstract public function serialise(): mixed; +} \ No newline at end of file diff --git a/src/Requests/PricesRequest.php b/src/Requests/PricesRequest.php new file mode 100644 index 0000000..dadac42 --- /dev/null +++ b/src/Requests/PricesRequest.php @@ -0,0 +1,42 @@ + [ + new NotBlank(), + new Type('integer'), + ], + 'count' => [ + new NotBlank(), + new Type('integer'), + ], + ], + ) + ] + )] + public $prices; + + /** + * серализация реквеста под массив + * @return mixed + */ + public function serialise(): mixed + { + return $this->prices; + } +} diff --git a/templates/id_search/index.html.twig b/templates/id_search/index.html.twig new file mode 100644 index 0000000..69a280e --- /dev/null +++ b/templates/id_search/index.html.twig @@ -0,0 +1,20 @@ +{% extends 'base.html.twig' %} + +{% block title %}Hello IdSearchController!{% endblock %} + +{% block body %} + + +
+

Hello {{ controller_name }}! ✅

+ + This friendly message is coming from: +
    +
  • Your controller at /home/tamanit/myProj/iqdevTranningProgram/src/Controller/IdSearchController.php
  • +
  • Your template at /home/tamanit/myProj/iqdevTranningProgram/templates/id_search/index.html.twig
  • +
+
+{% endblock %} -- GitLab From b0fd1077c9f3bf2182df70c7ae52c0b4cb947eed Mon Sep 17 00:00:00 2001 From: "Alex. Plokhikh" Date: Tue, 16 Apr 2024 00:58:58 +0500 Subject: [PATCH 5/6] rm manula http status init in response --- src/Controller/SortPriceController.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Controller/SortPriceController.php b/src/Controller/SortPriceController.php index 313d3dc..9f8a45d 100644 --- a/src/Controller/SortPriceController.php +++ b/src/Controller/SortPriceController.php @@ -22,9 +22,6 @@ class SortPriceController extends AbstractController #[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())); } } -- GitLab From 295a63428ac2d0a8995a0828b09a7e50221ad16e Mon Sep 17 00:00:00 2001 From: "Alex. Plokhikh" Date: Tue, 16 Apr 2024 01:20:11 +0500 Subject: [PATCH 6/6] rm useles use --- src/Controller/SortPriceController.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Controller/SortPriceController.php b/src/Controller/SortPriceController.php index 9f8a45d..0f1d250 100644 --- a/src/Controller/SortPriceController.php +++ b/src/Controller/SortPriceController.php @@ -3,19 +3,16 @@ 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 */ -- GitLab