added a /api/food_orders/latest/ endpoint to recieve the latest food order
Some checks failed
/ ls (pull_request) Successful in 2m6s
/ ls (release) Successful in 1m18s
/ ls (push) Failing after 1m25s

This commit is contained in:
Jan Felix Wiebe 2025-06-17 21:26:05 +02:00
parent ee32852789
commit 300c8cafc9
4 changed files with 64 additions and 0 deletions

View file

@ -10,6 +10,7 @@ use ApiPlatform\Metadata\Post;
use ApiPlatform\Metadata\Put; use ApiPlatform\Metadata\Put;
use App\Repository\FoodOrderRepository; use App\Repository\FoodOrderRepository;
use App\State\OpenOrdersProvider; use App\State\OpenOrdersProvider;
use App\State\LatestOrderProvider;
use DateInterval; use DateInterval;
use DateTimeImmutable; use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
@ -27,6 +28,11 @@ use function iterator_to_array;
description: 'Get only open orders', description: 'Get only open orders',
provider: OpenOrdersProvider::class, provider: OpenOrdersProvider::class,
), ),
new GetCollection(
uriTemplate: 'food_orders/latest',
description: 'Get the latest created order',
provider: LatestOrderProvider::class,
),
new GetCollection, new GetCollection,
new Get, new Get,
new Post, new Post,

View file

@ -62,4 +62,16 @@ final class FoodOrderRepository extends ServiceEntityRepository
->getQuery() ->getQuery()
->getResult(); ->getResult();
} }
/**
* @return FoodOrder|null
*/
public function findLatestOrder(): ?FoodOrder
{
return $this->createQueryBuilder('alias')
->orderBy('alias.id', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();
}
} }

View file

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

View file

@ -231,6 +231,30 @@ describe(FoodOrderController::class, function (): void {
$this->assertTrue($openOrder->isClosed()); $this->assertTrue($openOrder->isClosed());
}); });
test('api_latest_order', function (): void {
// Create an older order
$olderOrder = new FoodOrder($this->generateOldUlid());
$olderOrder->setFoodVendor($this->vendor);
$this->manager->persist($olderOrder);
// Create the latest order
$latestOrder = new FoodOrder;
$latestOrder->setFoodVendor($this->vendor);
$this->manager->persist($latestOrder);
$this->manager->flush();
// Test the API endpoint
$this->client->request('GET', '/api/food_orders/latest');
$this->assertResponseIsSuccessful();
$this->assertResponseHeaderSame('Content-Type', 'application/ld+json; charset=utf-8');
$response = json_decode($this->client->getResponse()->getContent(), true);
$this->assertIsArray($response);
$this->assertArrayHasKey('@id', $response);
$this->assertStringContainsString($latestOrder->getId()->__toString(), $response['@id']);
});
}) })
->covers( ->covers(
FoodOrderController::class, FoodOrderController::class,