Resolve "Warranty API improvements" #1010
8 changed files with 144 additions and 45 deletions
|
|
@ -41,20 +41,27 @@ class TestCommand extends Command
|
||||||
|
|
||||||
// TODO: shift this out of the bundle, since it's project specific
|
// TODO: shift this out of the bundle, since it's project specific
|
||||||
// warranty register
|
// warranty register
|
||||||
|
$serial = 'AJ34LJADR12134LKJL';
|
||||||
|
$plate_num = 'XEN918';
|
||||||
$params = [
|
$params = [
|
||||||
'serial' => 'LJ34LJADR12SDLKJL',
|
'serial' => $serial,
|
||||||
'plate_number' => 'XEN918',
|
'plate_number' => $plate_num,
|
||||||
'warranty_class' => 'private',
|
'warranty_class' => 'private',
|
||||||
|
'battery_model_id' => 438,
|
||||||
|
'battery_size_id' => 1171,
|
||||||
'date_purchase' => '20181001',
|
'date_purchase' => '20181001',
|
||||||
'date_expire' => '20191001',
|
'date_expire' => '20191001',
|
||||||
];
|
];
|
||||||
$api->post('/capi/warranty', $params);
|
$api->post('/capi/warranties', $params);
|
||||||
|
|
||||||
// warranty find
|
// warranty find
|
||||||
$api->get('/capi/warranty/LJ34LJADR12SDLKJL');
|
$api->get('/capi/warranties/' . $serial);
|
||||||
|
|
||||||
// warranty claim
|
// warranty claim
|
||||||
$api->post('/capi/warranty/LJ34LJADR12SDLKJL/claim');
|
$api->post('/capi/warranties/' . $serial . '/claim');
|
||||||
|
|
||||||
|
// plate warranty
|
||||||
|
$api->get('/capi/plates/' . $plate_num . '/warranties');
|
||||||
|
|
||||||
// battery
|
// battery
|
||||||
// $api->get('/capi/battery_models');
|
// $api->get('/capi/battery_models');
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,40 @@
|
||||||
|
|
||||||
namespace Catalyst\APIBundle\Controller;
|
namespace Catalyst\APIBundle\Controller;
|
||||||
|
|
||||||
interface APIController
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
|
abstract class APIController extends Controller
|
||||||
{
|
{
|
||||||
|
protected function checkRequiredParameters(Request $req, $params = [])
|
||||||
|
{
|
||||||
|
$missing = [];
|
||||||
|
|
||||||
|
// check if parameters are there
|
||||||
|
foreach ($params as $param)
|
||||||
|
{
|
||||||
|
if ($req->getMethod() == 'GET')
|
||||||
|
{
|
||||||
|
$check = $req->query->get($param);
|
||||||
|
if (empty($check))
|
||||||
|
$missing[] = $param;
|
||||||
|
}
|
||||||
|
// else if ($req->getMethod() == 'POST')
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$check = $req->request->get($param);
|
||||||
|
if (empty($check))
|
||||||
|
$missing[] = $param;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check missing parameters
|
||||||
|
if (count($missing) > 0)
|
||||||
|
{
|
||||||
|
$miss_string = implode(', ', $missing);
|
||||||
|
return 'Missing required parameter(s): ' . $miss_string;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,23 +32,31 @@ capi_vehicle_list:
|
||||||
methods: [GET]
|
methods: [GET]
|
||||||
|
|
||||||
|
|
||||||
|
# plate api
|
||||||
|
|
||||||
|
capi_plate_warranty:
|
||||||
|
path: /capi/plates/{plate_number}/warranties
|
||||||
|
controller: App\Controller\CAPI\WarrantyController::getPlateWarranties
|
||||||
|
methods: [GET]
|
||||||
|
|
||||||
|
|
||||||
# warranty api
|
# warranty api
|
||||||
|
|
||||||
# check warranty by serial
|
# check warranty by serial
|
||||||
capi_warranty_find:
|
capi_warranty_find:
|
||||||
path: /capi/warranty/{serial}
|
path: /capi/warranties/{serial}
|
||||||
controller: App\Controller\CAPI\WarrantyController::find
|
controller: App\Controller\CAPI\WarrantyController::find
|
||||||
methods: [GET]
|
methods: [GET]
|
||||||
|
|
||||||
# register battery
|
# register battery
|
||||||
capi_warranty_register:
|
capi_warranty_register:
|
||||||
path: /capi/warranty
|
path: /capi/warranties
|
||||||
controller: App\Controller\CAPI\WarrantyController::register
|
controller: App\Controller\CAPI\WarrantyController::register
|
||||||
methods: [POST]
|
methods: [POST]
|
||||||
|
|
||||||
# claim warranty
|
# claim warranty
|
||||||
capi_warranty_claim:
|
capi_warranty_claim:
|
||||||
path: /capi/warranty/{serial}/claim
|
path: /capi/warranties/{serial}/claim
|
||||||
controller: App\Controller\CAPI\WarrantyController::claim
|
controller: App\Controller\CAPI\WarrantyController::claim
|
||||||
methods: [POST]
|
methods: [POST]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ use Catalyst\APIBundle\Response\APIResponse;
|
||||||
use App\Entity\BatteryModel;
|
use App\Entity\BatteryModel;
|
||||||
use App\Entity\BatterySize;
|
use App\Entity\BatterySize;
|
||||||
|
|
||||||
class BatteryController extends Controller implements APIController
|
class BatteryController extends APIController
|
||||||
{
|
{
|
||||||
public function getModels(EntityManagerInterface $em)
|
public function getModels(EntityManagerInterface $em)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use Doctrine\ORM\Query;
|
||||||
use Catalyst\APIBundle\Controller\APIController;
|
use Catalyst\APIBundle\Controller\APIController;
|
||||||
use Catalyst\APIBundle\Response\APIResponse;
|
use Catalyst\APIBundle\Response\APIResponse;
|
||||||
|
|
||||||
class TestController extends Controller implements APIController
|
class TestController extends APIController
|
||||||
{
|
{
|
||||||
public function test()
|
public function test()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ use Catalyst\APIBundle\Response\APIResponse;
|
||||||
use App\Entity\Vehicle;
|
use App\Entity\Vehicle;
|
||||||
use App\Entity\VehicleManufacturer;
|
use App\Entity\VehicleManufacturer;
|
||||||
|
|
||||||
class VehicleController extends Controller implements APIController
|
class VehicleController extends APIController
|
||||||
{
|
{
|
||||||
public function getManufacturers(EntityManagerInterface $em)
|
public function getManufacturers(EntityManagerInterface $em)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -10,45 +10,15 @@ use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
|
||||||
use Catalyst\APIBundle\Controller\APIController;
|
use Catalyst\APIBundle\Controller\APIController;
|
||||||
use Catalyst\APIBundle\Response\APIResponse;
|
use Catalyst\APIBundle\Response\APIResponse;
|
||||||
use App\Entity\Warranty;
|
use App\Entity\Warranty;
|
||||||
|
use App\Entity\BatteryModel;
|
||||||
|
use App\Entity\BatterySize;
|
||||||
use App\Ramcar\NameValue;
|
use App\Ramcar\NameValue;
|
||||||
use App\Ramcar\WarrantyClass;
|
use App\Ramcar\WarrantyClass;
|
||||||
use App\Ramcar\WarrantyStatus;
|
use App\Ramcar\WarrantyStatus;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
|
|
||||||
class WarrantyController extends Controller implements APIController
|
class WarrantyController extends APIController
|
||||||
{
|
{
|
||||||
protected function checkRequiredParameters(Request $req, $params = [])
|
|
||||||
{
|
|
||||||
$missing = [];
|
|
||||||
|
|
||||||
// check if parameters are there
|
|
||||||
foreach ($params as $param)
|
|
||||||
{
|
|
||||||
if ($req->getMethod() == 'GET')
|
|
||||||
{
|
|
||||||
$check = $req->query->get($param);
|
|
||||||
if (empty($check))
|
|
||||||
$missing[] = $param;
|
|
||||||
}
|
|
||||||
// else if ($req->getMethod() == 'POST')
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$check = $req->request->get($param);
|
|
||||||
if (empty($check))
|
|
||||||
$missing[] = $param;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// check missing parameters
|
|
||||||
if (count($missing) > 0)
|
|
||||||
{
|
|
||||||
$miss_string = implode(', ', $missing);
|
|
||||||
return 'Missing required parameter(s): ' . $miss_string;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function cleanSerial($serial)
|
protected function cleanSerial($serial)
|
||||||
{
|
{
|
||||||
return trim(strtoupper($serial));
|
return trim(strtoupper($serial));
|
||||||
|
|
@ -56,11 +26,21 @@ class WarrantyController extends Controller implements APIController
|
||||||
|
|
||||||
protected function generateWarrantyData(Warranty $warr)
|
protected function generateWarrantyData(Warranty $warr)
|
||||||
{
|
{
|
||||||
|
$model = $warr->getBatteryModel();
|
||||||
|
$size = $warr->getBatterySize();
|
||||||
$data = [
|
$data = [
|
||||||
'id' => (int) $warr->getID(),
|
'id' => (int) $warr->getID(),
|
||||||
'serial' => (string) $warr->getSerial(),
|
'serial' => (string) $warr->getSerial(),
|
||||||
'warranty_class' => (string) $warr->getWarrantyClass(),
|
'warranty_class' => (string) $warr->getWarrantyClass(),
|
||||||
'plate_number' => (string) $warr->getPlateNumber(),
|
'plate_number' => (string) $warr->getPlateNumber(),
|
||||||
|
'battery_model' => [
|
||||||
|
(int) ($model == null ? 0 : $model->getID()),
|
||||||
|
(string) ($model == null ? '' : $model->getName()),
|
||||||
|
],
|
||||||
|
'battery_size' => [
|
||||||
|
(int) ($size == null ? 0 : $size->getID()),
|
||||||
|
(string) ($size == null ? '' : $size->getName()),
|
||||||
|
],
|
||||||
'status' => (string) $warr->getStatus(),
|
'status' => (string) $warr->getStatus(),
|
||||||
'date_create' => (string) $warr->getDateCreate()->format('YmdHis'),
|
'date_create' => (string) $warr->getDateCreate()->format('YmdHis'),
|
||||||
'date_purchase' => (string) $warr->getDatePurchase()->format('Ymd'),
|
'date_purchase' => (string) $warr->getDatePurchase()->format('Ymd'),
|
||||||
|
|
@ -101,6 +81,8 @@ class WarrantyController extends Controller implements APIController
|
||||||
'plate_number',
|
'plate_number',
|
||||||
'date_expire',
|
'date_expire',
|
||||||
'date_purchase',
|
'date_purchase',
|
||||||
|
'battery_model_id',
|
||||||
|
'battery_size_id',
|
||||||
];
|
];
|
||||||
$msg = $this->checkRequiredParameters($req, $params);
|
$msg = $this->checkRequiredParameters($req, $params);
|
||||||
error_log('msg - ' . $msg);
|
error_log('msg - ' . $msg);
|
||||||
|
|
@ -112,6 +94,8 @@ class WarrantyController extends Controller implements APIController
|
||||||
$date_pur_string = $req->request->get('date_purchase');
|
$date_pur_string = $req->request->get('date_purchase');
|
||||||
$warr_class = $req->request->get('warranty_class');
|
$warr_class = $req->request->get('warranty_class');
|
||||||
$plate = $req->request->get('plate_number');
|
$plate = $req->request->get('plate_number');
|
||||||
|
$bmodel_id = $req->request->get('battery_model_id');
|
||||||
|
$bsize_id = $req->request->get('battery_size_id');
|
||||||
|
|
||||||
// wrong date expire format
|
// wrong date expire format
|
||||||
$date_expire = DateTime::createFromFormat('Ymd', $date_expire_string);
|
$date_expire = DateTime::createFromFormat('Ymd', $date_expire_string);
|
||||||
|
|
@ -132,11 +116,23 @@ class WarrantyController extends Controller implements APIController
|
||||||
if (!$plate)
|
if (!$plate)
|
||||||
return new APIResponse(false, 'Invalid plate number.');
|
return new APIResponse(false, 'Invalid plate number.');
|
||||||
|
|
||||||
|
// battery model
|
||||||
|
$model = $em->getRepository(BatteryModel::class)->find($bmodel_id);
|
||||||
|
if ($model == null)
|
||||||
|
return new APIResponse(false, 'Invalid battery model id.');
|
||||||
|
|
||||||
|
// battery size
|
||||||
|
$size = $em->getRepository(BatterySize::class)->find($bsize_id);
|
||||||
|
if ($size == null)
|
||||||
|
return new APIResponse(false, 'Invalid battery size id.');
|
||||||
|
|
||||||
// warranty
|
// warranty
|
||||||
$warr = new Warranty();
|
$warr = new Warranty();
|
||||||
$warr->setSerial($serial)
|
$warr->setSerial($serial)
|
||||||
->setWarrantyClass($warr_class)
|
->setWarrantyClass($warr_class)
|
||||||
->setPlateNumber($plate)
|
->setPlateNumber($plate)
|
||||||
|
->setBatteryModel($model)
|
||||||
|
->setBatterySize($size)
|
||||||
->setDatePurchase($date_pur)
|
->setDatePurchase($date_pur)
|
||||||
->setDateClaim(null)
|
->setDateClaim(null)
|
||||||
->setDateExpire($date_expire);
|
->setDateExpire($date_expire);
|
||||||
|
|
@ -182,4 +178,22 @@ class WarrantyController extends Controller implements APIController
|
||||||
|
|
||||||
return new APIResponse(true, 'Warranty claimed successfully.');
|
return new APIResponse(true, 'Warranty claimed successfully.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getPlateWarranties($plate_number, EntityManagerInterface $em)
|
||||||
|
{
|
||||||
|
$warranties = $em->getRepository(Warranty::class)
|
||||||
|
->findBy(['plate_number' => $plate_number], ['date_purchase' => 'DESC']);
|
||||||
|
|
||||||
|
$warr_data = [];
|
||||||
|
foreach ($warranties as $warr)
|
||||||
|
{
|
||||||
|
$warr_data[] = $this->generateWarrantyData($warr);
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = [
|
||||||
|
'warranties' => $warr_data,
|
||||||
|
];
|
||||||
|
|
||||||
|
return new APIResponse(true, 'Warranties loaded.', $data);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -45,6 +45,20 @@ class Warranty
|
||||||
*/
|
*/
|
||||||
protected $plate_number;
|
protected $plate_number;
|
||||||
|
|
||||||
|
// battery model
|
||||||
|
/**
|
||||||
|
* @ORM\ManyToOne(targetEntity="BatteryModel", inversedBy="warranties")
|
||||||
|
* @ORM\JoinColumn(name="bty_model_id", referencedColumnName="id")
|
||||||
|
*/
|
||||||
|
protected $bty_model;
|
||||||
|
|
||||||
|
// battery size
|
||||||
|
/**
|
||||||
|
* @ORM\ManyToOne(targetEntity="BatterySize", inversedBy="warranties")
|
||||||
|
* @ORM\JoinColumn(name="bty_size_id", referencedColumnName="id")
|
||||||
|
*/
|
||||||
|
protected $bty_size;
|
||||||
|
|
||||||
// status
|
// status
|
||||||
/**
|
/**
|
||||||
* @ORM\Column(type="string", length=25)
|
* @ORM\Column(type="string", length=25)
|
||||||
|
|
@ -145,6 +159,28 @@ class Warranty
|
||||||
return $this->plate_number;
|
return $this->plate_number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function setBatteryModel(BatteryModel $model)
|
||||||
|
{
|
||||||
|
$this->bty_model = $model;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getBatteryModel()
|
||||||
|
{
|
||||||
|
return $this->bty_model;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setBatterySize(BatterySize $size)
|
||||||
|
{
|
||||||
|
$this->bty_size = $size;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getBatterySize()
|
||||||
|
{
|
||||||
|
return $this->bty_size;
|
||||||
|
}
|
||||||
|
|
||||||
public function setStatus($status)
|
public function setStatus($status)
|
||||||
{
|
{
|
||||||
$this->status = $status;
|
$this->status = $status;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue