From b1440e2f0fc1b6d464b36925e687374043cd6221 Mon Sep 17 00:00:00 2001 From: Ilya Vasilenko Date: Mon, 1 Jul 2024 14:17:20 +0500 Subject: [PATCH] add/remove favorite --- app/src/Controller/QuestController.php | 41 ++++++++++- app/src/Listeners/DateListener.php | 9 +++ app/src/Repository/FavoriteRepository.php | 25 +++++-- .../Service/Action/Classes/AddFavorite.php | 71 +++++++++++++++++++ .../Service/Action/Classes/RemoveFavorite.php | 68 ++++++++++++++++++ 5 files changed, 205 insertions(+), 9 deletions(-) create mode 100644 app/src/Service/Action/Classes/AddFavorite.php create mode 100644 app/src/Service/Action/Classes/RemoveFavorite.php diff --git a/app/src/Controller/QuestController.php b/app/src/Controller/QuestController.php index 08593cb..4c8b52d 100644 --- a/app/src/Controller/QuestController.php +++ b/app/src/Controller/QuestController.php @@ -2,13 +2,10 @@ namespace App\Controller; -use App\Entity\Quest; use App\Service\Action\ActionServiceInterface; -use App\Service\Action\Classes\CreateReview; use App\Service\Dto\Classes\CreateReviewDto; use App\Service\Dto\Classes\FilterDto; use App\Service\Dto\Classes\IdDto; -use App\Service\Dto\Classes\RegisterCodeDto; use App\Service\Dto\Classes\UpdateReviewDto; use App\Service\Response\Classes\FilterParamsResponse; use App\Service\Response\Classes\FilterResponse; @@ -244,4 +241,42 @@ class QuestController extends AbstractController { return $actionService->getResponse(); } + + #[Route('/quest/favorite/add', name: 'quest_favorite_add', methods: ['POST'])] + #[OA\RequestBody( + content: new OA\JsonContent(ref: new Model(type: IdDto::class)) + )] + #[OA\Response( + response: 200, + description: 'Ответ', + content: new OA\JsonContent( + ref: new Model(type: Response::class, groups: ["message"]) + ) + )] + public function addFavorite( + #[Autowire(service: 'action.favorite.add')] + ActionServiceInterface $actionService + ): JsonResponse + { + return $actionService->getResponse(); + } + + #[Route('/quest/favorite/remove', name: 'quest_favorite_remove', methods: ['POST'])] + #[OA\RequestBody( + content: new OA\JsonContent(ref: new Model(type: IdDto::class)) + )] + #[OA\Response( + response: 200, + description: 'Ответ', + content: new OA\JsonContent( + ref: new Model(type: Response::class, groups: ["message"]) + ) + )] + public function removeFavorite( + #[Autowire(service: 'action.favorite.remove')] + ActionServiceInterface $actionService + ): JsonResponse + { + return $actionService->getResponse(); + } } \ No newline at end of file diff --git a/app/src/Listeners/DateListener.php b/app/src/Listeners/DateListener.php index 1cb534a..4bec5dd 100644 --- a/app/src/Listeners/DateListener.php +++ b/app/src/Listeners/DateListener.php @@ -3,6 +3,7 @@ namespace App\Listeners; use App\Entity\Appointment; +use App\Entity\Favorite; use App\Entity\Like; use App\Entity\Review; use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener; @@ -12,6 +13,7 @@ use Doctrine\ORM\Events; #[AsEntityListener(event: Events::preFlush, method: 'prePersistAppointment', entity: Appointment::class)] #[AsEntityListener(event: Events::preFlush, method: 'prePersistReview', entity: Review::class)] #[AsEntityListener(event: Events::preFlush, method: 'prePersistLike', entity: Like::class)] +#[AsEntityListener(event: Events::preFlush, method: 'prePersistFavorite', entity: Favorite::class)] class DateListener { public function prePersistAppointment(Appointment $appointment, PreFlushEventArgs $args): void @@ -35,4 +37,11 @@ class DateListener $like->setDate(new \DateTime()); } } + + public function prePersistFavorite(Favorite $favorite, PreFlushEventArgs $args): void + { + if (!$favorite->getDate()) { + $favorite->setDate(new \DateTime()); + } + } } \ No newline at end of file diff --git a/app/src/Repository/FavoriteRepository.php b/app/src/Repository/FavoriteRepository.php index b96991e..7793693 100644 --- a/app/src/Repository/FavoriteRepository.php +++ b/app/src/Repository/FavoriteRepository.php @@ -26,6 +26,19 @@ class FavoriteRepository extends ServiceEntityRepository ->getResult(); } + public function getByQuest(int $questId, int $userId): ?Favorite + { + return $this->createQueryBuilder('f') + ->setParameter('user_id', $userId) + ->leftJoin('f.related_user', 'user') + ->andWhere('user.id = :user_id') + ->setParameter('quest_id', $questId) + ->leftJoin('f.quest', 'quest') + ->andWhere('quest.id = :quest_id') + ->getQuery() + ->getOneOrNullResult(); + } + // /** // * @return Favorite[] Returns an array of Favorite objects // */ @@ -43,11 +56,11 @@ class FavoriteRepository extends ServiceEntityRepository // public function findOneBySomeField($value): ?Favorite // { - // return $this->createQueryBuilder('f') - // ->andWhere('f.exampleField = :val') - // ->setParameter('val', $value) - // ->getQuery() - // ->getOneOrNullResult() - // ; +// return $this->createQueryBuilder('f') +// ->andWhere('f.exampleField = :val') +// ->setParameter('val', $value) +// ->getQuery() +// ->getOneOrNullResult() +// ; // } } diff --git a/app/src/Service/Action/Classes/AddFavorite.php b/app/src/Service/Action/Classes/AddFavorite.php new file mode 100644 index 0000000..83ee788 --- /dev/null +++ b/app/src/Service/Action/Classes/AddFavorite.php @@ -0,0 +1,71 @@ +getDto(); + if ($dto->id && $this->user->getId()) { + /** @var Quest|null $quest */ + $quest = $this->doctrine->getRepository(Quest::class)->findOneById($dto->id, $this->user->getId()); + if ($quest) { + $favorite = $this->doctrine->getRepository(Favorite::class)->getByQuest($dto->id, $this->user->getId()); + if ($favorite) { + $this->responseService->addError('Квест уже в избранном'); + } else { + try { + $newFavorite = new Favorite(); + $newFavorite->setQuest($quest); + $newFavorite->setRelatedUser($this->user); + $em = $this->doctrine->getManager(); + $em->persist($newFavorite); + $em->flush(); + $this->responseService->addMessage('Квест добавлен в избранное'); + } catch (\Exception $exception) { + $this->responseService->addError('Ошибка добавления в избранное'); + } + } + } else { + $this->responseService->addError('Квест не найден'); + } + } else { + $this->responseService->addError('ID квеста не получен'); + } + } + + public function needDto(): bool + { + return true; + } + + public function checkDelete(): bool + { + return true; + } + + public function checkConfirm(): bool + { + return true; + } +} \ No newline at end of file diff --git a/app/src/Service/Action/Classes/RemoveFavorite.php b/app/src/Service/Action/Classes/RemoveFavorite.php new file mode 100644 index 0000000..b705e6f --- /dev/null +++ b/app/src/Service/Action/Classes/RemoveFavorite.php @@ -0,0 +1,68 @@ +getDto(); + if ($dto->id && $this->user->getId()) { + /** @var Quest|null $quest */ + $quest = $this->doctrine->getRepository(Quest::class)->findOneById($dto->id, $this->user->getId()); + if ($quest) { + $favorite = $this->doctrine->getRepository(Favorite::class)->getByQuest($dto->id, $this->user->getId()); + if ($favorite) { + try { + $em = $this->doctrine->getManager(); + $em->remove($favorite); + $em->flush(); + $this->responseService->addMessage('Квест удален из избранного'); + } catch (\Exception $exception) { + $this->responseService->addError('Ошибка удаления из избранного'); + } + } else { + $this->responseService->addError('Квеста нет в списке избранного'); + } + } else { + $this->responseService->addError('Квест не найден'); + } + } else { + $this->responseService->addError('ID квеста не получен'); + } + } + + public function needDto(): bool + { + return true; + } + + public function checkDelete(): bool + { + return true; + } + + public function checkConfirm(): bool + { + return true; + } +} \ No newline at end of file -- GitLab