Skip to content

Commit

Permalink
feat: setup post and put for adding new data
Browse files Browse the repository at this point in the history
  • Loading branch information
benborla committed Nov 18, 2023
1 parent 25ef060 commit 66982cd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 16 deletions.
14 changes: 13 additions & 1 deletion api/src/Controller/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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);
}
}
20 changes: 5 additions & 15 deletions api/src/Repository/FruitRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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()
// ;
// }
}
27 changes: 27 additions & 0 deletions api/tests/Controller/IndexControllTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class IndexControllerTest extends WebTestCase
{
public function testAll()
{
$client = static::createClient();

$client->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);

}
}

0 comments on commit 66982cd

Please sign in to comment.