add open orders api route
All checks were successful
/ ls (pull_request) Successful in 1m31s
/ ls (push) Successful in 1m25s
/ ls (release) Successful in 1m0s

This commit is contained in:
lubiana 2025-04-23 18:56:59 +02:00
parent 5de80b0da0
commit 96b246462a
Signed by: lubiana
SSH key fingerprint: SHA256:vW1EA0fRR3Fw+dD/sM0K+x3Il2gSry6YRYHqOeQwrfk
5 changed files with 79 additions and 0 deletions

View file

@ -3,7 +3,9 @@
namespace App\Entity;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use App\Repository\FoodOrderRepository;
use App\State\OpenFoodOrderProvider;
use DateInterval;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
@ -15,6 +17,10 @@ use Symfony\Component\Uid\Ulid;
use function iterator_to_array;
#[ORM\Entity(repositoryClass: FoodOrderRepository::class)]
#[GetCollection(
uriTemplate: '/food_orders/open',
provider: OpenFoodOrderProvider::class,
)]
#[ApiResource]
class FoodOrder
{

View file

@ -47,4 +47,19 @@ final class FoodOrderRepository extends ServiceEntityRepository
->filter(static fn(FoodOrder $order): bool => $order->getCreatedAt() >= $date)
->getValues();
}
/**
* @return FoodOrder[]
*/
public function findOpenOrders(): array
{
$now = new DateTimeImmutable;
return $this->createQueryBuilder('o')
->where('o.closedAt IS NULL OR o.closedAt > :now')
->setParameter('now', $now)
->orderBy('o.id', 'DESC')
->getQuery()
->getResult();
}
}

View file

@ -0,0 +1,21 @@
<?php declare(strict_types=1);
namespace App\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProviderInterface;
use App\Repository\FoodOrderRepository;
use Override;
final readonly class OpenFoodOrderProvider implements ProviderInterface
{
public function __construct(
private FoodOrderRepository $repository
) {}
#[Override]
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
return $this->repository->findOpenOrders();
}
}