Skip to content
Snippets Groups Projects
Commit 7d627c15 authored by PEREZ-RAMIREZ Julian's avatar PEREZ-RAMIREZ Julian
Browse files

adding filter to get all items for the same wishList

parent 867dfa2f
No related branches found
No related tags found
No related merge requests found
......@@ -35,6 +35,9 @@ final class ItemController extends AbstractController
#[Route('', methods: ['GET'])]
public function getAllItems(Request $req): JsonResponse
{
$wishList = $req->query->get('wishList');
$wishList = $wishList !== null ? (int) $wishList : null; // Convert to int if provided
// Validate "onlyBought" as boolean (default: false)
$onlyBought = filter_var($req->query->get('onlyBought', false),
FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
......@@ -62,7 +65,7 @@ final class ItemController extends AbstractController
["options" => ["min_range" => 1]]) ?: 1;
// Fetch items from repository with filters
$items = $this->itemRepository->findAllItemsFiltered($onlyBought, $sortBy,
$items = $this->itemRepository->findAllItemsFiltered($wishList, $onlyBought, $sortBy,
$sort, $max, $page);
if (!$items) {
......@@ -71,7 +74,7 @@ final class ItemController extends AbstractController
$itemArray = array_map(fn($item) => [
'id' => $item->getId(),
'wishList' => $item->getWishList(),
'wishList' => $item->getWishList()->getId(),
'title' => $item->getTitle(),
'description' => $item->getDescription(),
'price' => $item->getPrice(),
......@@ -115,4 +118,7 @@ final class ItemController extends AbstractController
'path' => 'src/Controller/ItemController.php',
]);
}
}
......@@ -27,10 +27,16 @@ class ItemRepository extends ServiceEntityRepository
}
// Custom method to manage pagination and filters.
public function findAllItemsFiltered(bool $onlyBought, string $sortBy, string $sort, int $max, int $page)
public function findAllItemsFiltered(?int $wishList, bool $onlyBought, string $sortBy, string $sort, int $max, int $page)
{
$queryBuilder = $this->createQueryBuilder('i');
// Filter by wishListId if provided
if ($wishList !== null) {
$queryBuilder->andWhere('i.wishList = :wishListId')
->setParameter('wishListId', $wishList);
}
if ($onlyBought) {
$queryBuilder->innerJoin('App\Entity\Purchase', 'p', 'WITH', 'p.item = i.id');
}
......@@ -52,4 +58,6 @@ class ItemRepository extends ServiceEntityRepository
return $queryBuilder->getQuery()->getResult();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment