Skip to content

Commit

Permalink
add endpoints for bike management
Browse files Browse the repository at this point in the history
  • Loading branch information
Retch committed Dec 22, 2023
1 parent 447b447 commit d2de870
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/Controller/ApiAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Controller;

use App\Entity\Bike;
use App\Entity\Lock;
use App\Entity\LockType;
use Doctrine\ORM\EntityManagerInterface;
Expand Down Expand Up @@ -332,4 +333,62 @@ public function infoLockById(EntityManagerInterface $entityManager, LoggerInterf
}
return new Response($content, $statusCode);
}

#[Route('/api/admin/bike', name: 'app_api_admin_add_bike', methods: ['POST'])]
public function addBike(EntityManagerInterface $entityManager, Request $request): Response
{
$json = json_decode($request->getContent(), true);

$lock = $entityManager->getRepository(Lock::class)->find($json['lockId']);

if($lock == null)
{
return new Response("Lock with id " . $json['lockId'] . " does not exist", 409);
}

$bike = new Bike();
$bike->setIsAvailable(true);
$bike->setLock($lock);

$entityManager->persist($bike);
$entityManager->flush();

return new Response("added bike", 200);
}

#[Route('/api/admin/bikes', name: 'app_api_admin_get_bikes', methods: ['GET'])]
public function getAllBikes(EntityManagerInterface $entityManager): JsonResponse
{
$bikeEntries = $entityManager->getRepository(Bike::class)->findAll();

$bikes = [];

foreach($bikeEntries as $bike) {
$bikes[] = [
'id' => $bike->getId(),
'lockId' => $bike->getLock()->getId(),
'isAvailable' => $bike->isAvailable(),
];
}

return $this->json([
'bikes' => $bikes,
]);
}

#[Route('/api/admin/bike/{id}', name: 'app_api_admin_delete_bike', methods: ['DELETE'])]
public function deleteBike(EntityManagerInterface $entityManager, int $id): Response
{
$bike = $entityManager->getRepository(Bike::class)->find($id);

if ($bike == null)
{
return new Response("Bike with id " . $id . " does not exist", 409);
}

$entityManager->remove($bike);
$entityManager->flush();

return new Response("deleted bike", 200);
}
}

0 comments on commit d2de870

Please sign in to comment.