Skip to content

Commit

Permalink
#10 add put restore and refactoring return bool
Browse files Browse the repository at this point in the history
  • Loading branch information
bfoujols committed Sep 13, 2024
1 parent 2f0c05a commit 1f4c47f
Show file tree
Hide file tree
Showing 15 changed files with 141 additions and 10 deletions.
4 changes: 4 additions & 0 deletions app/Config/routes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ getVeloLocById:
uri: /api/velo/{id}/location
controller: Controller\api\Velo\VeloLocByIdController
httpMethod: [PUT]
getVeloRestoreById:
uri: /api/velo/{velo_id}/restore/{station_id}
controller: Controller\api\Velo\VeloRestoreByIdController
httpMethod: [PUT]
home:
uri: /
controller: Controller\HomeController
Expand Down
1 change: 1 addition & 0 deletions app/Controller/api/Station/StationIdController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function execute(Request $request): string|null
header('Content-Type: application/json');

$data = (new \Repository\StationRepository())->getOneStation($request->get('id'));
if ($data === false) { return json_encode("Station not found"); }

return json_encode($data);
}
Expand Down
1 change: 1 addition & 0 deletions app/Controller/api/Station/StationStatusIdController.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public function execute(Request $request): string|null
header('Content-Type: application/json');

$data = (new \Repository\StationRepository())->getOneStationStatus($request->get('id'));
if ($data === false) { return json_encode("Station not found"); }
if ($request->get('optional') === "velo") {
$data['bikes_available_types'] = (new VeloRepository())->getVeloByStation($request->get('id'));
}
Expand Down
1 change: 1 addition & 0 deletions app/Controller/api/Station/StationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function execute(Request $request): string|null
header('Content-Type: application/json');

$data = (new \Repository\StationRepository())->getAllStations();
if ($data === false) { return json_encode("Station not found"); }

return json_encode($data);
}
Expand Down
1 change: 1 addition & 0 deletions app/Controller/api/Status/StationsStatusController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function execute(Request $request): string|null
header('Content-Type: application/json');

$data = (new \Repository\StationStatusRepository())->getAllStationsStatus();
if ($data === false) { return json_encode("Stations not found"); }

return json_encode($data);
}
Expand Down
1 change: 1 addition & 0 deletions app/Controller/api/Velo/VeloByIdController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function execute(Request $request): string|null
header('Content-Type: application/json');

$data = (new \Repository\VeloRepository())->getVeloById($request->get('id'));
if ($data === false) { return json_encode("Velo not found"); }

return json_encode($data);
}
Expand Down
1 change: 1 addition & 0 deletions app/Controller/api/Velo/VeloController.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function execute(Request $request): string|null
header('Content-Type: application/json');

$data = (new \Repository\VeloRepository())->getAllVelo();
if ($data === false) { return json_encode("Velo not found"); }

return json_encode($data);
}
Expand Down
1 change: 1 addition & 0 deletions app/Controller/api/Velo/VeloLocByIdController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function execute(Request $request): string|null
if (AuthApi::checkToken($request) === false) { return json_encode("Token is not valid"); }

$Velo = (new \Repository\VeloRepository())->getVeloById($request->get('id'));
if ($Velo === false) { return json_encode("Velo not found"); }
if ($Velo["status"] === "location") { return json_encode("Velo statut is location"); }
$Velo["status"] = "location";
(new \Repository\VeloRepository())->update($Velo);
Expand Down
80 changes: 80 additions & 0 deletions app/Controller/api/Velo/VeloRestoreByIdController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Controller\api\Velo;

use Core\AuthApi;
use OpenApi\Attributes;
use Studoo\EduFramework\Core\Controller\ControllerInterface;
use Studoo\EduFramework\Core\Controller\Request;

class VeloRestoreByIdController implements ControllerInterface
{
#[Attributes\Put(
path: '/api/velo/{velo_id}/restore/{station_id}',
operationId: 'getVeloRestoreById',
summary: 'getVeloRestoreById',
description: 'Restaurer le vélo loué via son ID, Cette méthode permet de rendre le Vélo loué dans une station',
)]
#[Attributes\Parameter(
name: 'velo_id',
description: 'ID du vélo loué',
in: 'path',
required: true,
schema: new Attributes\Schema(type: 'integer')
)]
#[Attributes\Parameter(
name: 'station_id',
description: 'ID de la nouvelle station d\'accueil',
in: 'path',
required: true,
schema: new Attributes\Schema(type: 'integer')
)]
#[Attributes\Response(
response: '200',
description: 'L information du vélos sont transmis',
content: new Attributes\MediaType(
mediaType: 'application/json',
examples: [
new Attributes\Examples(
example: '/api/velo/245671/restore/17278902806',
summary: '/api/velo/245671/restore/17278902806',
value: [
"velo_id" => 245671,
"type" => "mechanical",
"status" => "available",
"num_km_total" => 234,
"station_id_available" => 17278902806,
]
)
]
)
)]
public function execute(Request $request): string|null
{
header('Content-Type: application/json');

if (AuthApi::checkToken($request) === false) { return json_encode("Token is not valid"); }

$Velo = (new \Repository\VeloRepository())->getVeloById($request->get('velo_id'));
if ($Velo === false) { return json_encode("Velo not found"); }
if ($Velo["status"] === "available") { return json_encode("Velo statut is available"); }

$station = (new \Repository\StationStatusRepository())->getStationsStatusById($request->get('station_id'));
if ($station["num_docks_available"] === 0) { return json_encode("Station dock is full"); }

$Velo["status"] = "available";
$Velo["station_id_available"] = $request->get('station_id');
(new \Repository\VeloRepository())->update($Velo);

$station["num_bikes_available"] = $station["num_bikes_available"] + 1;
$station["num_docks_available"] = $station["num_docks_available"] - 1;
foreach ($station["num_bikes_available_types"] as $idPos => $typeVelo) {
if (array_key_exists($Velo["type"], $typeVelo)) {
$station["num_bikes_available_types"][$idPos][$Velo["type"]] = $station["num_bikes_available_types"][$idPos][$Velo["type"]] + 1;
}
}
(new \Repository\StationStatusRepository())->update($station);

return json_encode($Velo);
}
}
6 changes: 3 additions & 3 deletions app/Repository/StationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,22 @@ public function insert(array $item): void
*
* @return array
*/
public function getAllStations(): array
public function getAllStations(): array|bool
{
$stmt = $this->db->prepare('SELECT * FROM station');
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}

public function getOneStation(int $id): mixed
public function getOneStation(int $id): array|bool
{
$stmt = $this->db->prepare('SELECT * FROM station WHERE station_id = :id');
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_ASSOC);
}

public function getOneStationStatus(int $id): mixed
public function getOneStationStatus(int $id): array|bool
{
$stmt = $this->db->prepare('SELECT * FROM station, station_status WHERE station.station_id = :id AND station.station_id = station_status.station_id');
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
Expand Down
5 changes: 4 additions & 1 deletion app/Repository/StationStatusRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ public function getStationsStatusById(int $stationId): array
*
* @return array
*/
public function getAllStationsStatus(): array
public function getAllStationsStatus(): array|bool
{
$stmt = $this->db->prepare('SELECT * FROM station_status');
$stmt->execute();
$station = $stmt->fetch(PDO::FETCH_ASSOC);
if ($station === false) {
return false;
}
$station['num_bikes_available_types'] = json_decode($station['num_bikes_available_types'], true);
return $station;
}
Expand Down
6 changes: 3 additions & 3 deletions app/Repository/VeloRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function update(array $item): void
* Récupérer tous les velos
* @return array
*/
public function getAllVelo(): array
public function getAllVelo(): array|bool
{
$stmt = $this->db->prepare('SELECT * FROM velo');
$stmt->execute();
Expand All @@ -70,7 +70,7 @@ public function getAllVelo(): array
* @param int $id
* @return array
*/
public function getVeloById(int $velo_id): array
public function getVeloById(int $velo_id): array|bool
{
$stmt = $this->db->prepare('SELECT * FROM velo WHERE velo_id = :velo_id');
$stmt->execute([
Expand All @@ -83,7 +83,7 @@ public function getVeloById(int $velo_id): array
* Récupérer tous les velos
* @return array
*/
public function getVeloByStation(int $station_id): array
public function getVeloByStation(int $station_id): array|bool
{
$stmt = $this->db->prepare('SELECT * FROM velo WHERE station_id_available = :station_id_available');
$stmt->execute([
Expand Down
Loading

0 comments on commit 1f4c47f

Please sign in to comment.