From 97d69d2837f03fab5b97f886e216ade4dd4a004b Mon Sep 17 00:00:00 2001 From: AlexP Date: Fri, 17 May 2024 15:40:12 +0500 Subject: [PATCH] STA-1046 | reqyiered requests for fulRestApi news & restaurants --- app/composer.json | 1 + app/src/News/Request/NewsCreateRequest.php | 50 +++++++++ .../News/Request/NewsFullUpdateRequest.php | 54 +++++++++ .../News/Request/NewsPartUpdateRequest.php | 48 ++++++++ .../Request/RestaurantCreateRequest.php | 100 +++++++++++++++++ .../Request/RestaurantFullUpdateRequest.php | 105 ++++++++++++++++++ .../Request/RestaurantPartUpdateRequest.php | 90 +++++++++++++++ .../Shared/Abstraction/AbstractRequest.php | 62 +++++++++++ .../Collection/ValidationErrorCollection.php | 16 +++ 9 files changed, 526 insertions(+) create mode 100644 app/src/News/Request/NewsCreateRequest.php create mode 100644 app/src/News/Request/NewsFullUpdateRequest.php create mode 100644 app/src/News/Request/NewsPartUpdateRequest.php create mode 100644 app/src/Restaurants/Request/RestaurantCreateRequest.php create mode 100644 app/src/Restaurants/Request/RestaurantFullUpdateRequest.php create mode 100644 app/src/Restaurants/Request/RestaurantPartUpdateRequest.php create mode 100644 app/src/Shared/Abstraction/AbstractRequest.php create mode 100644 app/src/Shared/Collection/ValidationErrorCollection.php diff --git a/app/composer.json b/app/composer.json index be399e9..a785c5a 100644 --- a/app/composer.json +++ b/app/composer.json @@ -13,6 +13,7 @@ "doctrine/orm": "^3.1", "phpdocumentor/reflection-docblock": "^5.3", "phpstan/phpdoc-parser": "^1.28", + "ramsey/uuid": "^4.7", "symfony/asset": "6.4.*", "symfony/asset-mapper": "6.4.*", "symfony/console": "6.4.*", diff --git a/app/src/News/Request/NewsCreateRequest.php b/app/src/News/Request/NewsCreateRequest.php new file mode 100644 index 0000000..084d874 --- /dev/null +++ b/app/src/News/Request/NewsCreateRequest.php @@ -0,0 +1,50 @@ + */ + #[All( + new Uuid() + )] + public $categories_uuid; + + #[Uuid] + public $detail_image; + + #[Uuid] + public $preview_image; +} diff --git a/app/src/News/Request/NewsPartUpdateRequest.php b/app/src/News/Request/NewsPartUpdateRequest.php new file mode 100644 index 0000000..b0a1423 --- /dev/null +++ b/app/src/News/Request/NewsPartUpdateRequest.php @@ -0,0 +1,48 @@ +populate(); + + if ($this->autoValidate) { + $this->validate(); + } + } + + protected function populate(): void + { + foreach ($this->getRequest()->toArray() as $property => $value) { + if (property_exists($this, $property)) { + $this->{$property} = $value; + } + } + } + + public function validate(): void + { + $errors = $this->validator->validate($this); + + $messages = new ValidationErrorCollection(); + + foreach ($errors as $error) { + $messages->add($this->errorDtoFactory->create($error)); + } + + if ($messages->count() > 0) { + $response = new JsonResponse($messages, 422); + $response->send(); + + throw new ValidatorException('Validation failed'); + } + } + + public function getRequest(): Request + { + return Request::createFromGlobals(); + } +} \ No newline at end of file diff --git a/app/src/Shared/Collection/ValidationErrorCollection.php b/app/src/Shared/Collection/ValidationErrorCollection.php new file mode 100644 index 0000000..346ae1a --- /dev/null +++ b/app/src/Shared/Collection/ValidationErrorCollection.php @@ -0,0 +1,16 @@ +collectionType, $data); + } +} \ No newline at end of file -- GitLab