132 lines
3.8 KiB
PHP
132 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Controller\CAPI;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Doctrine\ORM\Query;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Catalyst\APIBundle\Controller\APIController;
|
|
use Catalyst\APIBundle\Response\APIResponse;
|
|
use App\Entity\Vehicle;
|
|
use App\Entity\VehicleManufacturer;
|
|
|
|
use Catalyst\APIBundle\Access\Generator as ACLGenerator;
|
|
|
|
class VehicleController extends APIController
|
|
{
|
|
protected $acl_gen;
|
|
|
|
public function __construct(ACLGenerator $acl_gen)
|
|
{
|
|
$this->acl_gen = $acl_gen;
|
|
}
|
|
|
|
public function getManufacturers(EntityManagerInterface $em)
|
|
{
|
|
$this->denyAccessUnlessGranted('vmanufacturer.list', null, 'No access.');
|
|
|
|
$mfgs = $em->getRepository(VehicleManufacturer::class)->findBy([], ['name' => 'ASC']);
|
|
|
|
$result = [];
|
|
foreach ($mfgs as $mfg)
|
|
{
|
|
$result[] = [
|
|
'id' => $mfg->getID(),
|
|
'name' => $mfg->getName(),
|
|
];
|
|
}
|
|
|
|
$data = [
|
|
'manufacturers' => $result,
|
|
];
|
|
|
|
return new APIResponse(true, 'Vehicle manufacturers loaded.', $data);
|
|
}
|
|
|
|
public function getByManufacturer($mfg_id, EntityManagerInterface $em)
|
|
{
|
|
$this->denyAccessUnlessGranted('vehicle.list', null, 'No access.');
|
|
|
|
$mfg = $this->em->getRepository(VehicleManufacturer::class)->find($mfg_id);
|
|
|
|
// manufacturer not found
|
|
if ($mfg == null)
|
|
{
|
|
return new APIResponse(false, 'No vehicle manufacturer found with that Id.', null, 404);
|
|
}
|
|
|
|
// get all vehicles from manufacturer
|
|
$vehicles = $mfg->getVehicles();
|
|
foreach($vehicles as $vehicle)
|
|
{
|
|
$make_data[] = [
|
|
'id' => $vehicle->getID(),
|
|
'mfg_id' => $vehicle->getManufacturer()->getID(),
|
|
'make' => $vehicle->getMake(),
|
|
'model' => $vehicle->getModelYearFormatted(),
|
|
];
|
|
}
|
|
|
|
// TODO: need to add manufacturer details
|
|
|
|
return new APIResponse(true, 'Vehicles loaded.', $data);
|
|
}
|
|
|
|
public function list(EntityManagerInterface $em)
|
|
{
|
|
$this->denyAccessUnlessGranted('vehicle.list', null, 'No access.');
|
|
|
|
$conn = $em->getConnection();
|
|
// get manufacturers
|
|
$mfg_sql = 'SELECT vmfg.id, vmfg.name FROM vehicle_manufacturer vmfg ORDER BY vmfg.name ASC';
|
|
|
|
// get manufacturer results
|
|
$mfg_stmt = $conn->prepare($mfg_sql);
|
|
$mfg_stmt->execute();
|
|
|
|
$mfg_results = $mfg_stmt->fetchAll();
|
|
|
|
// get vehicles
|
|
$vehicle_sql = 'SELECT v.id, v.manufacturer_id, v.make, v.model_year_from, v.model_year_to
|
|
FROM vehicle v ORDER BY v.manufacturer_id ASC, v.make ASC';
|
|
|
|
// get vehicle results
|
|
$vehicle_stmt = $conn->prepare($vehicle_sql);
|
|
$vehicle_stmt->execute();
|
|
|
|
$vehicle_results = $vehicle_stmt->fetchAll();
|
|
|
|
// process manufacturer results
|
|
$mfg_data = [];
|
|
foreach($mfg_results as $mfg_row)
|
|
{
|
|
$mfg_data[] = [
|
|
'id' => $mfg_row['id'],
|
|
'name' => $mfg_row['name'],
|
|
];
|
|
}
|
|
|
|
// process vehicle results
|
|
$make_data = [];
|
|
foreach($vehicle_results as $vrow)
|
|
{
|
|
// format the model year from and model year to
|
|
$model_year = $vrow['model_year_from' ] . ' - ' . $vrow['model_year_to'];
|
|
|
|
$make_data[] = [
|
|
'id' => $vrow['id'],
|
|
'mfg_id' => $vrow['manufacturer_id'],
|
|
'make' => $vrow['make'],
|
|
'model' => $model_year,
|
|
];
|
|
}
|
|
|
|
$data = [
|
|
'manufacturers' => $mfg_data,
|
|
'vehicles' => $make_data,
|
|
];
|
|
|
|
return new APIResponse(true, 'Vehicles loaded.', $data);
|
|
}
|
|
}
|