added a /api/food_orders/latest/ endpoint to recieve the latest food order
This commit is contained in:
parent
ee32852789
commit
300c8cafc9
4 changed files with 64 additions and 0 deletions
|
@ -10,6 +10,7 @@ use ApiPlatform\Metadata\Post;
|
|||
use ApiPlatform\Metadata\Put;
|
||||
use App\Repository\FoodOrderRepository;
|
||||
use App\State\OpenOrdersProvider;
|
||||
use App\State\LatestOrderProvider;
|
||||
use DateInterval;
|
||||
use DateTimeImmutable;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
|
@ -27,6 +28,11 @@ use function iterator_to_array;
|
|||
description: 'Get only open orders',
|
||||
provider: OpenOrdersProvider::class,
|
||||
),
|
||||
new GetCollection(
|
||||
uriTemplate: 'food_orders/latest',
|
||||
description: 'Get the latest created order',
|
||||
provider: LatestOrderProvider::class,
|
||||
),
|
||||
new GetCollection,
|
||||
new Get,
|
||||
new Post,
|
||||
|
|
|
@ -62,4 +62,16 @@ final class FoodOrderRepository extends ServiceEntityRepository
|
|||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return FoodOrder|null
|
||||
*/
|
||||
public function findLatestOrder(): ?FoodOrder
|
||||
{
|
||||
return $this->createQueryBuilder('alias')
|
||||
->orderBy('alias.id', 'DESC')
|
||||
->setMaxResults(1)
|
||||
->getQuery()
|
||||
->getOneOrNullResult();
|
||||
}
|
||||
}
|
||||
|
|
22
src/State/LatestOrderProvider.php
Normal file
22
src/State/LatestOrderProvider.php
Normal 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();
|
||||
}
|
||||
}
|
|
@ -231,6 +231,30 @@ describe(FoodOrderController::class, function (): void {
|
|||
$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(
|
||||
FoodOrderController::class,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue