Loading app/src/Controller/QuestController.php +38 −3 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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 app/src/Listeners/DateListener.php +9 −0 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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 Loading @@ -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 app/src/Repository/FavoriteRepository.php +19 −6 Original line number Diff line number Diff line Loading @@ -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 // */ Loading app/src/Service/Action/Classes/AddFavorite.php 0 → 100644 +71 −0 Original line number Diff line number Diff line <?php namespace App\Service\Action\Classes; use App\Entity\Favorite; use App\Entity\Quest; use App\Service\Action\UserBaseActionService; use App\Service\Dto\Classes\IdDto; use App\Service\Dto\DtoServiceInterface; use Symfony\Component\DependencyInjection\Attribute\AsAlias; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Contracts\Service\Attribute\Required; #[AsAlias(id: 'action.favorite.add', public: true)] class AddFavorite extends UserBaseActionService { #[Required] public function initDto( #[Autowire(service: 'dto.id')] DtoServiceInterface $dtoService ): void { parent::initDto($dtoService); } public function runAction(): void { /** @var IdDto $dto */ $dto = $this->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 app/src/Service/Action/Classes/RemoveFavorite.php 0 → 100644 +68 −0 Original line number Diff line number Diff line <?php namespace App\Service\Action\Classes; use App\Entity\Favorite; use App\Entity\Quest; use App\Service\Action\UserBaseActionService; use App\Service\Dto\Classes\IdDto; use App\Service\Dto\DtoServiceInterface; use Symfony\Component\DependencyInjection\Attribute\AsAlias; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Contracts\Service\Attribute\Required; #[AsAlias(id: 'action.favorite.remove', public: true)] class RemoveFavorite extends UserBaseActionService { #[Required] public function initDto( #[Autowire(service: 'dto.id')] DtoServiceInterface $dtoService ): void { parent::initDto($dtoService); } public function runAction(): void { /** @var IdDto $dto */ $dto = $this->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 Loading
app/src/Controller/QuestController.php +38 −3 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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
app/src/Listeners/DateListener.php +9 −0 Original line number Diff line number Diff line Loading @@ -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; Loading @@ -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 Loading @@ -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
app/src/Repository/FavoriteRepository.php +19 −6 Original line number Diff line number Diff line Loading @@ -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 // */ Loading
app/src/Service/Action/Classes/AddFavorite.php 0 → 100644 +71 −0 Original line number Diff line number Diff line <?php namespace App\Service\Action\Classes; use App\Entity\Favorite; use App\Entity\Quest; use App\Service\Action\UserBaseActionService; use App\Service\Dto\Classes\IdDto; use App\Service\Dto\DtoServiceInterface; use Symfony\Component\DependencyInjection\Attribute\AsAlias; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Contracts\Service\Attribute\Required; #[AsAlias(id: 'action.favorite.add', public: true)] class AddFavorite extends UserBaseActionService { #[Required] public function initDto( #[Autowire(service: 'dto.id')] DtoServiceInterface $dtoService ): void { parent::initDto($dtoService); } public function runAction(): void { /** @var IdDto $dto */ $dto = $this->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
app/src/Service/Action/Classes/RemoveFavorite.php 0 → 100644 +68 −0 Original line number Diff line number Diff line <?php namespace App\Service\Action\Classes; use App\Entity\Favorite; use App\Entity\Quest; use App\Service\Action\UserBaseActionService; use App\Service\Dto\Classes\IdDto; use App\Service\Dto\DtoServiceInterface; use Symfony\Component\DependencyInjection\Attribute\AsAlias; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Contracts\Service\Attribute\Required; #[AsAlias(id: 'action.favorite.remove', public: true)] class RemoveFavorite extends UserBaseActionService { #[Required] public function initDto( #[Autowire(service: 'dto.id')] DtoServiceInterface $dtoService ): void { parent::initDto($dtoService); } public function runAction(): void { /** @var IdDto $dto */ $dto = $this->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