diff --git a/app/src/News/Controller/NewsController.php b/app/src/News/Controller/NewsController.php index 10d45300c9dc0cbbfb255a5dce0e67b9f5ccc3d7..a4bbc7f8290b9641ef8d307c07206fc4389b4465 100644 --- a/app/src/News/Controller/NewsController.php +++ b/app/src/News/Controller/NewsController.php @@ -25,13 +25,13 @@ class NewsController extends AbstractController { public function __construct( private readonly NewsGetListingUseUseCase $getListingUseCase, - private readonly NewsGetDetailUseCase $getDetailNews, - private readonly NewsGetMainUseCase $getMainNews, - private readonly NewsGetDetailMainUseCase $getDetailMainNews, - private readonly NewsCreateUseCase $createNews, - private readonly NewsFullUpdateUseCase $fullUpdate, - private readonly NewsPartUpdateUseCase $partUpdate, - private readonly NewsDeleteUseCase $delete, + private readonly NewsGetDetailUseCase $getDetailUseCase, + private readonly NewsGetMainUseCase $getMainUseCase, + private readonly NewsGetDetailMainUseCase $getDetailMainUseCase, + private readonly NewsCreateUseCase $createUseCase, + private readonly NewsFullUpdateUseCase $fullUpdateUseCase, + private readonly NewsPartUpdateUseCase $partUpdateUseCase, + private readonly NewsDeleteUseCase $deleteUseCase, ) { } @@ -44,42 +44,42 @@ class NewsController extends AbstractController #[Route('/mainNews', name: 'mainNews', methods: ['GET'])] public function mainNews(): JsonResponse { - return $this->getMainNews->execute(); + return $this->getMainUseCase->execute(); } #[Route('/search', name: 'searchNews', methods: ['GET'])] public function detailMainNews(): JsonResponse { - return $this->getDetailMainNews->execute(); + return $this->getDetailMainUseCase->execute(); } #[Route('/{detailId}', name: 'oneNews', methods: ['GET'])] public function oneNews(NewsDetailRequest $request): JsonResponse { - return $this->getDetailNews->execute($request); + return $this->getDetailUseCase->execute($request); } #[Route('/create', name: 'createNews', methods: ['POST'])] public function createNews(NewsCreateRequest $request): Response { - return $this->createNews->execute($request); + return $this->createUseCase->execute($request); } #[Route('/update', name: 'updateNews', methods: ['PUT'])] public function fillUpdateNews(NewsFullUpdateRequest $request): Response { - return $this->fullUpdate->execute($request); + return $this->fullUpdateUseCase->execute($request); } #[Route('/partUpdate', name: 'partUpdateNews', methods: ['PATCH'])] public function partUpdateNews(NewsPartUpdateRequest $request): Response { - return $this->partUpdate->execute($request); + return $this->partUpdateUseCase->execute($request); } #[Route('/delete/{detailId}', name: 'deleteNews', methods: ['DELETE'])] public function deleteNews(NewsDetailRequest $request): Response { - return $this->delete->execute($request); + return $this->deleteUseCase->execute($request); } } diff --git a/app/src/News/Service/NewsPrepareResponseService.php b/app/src/News/Service/NewsPrepareResponseService.php index 24a680e3322681926ce21371ab2a40be45918093..5b8fabada4a7c24158354d7414251853a6492004 100644 --- a/app/src/News/Service/NewsPrepareResponseService.php +++ b/app/src/News/Service/NewsPrepareResponseService.php @@ -32,7 +32,7 @@ class NewsPrepareResponseService private readonly NewsListDtoFactory $listingFactory, private readonly NewsCategoryDtoFactory $categoryFactory, private readonly NewsDetailElementDtoFactory $detailElementFactory, - private readonly NewsEntityFactory $entityFabric, + private readonly NewsEntityFactory $entityFactory, ) { } @@ -101,8 +101,7 @@ class NewsPrepareResponseService return $this->detailElementFactory->create($news); } - public - function unbirth( + public function unbirth( NewsDetailRequest $request ) { $news = $this->news->find($request->detailId); @@ -113,21 +112,21 @@ class NewsPrepareResponseService $this->news->remove($news); } - public function rebirth(NewsPartUpdateRequest $request):void + public function rebirth(NewsPartUpdateRequest $request): void { - $news = $this->entityFabric->hardUpdate($request); + $news = $this->entityFactory->hardUpdate($request); $this->news->createUpdate($news); } public function reborn(NewsFullUpdateRequest $request): void { - $news = $this->entityFabric->softUpdate($request); + $news = $this->entityFactory->softUpdate($request); $this->news->createUpdate($news); } public function birth(NewsCreateRequest $request): void { - $news = $this->entityFabric->create($request); + $news = $this->entityFactory->create($request); $this->news->createUpdate($news); } } diff --git a/app/src/Restaurants/Controller/RestaurantsController.php b/app/src/Restaurants/Controller/RestaurantsController.php index c589e3acfaba4a340b59a12ea0df51bafde7d192..f8a27562d51b806ec067e5d283adfe96ebc0dcf8 100644 --- a/app/src/Restaurants/Controller/RestaurantsController.php +++ b/app/src/Restaurants/Controller/RestaurantsController.php @@ -2,12 +2,20 @@ namespace App\Restaurants\Controller; +use App\Restaurants\Request\RestaurantCreateRequest; +use App\Restaurants\Request\RestaurantFullUpdateRequest; +use App\Restaurants\Request\RestaurantPartUpdateRequest; +use App\Restaurants\UseCase\RestaurantCreateUseCase; +use App\Restaurants\UseCase\RestaurantDeleteUseCase; +use App\Restaurants\UseCase\RestaurantFullUpdateUseCase; use App\Restaurants\UseCase\RestaurantGetListingUseCase; use App\Restaurants\Request\RestaurantDetailRequest; use App\Restaurants\Request\RestaurantListingRequest; use App\Restaurants\UseCase\RestaurantGetDetailUseCase; +use App\Restaurants\UseCase\RestaurantPartUpdateUseCase; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Attribute\Route; #[Route('/api/v1/restaurants')] @@ -16,6 +24,10 @@ class RestaurantsController extends AbstractController public function __construct( private readonly RestaurantGetListingUseCase $getListingUseCase, private readonly RestaurantGetDetailUseCase $getDetailUseCase, + private readonly RestaurantCreateUseCase $createUseCase, + private readonly RestaurantFullUpdateUseCase $fullUpdateUseCase, + private readonly RestaurantDeleteUseCase $deleteUseCase, + private readonly RestaurantPartUpdateUseCase $partUpdateUseCase, ) { } @@ -30,4 +42,28 @@ class RestaurantsController extends AbstractController { return $this->getDetailUseCase->execute($request); } + + #[Route('/create', name: 'createRestaurant', methods: ['POST'])] + public function createRestaurant(RestaurantCreateRequest $request): Response + { + return $this->createUseCase->execute($request); + } + + #[Route('/update', name: 'updateRestaurant', methods: ['PUT'])] + public function fillUpdateRestaurant(RestaurantFullUpdateRequest $request + ): Response { + return $this->fullUpdateUseCase->execute($request); + } + + #[Route('/partUpdate', name: 'partUpdateRestaurant', methods: ['PATCH'])] + public function partUpdateRestaurant(RestaurantPartUpdateRequest $request + ): Response { + return $this->partUpdateUseCase->execute($request); + } + + #[Route('/delete/{detailId}', name: 'deleteRestaurant', methods: ['DELETE'])] + public function deleteRestaurant(RestaurantDetailRequest $request): Response + { + return $this->deleteUseCase->execute($request); + } } diff --git a/app/src/Restaurants/Request/RestaurantCreateRequest.php b/app/src/Restaurants/Request/RestaurantCreateRequest.php index f788e43745775ab0ec7b6b0d2c11309c5aed2689..c35a7bf5575543c84f6740df1a470eddd7ab4178 100644 --- a/app/src/Restaurants/Request/RestaurantCreateRequest.php +++ b/app/src/Restaurants/Request/RestaurantCreateRequest.php @@ -10,6 +10,18 @@ use Symfony\Contracts\Service\Attribute\Required; class RestaurantCreateRequest extends AbstractRequest { + #[Uuid] + public $type_id; + + #[Uuid] + public $settlement_id; + + #[Uuid] + public $preview_image_id; + + #[Uuid] + public $detail_image_id; + #[Type('bool')] #[Required] public $active; @@ -20,48 +32,34 @@ class RestaurantCreateRequest extends AbstractRequest #[Type('string')] #[Required] - public $description; + public $code; #[Type('string')] #[Required] - public $code; + public $description; #[Type('string')] #[Required] - public $receipt; + public $check; #[Type('string')] #[Required] - public $receipt_info; + public $check_info; - #[Type('array')] - #[All( - new Type('string') - )] + + #[Type('string')] #[Required] public $phone; - #[Type('array')] - #[All( - new Type('string') - )] + #[Type('string')] #[Required] public $email; - #[Type('array')] - #[All( - new Type('string') - )] + #[Type('string')] #[Required] public $address; - #[Type('array')] - #[All([ - new Type('array'), - new All( - new Type('string') - ) - ])] + #[Type('string')] #[Required] public $tags; @@ -77,24 +75,15 @@ class RestaurantCreateRequest extends AbstractRequest #[Required] public $how_to_find; - #[Uuid] - public $type_id; - - #[Uuid] - public $settelement_id; - - #[Uuid] - public $preview_image_id; - - #[Uuid] - public $detail_inage_id; - - #[Uuid] + #[Type('array')] + #[All( + new UUid(), + )] public $kitchens_id; #[Type('array')] #[All( new UUid(), )] - public $gallery; -} \ No newline at end of file + public $gallery_id; +} diff --git a/app/src/Restaurants/Request/RestaurantFullUpdateRequest.php b/app/src/Restaurants/Request/RestaurantFullUpdateRequest.php index 74f52f9d365bc15b4d9e3f111905dbc7d8a5205c..7d6cad0d00e7135815df70461d1eaa615b92ef8c 100644 --- a/app/src/Restaurants/Request/RestaurantFullUpdateRequest.php +++ b/app/src/Restaurants/Request/RestaurantFullUpdateRequest.php @@ -14,6 +14,18 @@ class RestaurantFullUpdateRequest extends AbstractRequest #[Required] public $id; + #[Uuid] + public $type_id; + + #[Uuid] + public $settlement_id; + + #[Uuid] + public $preview_image_id; + + #[Uuid] + public $detail_image_id; + #[Type('bool')] #[Required] public $active; @@ -24,48 +36,34 @@ class RestaurantFullUpdateRequest extends AbstractRequest #[Type('string')] #[Required] - public $description; + public $code; #[Type('string')] #[Required] - public $code; + public $description; #[Type('string')] #[Required] - public $receipt; + public $check; #[Type('string')] #[Required] - public $receipt_info; + public $check_info; - #[Type('array')] - #[All( - new Type('string') - )] + + #[Type('string')] #[Required] public $phone; - #[Type('array')] - #[All( - new Type('string') - )] + #[Type('string')] #[Required] public $email; - #[Type('array')] - #[All( - new Type('string') - )] + #[Type('string')] #[Required] public $address; - #[Type('array')] - #[All([ - new Type('array'), - new All( - new Type('string') - ) - ])] + #[Type('string')] #[Required] public $tags; @@ -81,25 +79,15 @@ class RestaurantFullUpdateRequest extends AbstractRequest #[Required] public $how_to_find; - #[Uuid] - public $type_id; - - #[Uuid] - public $settelement_id; - - #[Uuid] - public $preview_image_id; - - #[Uuid] - public $detail_inage_id; - - #[Uuid] + #[Type('array')] + #[All( + new UUid(), + )] public $kitchens_id; - #[Required] #[Type('array')] #[All( new UUid(), )] - public $gallery; + public $gallery_id; } \ No newline at end of file diff --git a/app/src/Restaurants/Request/RestaurantPartUpdateRequest.php b/app/src/Restaurants/Request/RestaurantPartUpdateRequest.php index 21f9c57e6aef1f39541388754a1d1683a98908bc..45a22e1e571c1c22966ac630b15bd631c6f51b67 100644 --- a/app/src/Restaurants/Request/RestaurantPartUpdateRequest.php +++ b/app/src/Restaurants/Request/RestaurantPartUpdateRequest.php @@ -10,8 +10,20 @@ use Symfony\Component\Validator\Constraints\Uuid; class RestaurantPartUpdateRequest extends AbstractRequest { #[Uuid] - public $uuid; - + public $id; + + #[Uuid] + public $type_id; + + #[Uuid] + public $settlement_id; + + #[Uuid] + public $preview_image_id; + + #[Uuid] + public $detail_image_id; + #[Type('bool')] public $active; @@ -19,42 +31,28 @@ class RestaurantPartUpdateRequest extends AbstractRequest public $name; #[Type('string')] - public $description; + public $code; #[Type('string')] - public $code; + public $description; #[Type('string')] public $check; #[Type('string')] - public $receipt_info; + public $check_info; - #[Type('array')] - #[All( - new Type('string') - )] + + #[Type('string')] public $phone; - #[Type('array')] - #[All( - new Type('string') - )] + #[Type('string')] public $email; - #[Type('array')] - #[All( - new Type('string') - )] + #[Type('string')] public $address; - #[Type('array')] - #[All([ - new Type('array'), - new All( - new Type('string') - ) - ])] + #[Type('string')] public $tags; #[Type('string')] @@ -66,24 +64,15 @@ class RestaurantPartUpdateRequest extends AbstractRequest #[Type('string')] public $how_to_find; - #[Uuid] - public $type_id; - - #[Uuid] - public $settelement_id; - - #[Uuid] - public $preview_image_id; - - #[Uuid] - public $detail_inage_id; - - #[Uuid] + #[Type('array')] + #[All( + new UUid(), + )] public $kitchens_id; #[Type('array')] #[All( new UUid(), )] - public $gallery; + public $gallery_id; } \ No newline at end of file diff --git a/app/src/Restaurants/Service/RestaurantPrepareRequestService.php b/app/src/Restaurants/Service/RestaurantPrepareResponseService.php similarity index 70% rename from app/src/Restaurants/Service/RestaurantPrepareRequestService.php rename to app/src/Restaurants/Service/RestaurantPrepareResponseService.php index 9d24a6ad4f53a6050e64f8f80f28dd49c0197b98..1e44bf7b09b68418565f4fbed3ea5a70a5ae3269 100644 --- a/app/src/Restaurants/Service/RestaurantPrepareRequestService.php +++ b/app/src/Restaurants/Service/RestaurantPrepareResponseService.php @@ -10,16 +10,20 @@ use App\Restaurants\DtoFactory\RestaurantFilterVariantsDtoFactory; use App\Restaurants\DtoFactory\RestaurantListDtoFactory; use App\Restaurants\DtoFactory\RestaurantListingElementDtoFactory; use App\Restaurants\DtoFactory\RestaurantTypeDtoFactory; +use App\Restaurants\Request\RestaurantCreateRequest; use App\Restaurants\Request\RestaurantDetailRequest; +use App\Restaurants\Request\RestaurantFullUpdateRequest; use App\Restaurants\Request\RestaurantListingRequest; +use App\Restaurants\Request\RestaurantPartUpdateRequest; use App\Shared\DtoFactory\PaginationDtoFactory; +use App\Shared\EntityFactory\RestaurantEntityFactory; use App\Shared\Error\NotFoundError; use App\Shared\Repository\KitchensRepository; use App\Shared\Repository\RestaurantsRepository; use App\Shared\Repository\RestaurantTypesRepository; use JsonException; -class RestaurantPrepareRequestService +class RestaurantPrepareResponseService { public function __construct( private readonly RestaurantsRepository $restaurants, @@ -32,6 +36,7 @@ class RestaurantPrepareRequestService private readonly KitchenTypeDtoFactory $kitchenFactory, private readonly RestaurantListDtoFactory $listingFactory, private readonly RestaurantDetailElementDtoFactory $detailFactory, + private readonly RestaurantEntityFactory $entityFactory, ) { } @@ -39,7 +44,8 @@ class RestaurantPrepareRequestService * @param RestaurantListingRequest $request * @return RestaurantListDto */ - public function bornListDto(RestaurantListingRequest $request): RestaurantListDto { + public function bornListDto(RestaurantListingRequest $request + ): RestaurantListDto { $countOfRestaurants = $this ->restaurants ->getCountWithFilters( @@ -86,7 +92,9 @@ class RestaurantPrepareRequestService * @throws JsonException */ public - function bornDetailElement(RestaurantDetailRequest $request): RestaurantDetailElementDto { + function bornDetailElement( + RestaurantDetailRequest $request + ): RestaurantDetailElementDto { $restaurant = $this->restaurants->find($request->detailId); if ($restaurant === null) { @@ -95,4 +103,33 @@ class RestaurantPrepareRequestService return $this->detailFactory->create($restaurant); } + + public function unbirth( + RestaurantDetailRequest $request + ) { + $restaurants = $this->restaurants->find($request->detailId); + if ($restaurants === null) { + throw new NotFoundError('Restaurant not found'); + } + + $this->restaurants->remove($restaurants); + } + + public function rebirth(RestaurantPartUpdateRequest $request): void + { + $news = $this->entityFactory->hardUpdate($request); + $this->restaurants->createUpdate($news); + } + + public function reborn(RestaurantFullUpdateRequest $request): void + { + $news = $this->entityFactory->softUpdate($request); + $this->restaurants->createUpdate($news); + } + + public function birth(RestaurantCreateRequest $request): void + { + $news = $this->entityFactory->create($request); + $this->restaurants->createUpdate($news); + } } diff --git a/app/src/Restaurants/UseCase/RestaurantCreateUseCase.php b/app/src/Restaurants/UseCase/RestaurantCreateUseCase.php new file mode 100644 index 0000000000000000000000000000000000000000..4f0a2b599897158443e108fa93d22a668c82772a --- /dev/null +++ b/app/src/Restaurants/UseCase/RestaurantCreateUseCase.php @@ -0,0 +1,27 @@ +prepareRequestService->birth($request); + return new Response(); + } catch (Throwable) { + return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR); + } + } +} diff --git a/app/src/Restaurants/UseCase/RestaurantDeleteUseCase.php b/app/src/Restaurants/UseCase/RestaurantDeleteUseCase.php new file mode 100644 index 0000000000000000000000000000000000000000..f47f24074744aae3cdab5b440208ddeeb980d00c --- /dev/null +++ b/app/src/Restaurants/UseCase/RestaurantDeleteUseCase.php @@ -0,0 +1,27 @@ +prepareRequestService->unbirth($request); + return new Response(); + } catch (Throwable) { + return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR); + } + } +} diff --git a/app/src/Restaurants/UseCase/RestaurantFullUpdateUseCase.php b/app/src/Restaurants/UseCase/RestaurantFullUpdateUseCase.php new file mode 100644 index 0000000000000000000000000000000000000000..0018b0541e2face8186942d633e40251d16b54c8 --- /dev/null +++ b/app/src/Restaurants/UseCase/RestaurantFullUpdateUseCase.php @@ -0,0 +1,27 @@ +prepareRequestService->reborn($request); + return new Response(); + } catch (Throwable) { + return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR); + } + } +} diff --git a/app/src/Restaurants/UseCase/RestaurantGetDetailUseCase.php b/app/src/Restaurants/UseCase/RestaurantGetDetailUseCase.php index e36941547087cac0b72f8c777f3d35bfce79ebca..513478e4a4f7ac60188580b28ce5aad2c8d2d7af 100644 --- a/app/src/Restaurants/UseCase/RestaurantGetDetailUseCase.php +++ b/app/src/Restaurants/UseCase/RestaurantGetDetailUseCase.php @@ -3,7 +3,7 @@ namespace App\Restaurants\UseCase; use App\Restaurants\Request\RestaurantDetailRequest; -use App\Restaurants\Service\RestaurantPrepareRequestService; +use App\Restaurants\Service\RestaurantPrepareResponseService; use App\Shared\DtoFactory\ErrorDtoFactory; use App\Shared\Error\NotFoundError; use Symfony\Component\HttpFoundation\JsonResponse; @@ -13,7 +13,7 @@ use Throwable; class RestaurantGetDetailUseCase { public function __construct( - private readonly RestaurantPrepareRequestService $responsePrepareService, + private readonly RestaurantPrepareResponseService $responsePrepareService, private readonly ErrorDtoFactory $errorFactory, ) { } diff --git a/app/src/Restaurants/UseCase/RestaurantGetListingUseCase.php b/app/src/Restaurants/UseCase/RestaurantGetListingUseCase.php index 2b34a0318b47383f245b3f8d4b7ee87d584dc6fd..1dc52cb19b9def21aa020f8f8c10181511ce1f59 100644 --- a/app/src/Restaurants/UseCase/RestaurantGetListingUseCase.php +++ b/app/src/Restaurants/UseCase/RestaurantGetListingUseCase.php @@ -3,7 +3,7 @@ namespace App\Restaurants\UseCase; use App\Restaurants\Request\RestaurantListingRequest; -use App\Restaurants\Service\RestaurantPrepareRequestService; +use App\Restaurants\Service\RestaurantPrepareResponseService; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Throwable; @@ -11,11 +11,12 @@ use Throwable; class RestaurantGetListingUseCase { public function __construct( - private readonly RestaurantPrepareRequestService $prepareRequestService, + private readonly RestaurantPrepareResponseService $prepareRequestService, ) { } - public function execute(RestaurantListingRequest $request): JsonResponse { + public function execute(RestaurantListingRequest $request): JsonResponse + { try { return new JsonResponse( $this->prepareRequestService->bornListDto($request) diff --git a/app/src/Restaurants/UseCase/RestaurantPartUpdateUseCase.php b/app/src/Restaurants/UseCase/RestaurantPartUpdateUseCase.php new file mode 100644 index 0000000000000000000000000000000000000000..38221e7555cbafe77692e52d5d2e567068df7bbe --- /dev/null +++ b/app/src/Restaurants/UseCase/RestaurantPartUpdateUseCase.php @@ -0,0 +1,29 @@ +prepareRequestService->rebirth($request); + return new Response(); + } catch (Throwable) { + return new JsonResponse([], Response::HTTP_INTERNAL_SERVER_ERROR); + } + } +} diff --git a/app/src/Shared/Entity/Restaurants.php b/app/src/Shared/Entity/Restaurants.php index 7a8e894017cfde271704304877178dd9e1624bb6..444ead42da26e5eb22d63034898d98955518113b 100644 --- a/app/src/Shared/Entity/Restaurants.php +++ b/app/src/Shared/Entity/Restaurants.php @@ -10,6 +10,7 @@ use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: RestaurantsRepository::class)] +#[ORM\HasLifecycleCallbacks] class Restaurants { #[ORM\Id] diff --git a/app/src/Shared/EntityFactory/NewsEntityFactory.php b/app/src/Shared/EntityFactory/NewsEntityFactory.php index abcf0b6be304830a8aba78d9cda4de8a992ff569..c4a84143b0b2f56400e02eea2077040e81cefe0d 100644 --- a/app/src/Shared/EntityFactory/NewsEntityFactory.php +++ b/app/src/Shared/EntityFactory/NewsEntityFactory.php @@ -78,7 +78,7 @@ class NewsEntityFactory $newsSetters = new Collection('callable'); $newsSetters['id'] = function (string $id, News $news) { - $news->setId($id); + $news->setId($id); }; $newsSetters['name'] = function (string $value, News $news) { $news->setName($value); diff --git a/app/src/Shared/EntityFactory/RestaurantEntityFactory.php b/app/src/Shared/EntityFactory/RestaurantEntityFactory.php index a38f1f72a9df08299bd58cb453532e67a8ab1432..e0e8c8a8cd50371abe286fadc5b9ef609c81c516 100644 --- a/app/src/Shared/EntityFactory/RestaurantEntityFactory.php +++ b/app/src/Shared/EntityFactory/RestaurantEntityFactory.php @@ -2,16 +2,194 @@ namespace App\Shared\EntityFactory; +use App\Restaurants\Request\RestaurantCreateRequest; +use App\Restaurants\Request\RestaurantFullUpdateRequest; +use App\Restaurants\Request\RestaurantPartUpdateRequest; +use App\Shared\Entity\Restaurants; +use App\Shared\Error\NotFoundError; +use App\Shared\Repository\FileRepository; +use App\Shared\Repository\KitchensRepository; +use App\Shared\Repository\RestaurantsRepository; +use App\Shared\Repository\RestaurantTypesRepository; +use App\Shared\Repository\SettlementsRepository; use Ramsey\Collection\Collection; +use Ramsey\Uuid\Uuid; class RestaurantEntityFactory { - /** @var Collection */ + /** @var Collection */ private readonly Collection $restaurantSetters; + public function __construct( + private readonly RestaurantsRepository $restaurantsRepository, + private readonly RestaurantTypesRepository $restaurantTypesRepository, + private readonly SettlementsRepository $settlementsRepository, + private readonly FileRepository $fileRepository, + private readonly KitchensRepository $kitchensRepository, + ) { + $this->restaurantSetters = $this->setRestaurantSetters(); + } + + public function create(RestaurantCreateRequest $request): Restaurants + { + $restaurants = new Restaurants(); + $restaurants->setId(Uuid::uuid4()->toString()); + + foreach ($request as $key => $value) { + if ($value !== null) { + $this->restaurantSetters[$key]($value, $restaurants); + } + } + + return $restaurants; + } + + public function softUpdate(RestaurantFullUpdateRequest $request + ): Restaurants { + $restaurants = $this->restaurantsRepository->find( + $request->id + ) ?? new Restaurants(); + + foreach ($request as $key => $value) { + if ($value !== null) { + $this->restaurantSetters[$key]($value, $restaurants); + } + } + + return $restaurants; + } + + public function hardUpdate(RestaurantPartUpdateRequest $request + ): Restaurants { + $restaurant = $this->restaurantsRepository->find($request->id); + + if ($restaurant === null) { + throw new NotFoundError('News entity for update not found'); + } + + foreach ($request as $key => $value) { + if ($value !== null) { + $this->restaurantSetters[$key]($value, $restaurant); + } + } + + return $restaurant; + } + /** @return Collection */ private function setRestaurantSetters(): Collection { + $setters = new Collection('callable'); + + $setters['id'] = function (string $value, Restaurants $restaurant) { + $restaurant->setId($value); + }; + $setters['type_id'] = function ( + string $value, + Restaurants $restaurant + ) { + $restaurant->setType( + $this->restaurantTypesRepository->find($value) + ); + }; + $setters['settlement_id'] = function ( + string $value, + Restaurants $restaurant + ) { + $restaurant->setSettlement( + $this->settlementsRepository->find($value) + ); + }; + $setters['preview_image_id'] = function ( + string $value, + Restaurants $restaurant + ) { + $restaurant->setPreviewImage($this->fileRepository->find($value)); + }; + $setters['detail_image_id'] = function ( + string $value, + Restaurants $restaurant + ) { + $restaurant->setDetailImage($this->fileRepository->find($value)); + }; + $setters['active'] = function (bool $value, Restaurants $restaurant) { + $restaurant->setActive($value); + }; + $setters['name'] = function (string $value, Restaurants $restaurant) { + $restaurant->setName($value); + }; + $setters['code'] = function (string $value, Restaurants $restaurant) { + $restaurant->setCode($value); + }; + $setters['description'] = function ( + string $value, + Restaurants $restaurant + ) { + $restaurant->setDescription($value); + }; + $setters['check'] = function (string $value, Restaurants $restaurant) { + $restaurant->setReceipt($value); + }; + $setters['check_info'] = function ( + string $value, + Restaurants $restaurant + ) { + $restaurant->setReceiptInfo($value); + }; + $setters['phone'] = function (string $value, Restaurants $restaurant) { + $restaurant->setPhone($value); + }; + $setters['email'] = function (string $value, Restaurants $restaurant) { + $restaurant->setEmail($value); + }; + $setters['address'] = function ( + string $value, + Restaurants $restaurant + ) { + $restaurant->setAddress($value); + }; + $setters['tags'] = function ( + string $value, + Restaurants $restaurant + ) { + $restaurant->setTags($value); + }; + $setters['site'] = function (string $value, Restaurants $restaurant) { + $restaurant->setSite($value); + }; + $setters['coordinates'] = function ( + string $value, + Restaurants $restaurant + ) { + $restaurant->setCoordinates($value); + }; + $setters['how_to_find'] = function ( + string $value, + Restaurants $restaurant + ) { + $restaurant->setHowToFind($value); + }; + $setters['kitchens_id'] = function ( + Collection $ids, + Restaurants $restaurant + ) { + foreach ($ids as $id) { + $restaurant->addKitchen( + $this->kitchensRepository->find($id) + ); + } + }; + $setters['gallery_id'] = function ( + Collection $ids, + Restaurants $restaurant + ) { + foreach ($ids as $id) { + $restaurant->addGallery( + $this->fileRepository->find($id) + ); + } + }; + return $setters; } } \ No newline at end of file diff --git a/app/src/Shared/Repository/NewsRepository.php b/app/src/Shared/Repository/NewsRepository.php index 4b55634282513c354fd9bfeacdaf71ac77d669d6..d7b05bb576403e7fae4712e5dde81a5feaf334f0 100644 --- a/app/src/Shared/Repository/NewsRepository.php +++ b/app/src/Shared/Repository/NewsRepository.php @@ -39,7 +39,7 @@ class NewsRepository extends ServiceEntityRepository $this->getEntityManager()->clear(); } - public function getMaxValueSort(): int + public function getMaxValueSort(): ?int { return $this->createQueryBuilder('n') ->select('MAX(n.sort)') diff --git a/app/src/Shared/Repository/RestaurantsRepository.php b/app/src/Shared/Repository/RestaurantsRepository.php index 5bbf103e070b71c18cc3c0e37f3649ea0898b732..ab72e3ef43de8a159c81efe96664dc067435f88a 100644 --- a/app/src/Shared/Repository/RestaurantsRepository.php +++ b/app/src/Shared/Repository/RestaurantsRepository.php @@ -24,7 +24,21 @@ class RestaurantsRepository extends ServiceEntityRepository parent::__construct($registry, Restaurants::class); } - public function getMaxValueSort(): int + public function createUpdate(Restaurants $restaurants): void + { + $this->getEntityManager()->persist($restaurants); + $this->getEntityManager()->flush(); + $this->getEntityManager()->clear(); + } + + public function remove(Restaurants $restaurants): void + { + $this->getEntityManager()->remove($restaurants); + $this->getEntityManager()->flush(); + $this->getEntityManager()->clear(); + } + + public function getMaxValueSort(): ?int { return $this->createQueryBuilder('r') ->select('MAX(r.sort)')