diff --git a/src/ArgumentResolver/RequestBodyArgumentResolver.php b/src/ArgumentResolver/RequestBodyArgumentResolver.php new file mode 100644 index 0000000000000000000000000000000000000000..9e86ed021bb35eecb186cc42d35e74cf36865ae3 --- /dev/null +++ b/src/ArgumentResolver/RequestBodyArgumentResolver.php @@ -0,0 +1,46 @@ +getAttributesOfType(RequestBody::class, ArgumentMetadata::IS_INSTANCEOF)) { + return []; + } + + try { + $model = $this->serializer->deserialize( + $request->getContent(), + $argument->getType(), + JsonEncoder::FORMAT + ); + } catch (\Throwable $throwable) { + throw new RequestBodyConvertException($throwable->getMessage()); + } + + $errors = $this->validator->validate($model); + if (count($errors) > 0) { + throw new ValidationException($errors); + } + + return [$model]; + } +} \ No newline at end of file diff --git a/src/Attribute/RequestBody.php b/src/Attribute/RequestBody.php new file mode 100644 index 0000000000000000000000000000000000000000..34dc62c75047550eb7c803d31971b69f9ed80d7c --- /dev/null +++ b/src/Attribute/RequestBody.php @@ -0,0 +1,11 @@ +getCode()); } } + + #[Route('/news', name: 'addNews', methods: ['POST'])] + public function addNews( + #[RequestBody] CreateNewsRequest $request + ): Response { + return $this->json($this->newsService->addNewsByRequest($request)); + } + + #[Route('/news/{id}', name: 'putNews', methods: ['PUT'])] + public function putNews( + string $id, + #[RequestBody] EditNewsRequest $request + ): Response { + return $this->json($this->newsService->putNewsByRequest($id, $request)); + } + + #[Route('/news/{id}', name: 'patchNews', methods: ['PATCH'])] + public function patchNews( + string $id, + #[RequestBody] EditNewsRequest $request + ): Response { + return $this->json($this->newsService->patchNewsByRequest($id, $request)); + } + + #[Route('/news/{id}', name: 'deleteNews', methods: ['DELETE'])] + public function deleteNews(string $id): Response + { + $this->newsService->deleteNews($id); + return $this->json(null); + } } diff --git a/src/Controller/RestaurantController.php b/src/Controller/RestaurantController.php index 8f842cb2cac290ea6645ee58ffe50422a17844c3..7761231ebc62c4233bbb1dffbeeb832190ac5fc9 100644 --- a/src/Controller/RestaurantController.php +++ b/src/Controller/RestaurantController.php @@ -4,7 +4,10 @@ declare(strict_types=1); namespace App\Controller; +use App\Attribute\RequestBody; use App\Exception\RestaurantNotFoundException; +use App\Requests\CreateRestaurantRequest; +use App\Requests\EditRestaurantRequest; use App\Requests\RestaurantListRequest; use App\Service\RestaurantService; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; @@ -39,4 +42,34 @@ class RestaurantController extends AbstractController ], $e->getCode()); } } + + #[Route('/restaurant', name: 'addRestaurant', methods: ['POST'])] + public function addRestaurant( + #[RequestBody] CreateRestaurantRequest $request + ): Response { + return $this->json($this->restaurantService->addRestaurantByRequest($request)); + } + + #[Route('/restaurant/{id}', name: 'putRestaurant', methods: ['PUT'])] + public function putRestaurant( + string $id, + #[RequestBody] EditRestaurantRequest $request + ): Response { + return $this->json($this->restaurantService->putRestaurantByRequest($id, $request)); + } + + #[Route('/restaurant/{id}', name: 'patchRestaurant', methods: ['PATCH'])] + public function patchRestaurant( + string $id, + #[RequestBody] EditRestaurantRequest $request + ): Response { + return $this->json($this->restaurantService->patchRestaurantByRequest($id, $request)); + } + + #[Route('/restaurant/{id}', name: 'deleteRestaurant', methods: ['DELETE'])] + public function deleteRestaurant(string $id): Response + { + $this->restaurantService->deleteRestaurant($id); + return $this->json(null); + } } diff --git a/src/Entity/News.php b/src/Entity/News.php index 1cca2ff66661a948db65dedc90200d8a477b6716..2458122f1dea3aa268665264c40e89b65f0a86dd 100644 --- a/src/Entity/News.php +++ b/src/Entity/News.php @@ -63,7 +63,7 @@ class News #[ORM\JoinColumn(nullable: false)] private ?Seo $seo = null; - #[ORM\OneToOne(cascade: ['persist', 'remove'])] + #[ORM\OneToOne] #[ORM\JoinColumn(nullable: false)] private ?File $file = null; diff --git a/src/Entity/Restaurant.php b/src/Entity/Restaurant.php index ff7d311aba42895e808972b04f3ac1dbf2d1e66d..3783fc94657aa4a8a84070c69ac978882a49732f 100644 --- a/src/Entity/Restaurant.php +++ b/src/Entity/Restaurant.php @@ -94,7 +94,7 @@ class Restaurant #[ORM\JoinColumn(nullable: false)] private ?Seo $seo = null; - #[ORM\OneToOne(cascade: ['persist', 'remove'], fetch: 'EAGER')] + #[ORM\OneToOne(fetch: 'EAGER')] #[ORM\JoinColumn(nullable: false)] private ?File $file = null; @@ -109,6 +109,12 @@ class Restaurant return $this->id; } + public function setId(Uuid $id): self + { + $this->id = $id; + return $this; + } + public function getName(): ?string { return $this->name; diff --git a/src/Exception/ErrorCodeEnum.php b/src/Exception/ErrorCodeEnum.php new file mode 100644 index 0000000000000000000000000000000000000000..cc4948bfeac220b663ee006b759cafd80353c325 --- /dev/null +++ b/src/Exception/ErrorCodeEnum.php @@ -0,0 +1,12 @@ +value); + } +} \ No newline at end of file diff --git a/src/Exception/NewsExceptionEnum.php b/src/Exception/NewsExceptionEnum.php deleted file mode 100644 index 38c5c28b8508ce83ff0cfbb7c942eb77a7882738..0000000000000000000000000000000000000000 --- a/src/Exception/NewsExceptionEnum.php +++ /dev/null @@ -1,10 +0,0 @@ -value); + parent::__construct("News not found", ErrorCodeEnum::NotFound->value); } } \ No newline at end of file diff --git a/src/Exception/NewsTypeNotFoundException.php b/src/Exception/NewsTypeNotFoundException.php new file mode 100644 index 0000000000000000000000000000000000000000..0d4505fc5d4ebea2812ac875fe5eacc8b0a5ca7a --- /dev/null +++ b/src/Exception/NewsTypeNotFoundException.php @@ -0,0 +1,13 @@ +value); + } +} \ No newline at end of file diff --git a/src/Exception/RequestBodyConvertException.php b/src/Exception/RequestBodyConvertException.php new file mode 100644 index 0000000000000000000000000000000000000000..40a3418fa125a55aa17070b5581293d8c225c42f --- /dev/null +++ b/src/Exception/RequestBodyConvertException.php @@ -0,0 +1,13 @@ +value); + } +} \ No newline at end of file diff --git a/src/Exception/RestaurantExceptionEnum.php b/src/Exception/RestaurantExceptionEnum.php deleted file mode 100644 index 6f3f8fa9871a3d7b68784b85faedb4b7e55018e0..0000000000000000000000000000000000000000 --- a/src/Exception/RestaurantExceptionEnum.php +++ /dev/null @@ -1,10 +0,0 @@ -value); + parent::__construct("Restaurant not found", ErrorCodeEnum::NotFound->value); } } \ No newline at end of file diff --git a/src/Exception/RestaurantTypeNotFoundException.php b/src/Exception/RestaurantTypeNotFoundException.php new file mode 100644 index 0000000000000000000000000000000000000000..6c98c9cf07c05054b1522751000679d590f593f9 --- /dev/null +++ b/src/Exception/RestaurantTypeNotFoundException.php @@ -0,0 +1,13 @@ +value); + } +} \ No newline at end of file diff --git a/src/Exception/SeoNotFoundException.php b/src/Exception/SeoNotFoundException.php new file mode 100644 index 0000000000000000000000000000000000000000..73c237c947a8fa68cb6e8565fa321c35edffc6fa --- /dev/null +++ b/src/Exception/SeoNotFoundException.php @@ -0,0 +1,13 @@ +value); + } +} \ No newline at end of file diff --git a/src/Exception/SettlementNotFoundException.php b/src/Exception/SettlementNotFoundException.php new file mode 100644 index 0000000000000000000000000000000000000000..85a159eb71f25aa97f3d0062dc00ce53d751d497 --- /dev/null +++ b/src/Exception/SettlementNotFoundException.php @@ -0,0 +1,13 @@ +value); + } +} \ No newline at end of file diff --git a/src/Exception/ValidationException.php b/src/Exception/ValidationException.php new file mode 100644 index 0000000000000000000000000000000000000000..08b8e847a78afcc04f506b6b1d77b43c718dc792 --- /dev/null +++ b/src/Exception/ValidationException.php @@ -0,0 +1,13 @@ +value); + } +} \ No newline at end of file diff --git a/src/Mapper/NewsMapper.php b/src/Mapper/NewsMapper.php index 4b06e81280f3f341e99e4fc4fc71fadfa960f6d3..679bb329b5f70e7bfd1e99a81ca5f9efd5d6d3dc 100644 --- a/src/Mapper/NewsMapper.php +++ b/src/Mapper/NewsMapper.php @@ -2,15 +2,22 @@ namespace App\Mapper; +use App\Entity\File; use App\Entity\News; use App\Entity\NewsCategory; +use App\Entity\NewsType; +use App\Entity\Seo; use App\Model\NewsDetailElement; use App\Model\NewsFilterVariants; use App\Model\NewsList; use App\Model\NewsListingElement; use App\Model\NewsCategory as NewsCategoryModel; use App\Model\Pagination; +use App\Requests\CreateNewsRequest; +use App\Requests\EditNewsRequest; use Ramsey\Collection\Collection; +use DateTimeImmutable; +use Symfony\Component\Uid\Uuid; class NewsMapper { @@ -74,4 +81,58 @@ class NewsMapper $newsCategory->getCode(), ); } + + public static function createNewsEntity( + CreateNewsRequest|EditNewsRequest $request, + NewsType $newsType, + Seo $seo, + File $file, + string $id = null + ): News { + $news = (new News()) + ->setCode($request->getCode()) + ->setActive($request->isActive()) + ->setCreateAt(new DateTimeImmutable()) + ->setUpdateAt(new DateTimeImmutable()) + ->setSort($request->getSort()) + ->setPreviewImage($request->getPreviewImage()) + ->setDetailImage($request->getDetailImage()) + ->setPreviewText($request->getPreviewText()) + ->setDetailText($request->getDetailText()) + ->setType($newsType) + ->setMainPageRender($request->isMainPageRender()) + ->setSeo($seo) + ->setFile($file); + + if ($id !== null) { + $news->setId(new Uuid($id)); + } + + return $news; + } + + public static function updateNewsEntity( + CreateNewsRequest|EditNewsRequest $request, + NewsType $newsType, + Seo $seo, + File $file, + News $news + ): News + { + $news->setCode($request->getCode()); + $news->setActive($request->isActive()); + $news->setCreateAt($news->getCreateAt()); + $news->setUpdateAt(new DateTimeImmutable()); + $news->setSort($request->getSort()); + $news->setPreviewText($request->getPreviewText()); + $news->setDetailText($request->getDetailText()); + $news->setPreviewImage($request->getPreviewImage()); + $news->setDetailImage($request->getDetailImage()); + $news->setType($newsType); + $news->setMainPageRender($request->isMainPageRender()); + $news->setSeo($seo); + $news->setFile($file); + + return $news; + } } \ No newline at end of file diff --git a/src/Mapper/RestaurantMapper.php b/src/Mapper/RestaurantMapper.php index fc72b1520568a617d193e7c6712b59f10d28f9e4..6674e68cf2e4aae961fa569e825d1282390679d9 100644 --- a/src/Mapper/RestaurantMapper.php +++ b/src/Mapper/RestaurantMapper.php @@ -6,7 +6,10 @@ use App\Entity\Gallery; use App\Entity\Kitchen; use App\Entity\Restaurant; use App\Entity\RestaurantType; +use App\Entity\Seo; +use App\Entity\Settlement; use App\Entity\Tags; +use App\Entity\File; use App\Model\KitchenType; use App\Model\Pagination; use App\Model\RestaurantDetailElement; @@ -15,8 +18,12 @@ use App\Model\RestaurantList; use App\Model\RestaurantListingElement; use App\Model\RestaurantType as RestaurantTypeModel; use App\Model\Tag; -use App\Model\File; +use App\Model\File as FileModel; +use App\Requests\CreateRestaurantRequest; +use App\Requests\EditRestaurantRequest; use Ramsey\Collection\Collection; +use DateTimeImmutable; +use Symfony\Component\Uid\Uuid; class RestaurantMapper { @@ -69,7 +76,7 @@ class RestaurantMapper public static function mapToDetailElement( Restaurant $restaurant, - Collection $gallery + Collection|null $gallery ): RestaurantDetailElement { return new RestaurantDetailElement( $restaurant->getId(), @@ -96,7 +103,9 @@ class RestaurantMapper ), $restaurant->getSite(), FileMapper::mapToFile($restaurant->getFile()), - self::mapToGallery($gallery), + $gallery + ? self::mapToGallery($gallery) + : new Collection(FileModel::class), $restaurant->getSeo()?->getTitle(), $restaurant->getSeo()?->getDescription(), $restaurant->getSeo()?->getKeywords() @@ -135,10 +144,81 @@ class RestaurantMapper public static function mapToGallery(Collection $gallery): Collection { - return new Collection(File::class, array_map( + return new Collection(FileModel::class, array_map( function (Gallery $galleryOne) { return FileMapper::mapToFile($galleryOne->getFile()); }, $gallery->toArray()) ); } + + public static function createRestaurantEntity( + CreateRestaurantRequest|EditRestaurantRequest $request, + RestaurantType $restaurantType, + Settlement $settlement, + Seo $seo, + File $file, + string $id = null + ): Restaurant { + $restaurant = (new Restaurant()) + ->setName($request->getName()) + ->setCode($request->getCode()) + ->setActive($request->isActive()) + ->setSort($request->getSort()) + ->setCreateAt(new DateTimeImmutable()) + ->setUpdateAt(new DateTimeImmutable()) + ->setCoordinates($request->getCoordinates()) + ->setTypeId($restaurantType) + ->setSettlementId($settlement) + ->setDescription($request->getDescription()) + ->setCheckPrice($request->getCheck()) + ->setCheckInfo($request->getCheckInfo()) + ->setPhone($request->getPhone()) + ->setEmail($request->getEmail()) + ->setAddress($request->getAddress()) + ->setSite($request->getSite()) + ->setPreviewImage($request->getPreviewImage()) + ->setDetailImage($request->getDetailImage()) + ->setSeo($seo) + ->setFile($file) + ->setHowToFind($request->getHowToFind()); + + if ($id !== null) { + $restaurant->setId(new Uuid($id)); + } + + return $restaurant; + } + + public static function updateRestaurantEntity( + EditRestaurantRequest $request, + RestaurantType $restaurantType, + Settlement $settlement, + Seo $seo, + File $file, + Restaurant $restaurant + ): Restaurant + { + $restaurant->setName($request->getName()); + $restaurant->setCode($request->getCode()); + $restaurant->setActive($request->isActive()); + $restaurant->setSort($request->getSort()); + $restaurant->setCreateAt($restaurant->getCreateAt()); + $restaurant->setUpdateAt(new DateTimeImmutable()); + $restaurant->setCoordinates($request->getCoordinates()); + $restaurant->setTypeId($restaurantType); + $restaurant->setSettlementId($settlement); + $restaurant->setDescription($request->getDescription()); + $restaurant->setCheckPrice($request->getCheck()); + $restaurant->setCheckInfo($request->getCheckInfo()); + $restaurant->setPhone($request->getPhone()); + $restaurant->setEmail($request->getEmail()); + $restaurant->setAddress($request->getAddress()); + $restaurant->setSite($request->getSite()); + $restaurant->setPreviewImage($request->getPreviewImage()); + $restaurant->setHowToFind($request->getHowToFind()); + $restaurant->setDetailImage($request->getDetailImage()); + $restaurant->setSeo($seo); + $restaurant->setFile($file); + return $restaurant; + } } \ No newline at end of file diff --git a/src/Repository/FileRepository.php b/src/Repository/FileRepository.php index 4d5449a24e8e97d38429de3e9f3cf866990b9c81..2253c9c949800bbab1a856cf82293280004361a3 100644 --- a/src/Repository/FileRepository.php +++ b/src/Repository/FileRepository.php @@ -5,6 +5,7 @@ namespace App\Repository; use App\Entity\File; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; +use Symfony\Component\Uid\Uuid; /** * @extends ServiceEntityRepository @@ -21,28 +22,8 @@ class FileRepository extends ServiceEntityRepository parent::__construct($registry, File::class); } -// /** -// * @return File[] Returns an array of File objects -// */ -// public function findByExampleField($value): array -// { -// return $this->createQueryBuilder('f') -// ->andWhere('f.exampleField = :val') -// ->setParameter('val', $value) -// ->orderBy('f.id', 'ASC') -// ->setMaxResults(10) -// ->getQuery() -// ->getResult() -// ; -// } - -// public function findOneBySomeField($value): ?File -// { -// return $this->createQueryBuilder('f') -// ->andWhere('f.exampleField = :val') -// ->setParameter('val', $value) -// ->getQuery() -// ->getOneOrNullResult() -// ; -// } + public function getById(Uuid $id): ?File + { + return $this->find($id); + } } diff --git a/src/Repository/Interface/NewsRepositoryInterface.php b/src/Repository/Interface/NewsRepositoryInterface.php index 10a95f66d331c7fa647b978cad77e8dca0acc429..a27f0d8cde242b6e8169c62246b1085c79109490 100644 --- a/src/Repository/Interface/NewsRepositoryInterface.php +++ b/src/Repository/Interface/NewsRepositoryInterface.php @@ -10,4 +10,11 @@ interface NewsRepositoryInterface public function getCount(): int; public function getMainNews(): News; public function getNewsById(string $newsId): News|null; + public function getById(string $id): News|null; + + public function create(News $news): News; + + public function newsExists(string $id): bool; + + public function delete(string $id): void; } \ No newline at end of file diff --git a/src/Repository/Interface/RestaurantRepositoryInterface.php b/src/Repository/Interface/RestaurantRepositoryInterface.php index b23f1e21eb57d470a7c506c76c8028a0dcc234d3..48665b3a91f7a15f5c674aad18524c7809316146 100644 --- a/src/Repository/Interface/RestaurantRepositoryInterface.php +++ b/src/Repository/Interface/RestaurantRepositoryInterface.php @@ -11,4 +11,10 @@ interface RestaurantRepositoryInterface public function getCount(): int; public function getById(string $id): Restaurant|null; + + public function create(Restaurant $restaurant): Restaurant; + + public function restaurantExists(string $id): bool; + + public function delete(string $id): void; } \ No newline at end of file diff --git a/src/Repository/NewsRepository.php b/src/Repository/NewsRepository.php index 597484efde1f03a418249fc778da24bcc5ecd6f9..f22f9a93d832540dece2c9d620d54d6b4e7c0638 100644 --- a/src/Repository/NewsRepository.php +++ b/src/Repository/NewsRepository.php @@ -57,4 +57,45 @@ class NewsRepository extends ServiceEntityRepository implements NewsRepositoryIn return null; } } + + public function getById(string $id): News|null + { + try { + $uuid = new Uuid($id); + return $this->find($uuid); + } catch (\Exception $e) { + return null; + } + } + + public function create(News $news): News + { + $id = $news->getId(); + $this->getEntityManager()->persist($news); + + if ($id !== null) { + $news->setId($id); + } + + $this->getEntityManager()->flush(); + return $news; + } + + public function update(News $news): News + { + $this->getEntityManager()->persist($news); + $this->getEntityManager()->flush(); + return $news; + } + + public function newsExists(string $id): bool + { + return null !== $this->find(new Uuid($id)); + } + + public function delete(string $id): void + { + $this->getEntityManager()->remove($this->find(new Uuid($id))); + $this->getEntityManager()->flush(); + } } diff --git a/src/Repository/NewsTypeRepository.php b/src/Repository/NewsTypeRepository.php index 385998b7a956694f621dbd6286b876565699a042..99181dacff27f1f3eb8bd6e8342bc6e8f04661ad 100644 --- a/src/Repository/NewsTypeRepository.php +++ b/src/Repository/NewsTypeRepository.php @@ -5,6 +5,7 @@ namespace App\Repository; use App\Entity\NewsType; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; +use Symfony\Component\Uid\Uuid; /** * @extends ServiceEntityRepository @@ -21,28 +22,8 @@ class NewsTypeRepository extends ServiceEntityRepository parent::__construct($registry, NewsType::class); } -// /** -// * @return NewsType[] Returns an array of NewsType objects -// */ -// public function findByExampleField($value): array -// { -// return $this->createQueryBuilder('n') -// ->andWhere('n.exampleField = :val') -// ->setParameter('val', $value) -// ->orderBy('n.id', 'ASC') -// ->setMaxResults(10) -// ->getQuery() -// ->getResult() -// ; -// } - -// public function findOneBySomeField($value): ?NewsType -// { -// return $this->createQueryBuilder('n') -// ->andWhere('n.exampleField = :val') -// ->setParameter('val', $value) -// ->getQuery() -// ->getOneOrNullResult() -// ; -// } + public function getById(string $id): ?NewsType + { + return $this->find(new Uuid($id)); + } } diff --git a/src/Repository/RestaurantRepository.php b/src/Repository/RestaurantRepository.php index 3a23833c0aed436afc3c087a8c01ea53bde11a0e..a235f54369bbd456efb19194d38dccb02013e46f 100644 --- a/src/Repository/RestaurantRepository.php +++ b/src/Repository/RestaurantRepository.php @@ -62,4 +62,35 @@ class RestaurantRepository extends ServiceEntityRepository implements Restaurant return null; } } + + public function create(Restaurant $restaurant): Restaurant + { + $id = $restaurant->getId(); + $this->getEntityManager()->persist($restaurant); + + if ($id !== null) { + $restaurant->setId($id); + } + + $this->getEntityManager()->flush(); + return $restaurant; + } + + public function update(Restaurant $restaurant): Restaurant + { + $this->getEntityManager()->persist($restaurant); + $this->getEntityManager()->flush(); + return $restaurant; + } + + public function restaurantExists(string $id): bool + { + return null !== $this->find(new Uuid($id)); + } + + public function delete(string $id): void + { + $this->getEntityManager()->remove($this->find(new Uuid($id))); + $this->getEntityManager()->flush(); + } } diff --git a/src/Repository/RestaurantTypeRepository.php b/src/Repository/RestaurantTypeRepository.php index aa1f62104bf26d4c2efa06d8440f0ee7a4960a72..901e49d444e23dd8390fe0880aa88e23df0aa3bf 100644 --- a/src/Repository/RestaurantTypeRepository.php +++ b/src/Repository/RestaurantTypeRepository.php @@ -6,6 +6,7 @@ use App\Entity\RestaurantType; use App\Repository\Interface\RestaurantTypeRepositoryInterface; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; +use Symfony\Component\Uid\Uuid; /** * @extends ServiceEntityRepository @@ -26,4 +27,9 @@ class RestaurantTypeRepository extends ServiceEntityRepository implements Restau { return $this->findAll(); } + + public function getById(Uuid $id): ?RestaurantType + { + return $this->find($id); + } } diff --git a/src/Repository/SeoRepository.php b/src/Repository/SeoRepository.php index 09b09908908fccbde126ab3a9adc4fdbf966ea01..97e976a3fac7f1ad37cd0920a1a81738fdabc41d 100644 --- a/src/Repository/SeoRepository.php +++ b/src/Repository/SeoRepository.php @@ -5,6 +5,7 @@ namespace App\Repository; use App\Entity\Seo; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; +use Symfony\Component\Uid\Uuid; /** * @extends ServiceEntityRepository @@ -21,28 +22,8 @@ class SeoRepository extends ServiceEntityRepository parent::__construct($registry, Seo::class); } -// /** -// * @return Seo[] Returns an array of Seo objects -// */ -// public function findByExampleField($value): array -// { -// return $this->createQueryBuilder('s') -// ->andWhere('s.exampleField = :val') -// ->setParameter('val', $value) -// ->orderBy('s.id', 'ASC') -// ->setMaxResults(10) -// ->getQuery() -// ->getResult() -// ; -// } - -// public function findOneBySomeField($value): ?Seo -// { -// return $this->createQueryBuilder('s') -// ->andWhere('s.exampleField = :val') -// ->setParameter('val', $value) -// ->getQuery() -// ->getOneOrNullResult() -// ; -// } + public function getById(Uuid $id): ?Seo + { + return $this->find($id); + } } diff --git a/src/Repository/SettlementRepository.php b/src/Repository/SettlementRepository.php index 1b5e100bda76a7b1bcf8677a447d1c551a228caf..d191d815fa2bf8bab110fac8626d13cae52058eb 100644 --- a/src/Repository/SettlementRepository.php +++ b/src/Repository/SettlementRepository.php @@ -5,6 +5,7 @@ namespace App\Repository; use App\Entity\Settlement; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; +use Symfony\Component\Uid\Uuid; /** * @extends ServiceEntityRepository @@ -21,28 +22,8 @@ class SettlementRepository extends ServiceEntityRepository parent::__construct($registry, Settlement::class); } -// /** -// * @return Settlement[] Returns an array of Settlement objects -// */ -// public function findByExampleField($value): array -// { -// return $this->createQueryBuilder('s') -// ->andWhere('s.exampleField = :val') -// ->setParameter('val', $value) -// ->orderBy('s.id', 'ASC') -// ->setMaxResults(10) -// ->getQuery() -// ->getResult() -// ; -// } - -// public function findOneBySomeField($value): ?Settlement -// { -// return $this->createQueryBuilder('s') -// ->andWhere('s.exampleField = :val') -// ->setParameter('val', $value) -// ->getQuery() -// ->getOneOrNullResult() -// ; -// } + public function getById(Uuid $id): ?Settlement + { + return $this->find($id); + } } diff --git a/src/Requests/CreateNewsRequest.php b/src/Requests/CreateNewsRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..25653b6a5bf266f690b8c5b0bae4d010b9aee47c --- /dev/null +++ b/src/Requests/CreateNewsRequest.php @@ -0,0 +1,164 @@ +typeId; + } + + public function setTypeId(Uuid $typeId): self + { + $this->typeId = $typeId; + return $this; + } + + public function getSeoId(): Uuid + { + return $this->seoId; + } + + public function setSeoId(Uuid $seoId): self + { + $this->seoId = $seoId; + return $this; + } + + public function getFileId(): Uuid + { + return $this->fileId; + } + + public function setFileId(Uuid $fileId): self + { + $this->fileId = $fileId; + return $this; + } + + public function getCode(): string + { + return $this->code; + } + + public function setCode(string $code): self + { + $this->code = $code; + return $this; + } + + public function isActive(): bool + { + return $this->active; + } + + public function setActive(bool $active): self + { + $this->active = $active; + return $this; + } + + public function getSort(): int + { + return $this->sort; + } + + public function setSort(int $sort): self + { + $this->sort = $sort; + return $this; + } + + public function getPreviewImage(): string + { + return $this->previewImage; + } + + public function setPreviewImage(string $previewImage): self + { + $this->previewImage = $previewImage; + return $this; + } + + public function getDetailImage(): string + { + return $this->detailImage; + } + + public function setDetailImage(string $detailImage): self + { + $this->detailImage = $detailImage; + return $this; + } + + public function getPreviewText(): string + { + return $this->previewText; + } + + public function setPreviewText(string $previewText): self + { + $this->previewText = $previewText; + return $this; + } + + public function getDetailText(): string + { + return $this->detailText; + } + + public function setDetailText(string $detailText): self + { + $this->detailText = $detailText; + return $this; + } + + public function isMainPageRender(): bool + { + return $this->mainPageRender; + } + + public function setMainPageRender(bool $mainPageRender): self + { + $this->mainPageRender = $mainPageRender; + return $this; + } +} \ No newline at end of file diff --git a/src/Requests/CreateRestaurantRequest.php b/src/Requests/CreateRestaurantRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..bc2e7615ad99d2af238c0d0676147d9d70c7a9ac --- /dev/null +++ b/src/Requests/CreateRestaurantRequest.php @@ -0,0 +1,272 @@ +typeId; + } + + public function setTypeId(Uuid $typeId): self + { + $this->typeId = $typeId; + return $this; + } + + public function getSettlementId(): Uuid + { + return $this->settlementId; + } + + public function setSettlementId(Uuid $settlementId): self + { + $this->settlementId = $settlementId; + return $this; + } + + public function getSeoId(): Uuid + { + return $this->seoId; + } + + public function setSeoId(Uuid $seoId): self + { + $this->seoId = $seoId; + return $this; + } + + public function getFileId(): Uuid + { + return $this->fileId; + } + + public function setFileId(Uuid $fileId): self + { + $this->fileId = $fileId; + return $this; + } + + public function getName(): string + { + return $this->name; + } + + public function setName(string $name): self + { + $this->name = $name; + return $this; + } + + public function getCode(): string + { + return $this->code; + } + + public function setCode(string $code): self + { + $this->code = $code; + return $this; + } + + public function isActive(): bool + { + return $this->active; + } + + public function setActive(bool $active): self + { + $this->active = $active; + return $this; + } + + public function getSort(): int + { + return $this->sort; + } + + public function setSort(int $sort): self + { + $this->sort = $sort; + return $this; + } + + public function getCoordinates(): array + { + return $this->coordinates; + } + + public function setCoordinates(array $coordinates): self + { + $this->coordinates = $coordinates; + return $this; + } + + public function getDescription(): string + { + return $this->description; + } + + public function setDescription(string $description): self + { + $this->description = $description; + return $this; + } + + public function getCheck(): string + { + return $this->check; + } + + public function setCheck(string $check): self + { + $this->check = $check; + return $this; + } + + public function getCheckInfo(): string + { + return $this->checkInfo; + } + + public function setCheckInfo(string $checkInfo): self + { + $this->checkInfo = $checkInfo; + return $this; + } + + public function getPhone(): array + { + return $this->phone; + } + + public function setPhone(array $phone): self + { + $this->phone = $phone; + return $this; + } + + public function getEmail(): array + { + return $this->email; + } + + public function setEmail(array $email): self + { + $this->email = $email; + return $this; + } + + public function getAddress(): array + { + return $this->address; + } + + public function setAddress(array $address): self + { + $this->address = $address; + return $this; + } + + public function getSite(): string + { + return $this->site; + } + + public function setSite(string $site): self + { + $this->site = $site; + return $this; + } + + public function getPreviewImage(): string + { + return $this->previewImage; + } + + public function setPreviewImage(string $previewImage): self + { + $this->previewImage = $previewImage; + return $this; + } + + public function getDetailImage(): string + { + return $this->detailImage; + } + + public function setDetailImage(string $detailImage): self + { + $this->detailImage = $detailImage; + return $this; + } + + public function getHowToFind(): string + { + return $this->howToFind; + } + + public function setHowToFind(string $howToFind): self + { + $this->howToFind = $howToFind; + return $this; + } +} \ No newline at end of file diff --git a/src/Requests/EditNewsRequest.php b/src/Requests/EditNewsRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..e78432c830183856abc72e7278e08024de5ccde5 --- /dev/null +++ b/src/Requests/EditNewsRequest.php @@ -0,0 +1,164 @@ +typeId; + } + + public function setTypeId(Uuid $typeId): self + { + $this->typeId = $typeId; + return $this; + } + + public function getSeoId(): Uuid + { + return $this->seoId; + } + + public function setSeoId(Uuid $seoId): self + { + $this->seoId = $seoId; + return $this; + } + + public function getFileId(): Uuid + { + return $this->fileId; + } + + public function setFileId(Uuid $fileId): self + { + $this->fileId = $fileId; + return $this; + } + + public function getCode(): string + { + return $this->code; + } + + public function setCode(string $code): self + { + $this->code = $code; + return $this; + } + + public function isActive(): bool + { + return $this->active; + } + + public function setActive(bool $active): self + { + $this->active = $active; + return $this; + } + + public function getSort(): int + { + return $this->sort; + } + + public function setSort(int $sort): self + { + $this->sort = $sort; + return $this; + } + + public function getPreviewImage(): string + { + return $this->previewImage; + } + + public function setPreviewImage(string $previewImage): self + { + $this->previewImage = $previewImage; + return $this; + } + + public function getDetailImage(): string + { + return $this->detailImage; + } + + public function setDetailImage(string $detailImage): self + { + $this->detailImage = $detailImage; + return $this; + } + + public function getPreviewText(): string + { + return $this->previewText; + } + + public function setPreviewText(string $previewText): self + { + $this->previewText = $previewText; + return $this; + } + + public function getDetailText(): string + { + return $this->detailText; + } + + public function setDetailText(string $detailText): self + { + $this->detailText = $detailText; + return $this; + } + + public function isMainPageRender(): bool + { + return $this->mainPageRender; + } + + public function setMainPageRender(bool $mainPageRender): self + { + $this->mainPageRender = $mainPageRender; + return $this; + } +} \ No newline at end of file diff --git a/src/Requests/EditRestaurantRequest.php b/src/Requests/EditRestaurantRequest.php new file mode 100644 index 0000000000000000000000000000000000000000..956c9dba04df35cd47ad94bc09ad55f0ae88fe9b --- /dev/null +++ b/src/Requests/EditRestaurantRequest.php @@ -0,0 +1,272 @@ +typeId; + } + + public function setTypeId(Uuid $typeId): self + { + $this->typeId = $typeId; + return $this; + } + + public function getSettlementId(): Uuid + { + return $this->settlementId; + } + + public function setSettlementId(Uuid $settlementId): self + { + $this->settlementId = $settlementId; + return $this; + } + + public function getSeoId(): Uuid + { + return $this->seoId; + } + + public function setSeoId(Uuid $seoId): self + { + $this->seoId = $seoId; + return $this; + } + + public function getFileId(): Uuid + { + return $this->fileId; + } + + public function setFileId(Uuid $fileId): self + { + $this->fileId = $fileId; + return $this; + } + + public function getName(): string + { + return $this->name; + } + + public function setName(string $name): self + { + $this->name = $name; + return $this; + } + + public function getCode(): string + { + return $this->code; + } + + public function setCode(string $code): self + { + $this->code = $code; + return $this; + } + + public function isActive(): bool + { + return $this->active; + } + + public function setActive(bool $active): self + { + $this->active = $active; + return $this; + } + + public function getSort(): int + { + return $this->sort; + } + + public function setSort(int $sort): self + { + $this->sort = $sort; + return $this; + } + + public function getCoordinates(): array + { + return $this->coordinates; + } + + public function setCoordinates(array $coordinates): self + { + $this->coordinates = $coordinates; + return $this; + } + + public function getDescription(): string + { + return $this->description; + } + + public function setDescription(string $description): self + { + $this->description = $description; + return $this; + } + + public function getCheck(): string + { + return $this->check; + } + + public function setCheck(string $check): self + { + $this->check = $check; + return $this; + } + + public function getCheckInfo(): string + { + return $this->checkInfo; + } + + public function setCheckInfo(string $checkInfo): self + { + $this->checkInfo = $checkInfo; + return $this; + } + + public function getPhone(): array + { + return $this->phone; + } + + public function setPhone(array $phone): self + { + $this->phone = $phone; + return $this; + } + + public function getEmail(): array + { + return $this->email; + } + + public function setEmail(array $email): self + { + $this->email = $email; + return $this; + } + + public function getAddress(): array + { + return $this->address; + } + + public function setAddress(array $address): self + { + $this->address = $address; + return $this; + } + + public function getSite(): string + { + return $this->site; + } + + public function setSite(string $site): self + { + $this->site = $site; + return $this; + } + + public function getPreviewImage(): string + { + return $this->previewImage; + } + + public function setPreviewImage(string $previewImage): self + { + $this->previewImage = $previewImage; + return $this; + } + + public function getDetailImage(): string + { + return $this->detailImage; + } + + public function setDetailImage(string $detailImage): self + { + $this->detailImage = $detailImage; + return $this; + } + + public function getHowToFind(): string + { + return $this->howToFind; + } + + public function setHowToFind(string $howToFind): self + { + $this->howToFind = $howToFind; + return $this; + } +} \ No newline at end of file diff --git a/src/Service/NewsService.php b/src/Service/NewsService.php index a982f83781613bb5bfe85d6c80dd22135ecb0b03..3e596f4aa2b6726bdf061f41e53154121eea53e4 100644 --- a/src/Service/NewsService.php +++ b/src/Service/NewsService.php @@ -2,15 +2,26 @@ namespace App\Service; +use App\Entity\File; use App\Entity\News; use App\Entity\NewsCategory; +use App\Entity\NewsType; +use App\Entity\Seo; +use App\Exception\FileNotFoundException; use App\Exception\NewsNotFoundException; +use App\Exception\NewsTypeNotFoundException; +use App\Exception\SeoNotFoundException; use App\Mapper\NewsMapper; use App\Model\NewsDetailElement; use App\Model\NewsList; use App\Model\NewsListingElement; +use App\Repository\FileRepository; use App\Repository\Interface\NewsCategoryRepositoryInterface; use App\Repository\Interface\NewsRepositoryInterface; +use App\Repository\NewsTypeRepository; +use App\Repository\SeoRepository; +use App\Requests\CreateNewsRequest; +use App\Requests\EditNewsRequest; use App\Requests\NewsListRequest; use Ramsey\Collection\Collection; use Symfony\Component\HttpFoundation\Request; @@ -22,7 +33,10 @@ class NewsService public function __construct( private NewsRepositoryInterface $newsRepository, - private NewsCategoryRepositoryInterface $newsCategoryRepository + private NewsCategoryRepositoryInterface $newsCategoryRepository, + private NewsTypeRepository $newsTypeRepository, + private SeoRepository $seoRepository, + private FileRepository $fileRepository ) {} public function getNewsByRequest(NewsListRequest $request): NewsList @@ -83,4 +97,87 @@ class NewsService return NewsMapper::mapToDetailElement($news); } + + public function addNewsByRequest( + CreateNewsRequest|EditNewsRequest $request, + string $id = null + ): NewsDetailElement { + $newsType = $this->newsTypeRepository->getById($request->getTypeId()); + $seo = $this->seoRepository->getById($request->getSeoId()); + $file = $this->fileRepository->getById($request->getFileId()); + $this->checkForNulls($newsType, $seo, $file); + + $news = $this->newsRepository->create( + NewsMapper::createNewsEntity( + $request, + $newsType, + $seo, + $file, + $id + )); + + return NewsMapper::mapToDetailElement($news); + } + + public function putNewsByRequest( + string $id, + EditNewsRequest $request + ): NewsDetailElement { + if ($this->newsRepository->newsExists($id)) { + return $this->patchNewsByRequest($id, $request); + } + + return $this->addNewsByRequest($request, $id); + } + + public function patchNewsByRequest( + string $id, + EditNewsRequest $request + ): NewsDetailElement { + $newsType = $this->newsTypeRepository->getById($request->getTypeId()); + $seo = $this->seoRepository->getById($request->getSeoId()); + $file = $this->fileRepository->getById($request->getFileId()); + $this->checkForNulls($newsType, $seo, $file); + + if (!$this->newsRepository->newsExists($id)) { + throw new NewsNotFoundException(); + } + + $newNews = NewsMapper::updateNewsEntity( + $request, + $newsType, + $seo, + $file, + $this->newsRepository->getById($id) + ); + + return NewsMapper::mapToDetailElement( + $this->newsRepository->update($newNews), + ); + } + + public function deleteNews(string $id): void + { + if (!$this->newsRepository->newsExists($id)) { + throw new NewsNotFoundException(); + } + + $this->newsRepository->delete($id); + } + + private function checkForNulls( + ?NewsType $type, + ?Seo $seo, + ?File $file + ): void { + if ($type === null) { + throw new NewsTypeNotFoundException(); + } + if ($seo === null) { + throw new SeoNotFoundException; + } + if ($file === null) { + throw new FileNotFoundException(); + } + } } \ No newline at end of file diff --git a/src/Service/RestaurantService.php b/src/Service/RestaurantService.php index a53f59770f4a8087ef8a99949f3d0a862fe46a9e..481d33e60ce36ce4351f2ce046d93bd985c9f7e1 100644 --- a/src/Service/RestaurantService.php +++ b/src/Service/RestaurantService.php @@ -2,12 +2,24 @@ namespace App\Service; +use App\Entity\File; use App\Entity\Gallery; use App\Entity\Kitchen; use App\Entity\Restaurant; use App\Entity\RestaurantType; +use App\Entity\Seo; +use App\Entity\Settlement; +use App\Exception\FileNotFoundException; use App\Exception\RestaurantNotFoundException; +use App\Exception\RestaurantTypeNotFoundException; +use App\Exception\SeoNotFoundException; +use App\Exception\SettlementNotFoundException; +use App\Repository\FileRepository; use App\Repository\Interface\GalleryRepositoryInterface; +use App\Repository\SeoRepository; +use App\Repository\SettlementRepository; +use App\Requests\CreateRestaurantRequest; +use App\Requests\EditRestaurantRequest; use App\Requests\RestaurantListRequest; use App\Mapper\RestaurantMapper; use App\Model\RestaurantDetailElement; @@ -17,6 +29,7 @@ use App\Repository\Interface\RestaurantRepositoryInterface; use App\Repository\Interface\RestaurantTypeRepositoryInterface; use Ramsey\Collection\Collection; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Uid\Uuid; class RestaurantService { @@ -28,6 +41,9 @@ class RestaurantService private RestaurantTypeRepositoryInterface $restaurantTypeRepository, private KitchenRepositoryInterface $kitchenRepository, private GalleryRepositoryInterface $galleryRepository, + private SettlementRepository $settlementRepository, + private SeoRepository $seoRepository, + private FIleRepository $fileRepository, ) {} public function getRestaurantsByRequest( @@ -99,4 +115,96 @@ class RestaurantService return RestaurantMapper::mapToDetailElement($restaurant, $gallery); } + + public function addRestaurantByRequest( + CreateRestaurantRequest|EditRestaurantRequest $request, + string $id = null + ): RestaurantDetailElement { + $restaurantType = $this->restaurantTypeRepository->getById($request->getTypeId()); + $settlement = $this->settlementRepository->getById($request->getSettlementId()); + $seo = $this->seoRepository->getById($request->getSeoId()); + $file = $this->fileRepository->getById($request->getFileId()); + $this->checkForNulls($restaurantType, $settlement, $seo, $file); + + $restaurant = $this->restaurantRepository->create( + RestaurantMapper::createRestaurantEntity( + $request, + $restaurantType, + $settlement, + $seo, + $file, + $id + )); + + return RestaurantMapper::mapToDetailElement($restaurant, null); + } + + public function putRestaurantByRequest( + string $id, + EditRestaurantRequest $request + ): RestaurantDetailElement { + if ($this->restaurantRepository->restaurantExists($id)) { + return $this->patchRestaurantByRequest($id, $request); + } + + return $this->addRestaurantByRequest($request, $id); + } + + public function patchRestaurantByRequest( + string $id, + EditRestaurantRequest $request + ): RestaurantDetailElement { + $restaurantType = $this->restaurantTypeRepository->getById($request->getTypeId()); + $settlement = $this->settlementRepository->getById($request->getSettlementId()); + $seo = $this->seoRepository->getById($request->getSeoId()); + $file = $this->fileRepository->getById($request->getFileId()); + $this->checkForNulls($restaurantType, $settlement, $seo, $file); + + if (!$this->restaurantRepository->restaurantExists($id)) { + throw new RestaurantNotFoundException(); + } + + $newRestaurant = RestaurantMapper::updateRestaurantEntity( + $request, + $restaurantType, + $settlement, + $seo, + $file, + $this->restaurantRepository->getById($id) + ); + + return RestaurantMapper::mapToDetailElement( + $this->restaurantRepository->update($newRestaurant), + null + ); + } + + public function deleteRestaurant(string $id): void + { + if (!$this->restaurantRepository->restaurantExists($id)) { + throw new RestaurantNotFoundException(); + } + + $this->restaurantRepository->delete($id); + } + + private function checkForNulls( + ?RestaurantType $type, + ?Settlement $settlement, + ?Seo $seo, + ?File $file + ): void { + if ($type === null) { + throw new RestaurantTypeNotFoundException(); + } + if ($settlement === null) { + throw new SettlementNotFoundException(); + } + if ($seo === null) { + throw new SeoNotFoundException; + } + if ($file === null) { + throw new FileNotFoundException(); + } + } } \ No newline at end of file