Skip to content

Commit

Permalink
add endpoint to see if user has reward
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbeig committed Apr 30, 2024
1 parent e3fdc24 commit 0ef04c8
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 25 deletions.
2 changes: 1 addition & 1 deletion config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ security:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
api:
pattern: ^/(userInfo|userApiToken|userInvests)
pattern: ^/(userInfo|userApiToken|userInvests|userInvestedToReward)
security: true
stateless: true
oauth2: true
Expand Down
9 changes: 9 additions & 0 deletions src/Controller/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\UseCase\GetUserApiTokenUseCase;
use App\UseCase\GetUserInfoUseCase;
use App\UseCase\GetUserInvestsUseCase;
use App\UseCase\GetUserInvestedToRewardUseCase;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -53,6 +54,14 @@ public function userInvests(GetUserInvestsUseCase $useCase): Response
return $this->json($response);
}

#[Route('/userInvestedToReward/{reward_id}', name: 'userInvestedToReward')]
public function userInvestedToReward(Request $request, int $reward_id, GetUserInvestedToRewardUseCase $useCase): Response
{
$response = $useCase->execute($reward_id, $this->getUser()->getUserIdentifier());

return $this->json($response);
}

#[Route('/{_locale}', name: 'home')]
public function index(): Response
{
Expand Down
50 changes: 50 additions & 0 deletions src/Entity/InvestReward.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Entity;

use App\Repository\InvestRewardRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: InvestRewardRepository::class)]
class InvestReward
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;

#[ORM\Column()]
private ?int $invest = null;

#[ORM\Column()]
private ?int $reward = null;

public function getId(): ?int
{
return $this->id;
}

public function getInvest(): ?int
{
return $this->invest;
}

public function setInvest(int $invest): static
{
$this->invest = $invest;

return $this;
}

public function getReward(): ?int
{
return $this->reward;
}

public function setReward(int $reward): static
{
$this->reward = $reward;

return $this;
}
}
38 changes: 14 additions & 24 deletions src/Repository/InvestRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace App\Repository;

use App\Entity\Invest;
use App\Entity\InvestReward;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Orm\Query\Expr\Join;
use Doctrine\Persistence\ManagerRegistry;

/**
Expand Down Expand Up @@ -35,28 +37,16 @@ public function findByUser(User $user): array
->getResult();
}

// /**
// * @return Invest[] Returns an array of Invest objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('i')
// ->andWhere('i.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('i.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }

// public function findOneBySomeField($value): ?Invest
// {
// return $this->createQueryBuilder('i')
// ->andWhere('i.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
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')
->setParameter(':user_id', $user->getId())
->setParameter(':reward_id', $reward_id)
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
}
}
23 changes: 23 additions & 0 deletions src/Repository/InvestRewardRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Repository;

use App\Entity\InvestReward;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

/**
* @extends ServiceEntityRepository<InvestReward>
*
* @method InvestReward|null find($id, $lockMode = null, $lockVersion = null)
* @method InvestReward|null findOneBy(array $criteria, array $orderBy = null)
* @method InvestReward[] findAll()
* @method InvestReward[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class InvestRewardRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, InvestReward::class);
}
}
37 changes: 37 additions & 0 deletions src/UseCase/GetUserInvestedToRewardUseCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Goteo Package.
*
* (c) Platoniq y Fundación Goteo <[email protected]>
*
* 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 GetUserInvestedToRewardUseCase
{
private UserRepository $userRepository;
private InvestRepository $investRepository;

public function __construct(
UserRepository $userRepository,
InvestRepository $investRepository
) {
$this->userRepository = $userRepository;
$this->investRepository = $investRepository;
}

public function execute(
int $reward_id,
string $userId,
): bool {
$user = $this->userRepository->findById($userId);

return $this->investRepository->hasUserInvestedInReward($user, $reward_id);
}
}

0 comments on commit 0ef04c8

Please sign in to comment.