From 66982cdb0f9bd4fc4e03ba5f5b0fcf0abf5b52a5 Mon Sep 17 00:00:00 2001 From: Ben Borla Date: Sat, 18 Nov 2023 13:46:56 +0800 Subject: [PATCH] feat: setup post and put for adding new data --- api/src/Controller/IndexController.php | 14 ++++++++++- api/src/Repository/FruitRepository.php | 20 ++++------------ api/tests/Controller/IndexControllTest.php | 27 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 api/tests/Controller/IndexControllTest.php diff --git a/api/src/Controller/IndexController.php b/api/src/Controller/IndexController.php index 9f2e1e9..8ed2b3a 100644 --- a/api/src/Controller/IndexController.php +++ b/api/src/Controller/IndexController.php @@ -10,7 +10,7 @@ final class IndexController extends AbstractController { - #[Route('/', name: 'fruits_get_all', methods: ['GET'])] + #[Route('/', name: 'fruits', methods: ['GET'])] public function all(Request $request, FruitRepository $fruits): JsonResponse { $page = (int) $request->get('page'); @@ -27,4 +27,16 @@ public function all(Request $request, FruitRepository $fruits): JsonResponse return new JsonResponse($result); } + + #[Route('/fruit', name: 'fruit_add', methods: ['POST', 'PUT'])] + public function fruitNew(Request $request) + { + // dd($request); + } + + #[Route('/fruit/{id}', name: 'fruit', methods: ['GET', 'POST', 'PUT'])] + public function fruit(Request $request) + { + // dd($request); + } } diff --git a/api/src/Repository/FruitRepository.php b/api/src/Repository/FruitRepository.php index 3a775b1..84f4051 100644 --- a/api/src/Repository/FruitRepository.php +++ b/api/src/Repository/FruitRepository.php @@ -28,11 +28,11 @@ public function __construct(ManagerRegistry $registry) * @return \App\Paginator\Paginator Returns a paginated results of Fruits[] */ public function all( - int $page = 1, - int $size = 10, - string $orderBy = 'name', - string $direction = 'ASC', - string $search = '', + ?int $page = 1, + ?int $size = 10, + ?string $orderBy = 'name', + ?string $direction = 'ASC', + ?string $search = '', ): Paginator { $qb = $this->createQueryBuilder('f') ->orWhere('f.name LIKE :search') @@ -63,14 +63,4 @@ public function findByField(string $field, string $value): null|Fruit ->getQuery() ->getOneOrNullResult(); } - - // public function findOneByField(string $field, string $value): ?Fruit - // { - // return $this->createQueryBuilder('f') - // ->andWhere('f.exampleField = :val') - // ->setParameter('val', $value) - // ->getQuery() - // ->getOneOrNullResult() - // ; - // } } diff --git a/api/tests/Controller/IndexControllTest.php b/api/tests/Controller/IndexControllTest.php new file mode 100644 index 0000000..876fa25 --- /dev/null +++ b/api/tests/Controller/IndexControllTest.php @@ -0,0 +1,27 @@ +request('GET', '/'); + + $this->assertResponseIsSuccessful(); + $this->assertResponseHeaderSame('Content-Type', 'application/json'); + + $data = json_decode($client->getResponse()->getContent(), true); + + // Assert the structure of the JSON response + $this->assertArrayHasKey('currentPage', $data); + $this->assertArrayHasKey('pageSize', $data); + $this->assertArrayHasKey('results', $data); + $this->assertArrayHasKey('numResults', $data); + + } +}