From 378d46af5e06623f9d924f2948c757df3def8911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Ig=C3=B3n?= Date: Thu, 2 May 2024 11:24:39 +0200 Subject: [PATCH] add two endpoints to check if user donated to reward --- config/packages/security.yaml | 2 +- src/Controller/HomeController.php | 9 +++++ src/Entity/Invest.php | 16 ++++++++ src/Repository/InvestRepository.php | 15 +++++++- .../GetUserActiveInvestedRewardUseCase.php | 37 +++++++++++++++++++ .../GetUserInvestedToRewardUseCase.php | 4 +- 6 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 src/UseCase/GetUserActiveInvestedRewardUseCase.php diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 7d364b4..8b412f3 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -26,7 +26,7 @@ security: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false api: - pattern: ^/(userInfo|userApiToken|userInvests|userInvestedToReward) + pattern: ^/(userInfo|userApiToken|userInvests|userInvestedToReward|userActiveInvestedToReward) security: true stateless: true oauth2: true diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index e9adb86..810706d 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -10,6 +10,7 @@ namespace App\Controller; +use App\UseCase\GetUserActiveInvestedRewardUseCase; use App\UseCase\GetUserApiTokenUseCase; use App\UseCase\GetUserInfoUseCase; use App\UseCase\GetUserInvestsUseCase; @@ -62,6 +63,14 @@ public function userInvestedToReward(Request $request, int $reward_id, GetUserIn return $this->json($response); } + #[Route('/userActiveInvestedToReward/{reward_id}', name: 'userInvestedToReward')] + public function userActiveInvestInReward(int $reward_id, GetUserActiveInvestedRewardUseCase $useCase): Response + { + $response = $useCase->execute($reward_id, $this->getUser()->getUserIdentifier()); + + return $this->json($response); + } + #[Route('/{_locale}', name: 'home')] public function index(): Response { diff --git a/src/Entity/Invest.php b/src/Entity/Invest.php index 2da923b..ff12274 100644 --- a/src/Entity/Invest.php +++ b/src/Entity/Invest.php @@ -3,6 +3,7 @@ namespace App\Entity; use App\Repository\InvestRepository; +use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: InvestRepository::class)] @@ -22,6 +23,9 @@ class Invest #[ORM\Column] private ?int $status = null; + #[ORM\Column(type: Types::DATE_MUTABLE)] + private ?\DateTimeInterface $invested = null; + public function getId(): ?int { return $this->id; @@ -62,4 +66,16 @@ public function setStatus(int $status): static return $this; } + + public function getInvested(): ?\DateTimeInterface + { + return $this->invested; + } + + public function setInvested(\DateTimeInterface $invested): static + { + $this->invested = $invested; + + return $this; + } } diff --git a/src/Repository/InvestRepository.php b/src/Repository/InvestRepository.php index 1f13412..8d8191a 100644 --- a/src/Repository/InvestRepository.php +++ b/src/Repository/InvestRepository.php @@ -42,7 +42,20 @@ public function hasUserInvestedInReward(User $user, int $reward_id): bool return (bool) $this->createQueryBuilder('i') ->select('i') ->innerJoin(InvestReward::class, 'ir', 'WITH', 'ir.invest = i.id') - ->where('i.userId = :user_id AND ir.reward = :reward_id') + ->where('i.userId = :user_id AND ir.reward = :reward_id and i.status = 1') + ->setParameter(':user_id', $user->getId()) + ->setParameter(':reward_id', $reward_id) + ->setMaxResults(1) + ->getQuery() + ->getOneOrNullResult(); + } + + public function hasUserInvestedInRewardLastMonth(User $user, int $reward_id): bool + { + return (bool) $this->createQueryBuilder('i') + ->select('i') + ->innerJoin(InvestReward::class, 'ir', 'WITH', 'ir.invest = i.id') + ->where('i.status = 1 and i.userId = :user_id AND ir.reward = :reward_id AND i.invested >= DATE_SUB(CURRENT_DATE(), 1, \'MONTH\')') ->setParameter(':user_id', $user->getId()) ->setParameter(':reward_id', $reward_id) ->setMaxResults(1) diff --git a/src/UseCase/GetUserActiveInvestedRewardUseCase.php b/src/UseCase/GetUserActiveInvestedRewardUseCase.php new file mode 100644 index 0000000..bdfbf22 --- /dev/null +++ b/src/UseCase/GetUserActiveInvestedRewardUseCase.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the README.md + * and LICENSE files that was distributed with this source code. + */ + + namespace App\UseCase; + +use App\Repository\InvestRepository; +use App\Repository\UserRepository; + + class GetUserActiveInvestedRewardUseCase + { + private UserRepository $userRepository; + private InvestRepository $investRepository; + + public function __construct( + UserRepository $userRepository, + InvestRepository $investRepository + ) { + $this->userRepository = $userRepository; + $this->investRepository = $investRepository; + } + + public function execute( + int $rewardId, + string $userId, + ): bool { + $user = $this->userRepository->findById($userId); + + return $this->investRepository->hasUserInvestedInRewardLastMonth($user, $rewardId); + } + } \ No newline at end of file diff --git a/src/UseCase/GetUserInvestedToRewardUseCase.php b/src/UseCase/GetUserInvestedToRewardUseCase.php index 381b652..9a3cae5 100644 --- a/src/UseCase/GetUserInvestedToRewardUseCase.php +++ b/src/UseCase/GetUserInvestedToRewardUseCase.php @@ -27,11 +27,11 @@ public function __construct( } public function execute( - int $reward_id, + int $rewardId, string $userId, ): bool { $user = $this->userRepository->findById($userId); - return $this->investRepository->hasUserInvestedInReward($user, $reward_id); + return $this->investRepository->hasUserInvestedInReward($user, $rewardId); } }