Merge branch '166-warranty-api-improvements' into 'master'

Resolve "Warranty API improvements"

Closes #166

See merge request jankstudio/resq!199
This commit is contained in:
Kendrick Chan 2018-11-05 17:58:10 +00:00
commit 15ad135d48
8 changed files with 144 additions and 45 deletions

View file

@ -41,20 +41,27 @@ class TestCommand extends Command
// TODO: shift this out of the bundle, since it's project specific
// warranty register
$serial = 'AJ34LJADR12134LKJL';
$plate_num = 'XEN918';
$params = [
'serial' => 'LJ34LJADR12SDLKJL',
'plate_number' => 'XEN918',
'serial' => $serial,
'plate_number' => $plate_num,
'warranty_class' => 'private',
'battery_model_id' => 438,
'battery_size_id' => 1171,
'date_purchase' => '20181001',
'date_expire' => '20191001',
];
$api->post('/capi/warranty', $params);
$api->post('/capi/warranties', $params);
// warranty find
$api->get('/capi/warranty/LJ34LJADR12SDLKJL');
$api->get('/capi/warranties/' . $serial);
// warranty claim
$api->post('/capi/warranty/LJ34LJADR12SDLKJL/claim');
$api->post('/capi/warranties/' . $serial . '/claim');
// plate warranty
$api->get('/capi/plates/' . $plate_num . '/warranties');
// battery
// $api->get('/capi/battery_models');

View file

@ -2,6 +2,40 @@
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;
}
}

View file

@ -32,23 +32,31 @@ capi_vehicle_list:
methods: [GET]
# plate api
capi_plate_warranty:
path: /capi/plates/{plate_number}/warranties
controller: App\Controller\CAPI\WarrantyController::getPlateWarranties
methods: [GET]
# warranty api
# check warranty by serial
capi_warranty_find:
path: /capi/warranty/{serial}
path: /capi/warranties/{serial}
controller: App\Controller\CAPI\WarrantyController::find
methods: [GET]
# register battery
capi_warranty_register:
path: /capi/warranty
path: /capi/warranties
controller: App\Controller\CAPI\WarrantyController::register
methods: [POST]
# claim warranty
capi_warranty_claim:
path: /capi/warranty/{serial}/claim
path: /capi/warranties/{serial}/claim
controller: App\Controller\CAPI\WarrantyController::claim
methods: [POST]

View file

@ -11,7 +11,7 @@ use Catalyst\APIBundle\Response\APIResponse;
use App\Entity\BatteryModel;
use App\Entity\BatterySize;
class BatteryController extends Controller implements APIController
class BatteryController extends APIController
{
public function getModels(EntityManagerInterface $em)
{

View file

@ -8,7 +8,7 @@ use Doctrine\ORM\Query;
use Catalyst\APIBundle\Controller\APIController;
use Catalyst\APIBundle\Response\APIResponse;
class TestController extends Controller implements APIController
class TestController extends APIController
{
public function test()
{

View file

@ -11,7 +11,7 @@ use Catalyst\APIBundle\Response\APIResponse;
use App\Entity\Vehicle;
use App\Entity\VehicleManufacturer;
class VehicleController extends Controller implements APIController
class VehicleController extends APIController
{
public function getManufacturers(EntityManagerInterface $em)
{

View file

@ -10,45 +10,15 @@ use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Catalyst\APIBundle\Controller\APIController;
use Catalyst\APIBundle\Response\APIResponse;
use App\Entity\Warranty;
use App\Entity\BatteryModel;
use App\Entity\BatterySize;
use App\Ramcar\NameValue;
use App\Ramcar\WarrantyClass;
use App\Ramcar\WarrantyStatus;
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)
{
return trim(strtoupper($serial));
@ -56,11 +26,21 @@ class WarrantyController extends Controller implements APIController
protected function generateWarrantyData(Warranty $warr)
{
$model = $warr->getBatteryModel();
$size = $warr->getBatterySize();
$data = [
'id' => (int) $warr->getID(),
'serial' => (string) $warr->getSerial(),
'warranty_class' => (string) $warr->getWarrantyClass(),
'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(),
'date_create' => (string) $warr->getDateCreate()->format('YmdHis'),
'date_purchase' => (string) $warr->getDatePurchase()->format('Ymd'),
@ -101,6 +81,8 @@ class WarrantyController extends Controller implements APIController
'plate_number',
'date_expire',
'date_purchase',
'battery_model_id',
'battery_size_id',
];
$msg = $this->checkRequiredParameters($req, $params);
error_log('msg - ' . $msg);
@ -112,6 +94,8 @@ class WarrantyController extends Controller implements APIController
$date_pur_string = $req->request->get('date_purchase');
$warr_class = $req->request->get('warranty_class');
$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
$date_expire = DateTime::createFromFormat('Ymd', $date_expire_string);
@ -132,11 +116,23 @@ class WarrantyController extends Controller implements APIController
if (!$plate)
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
$warr = new Warranty();
$warr->setSerial($serial)
->setWarrantyClass($warr_class)
->setPlateNumber($plate)
->setBatteryModel($model)
->setBatterySize($size)
->setDatePurchase($date_pur)
->setDateClaim(null)
->setDateExpire($date_expire);
@ -182,4 +178,22 @@ class WarrantyController extends Controller implements APIController
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);
}
}

View file

@ -45,6 +45,20 @@ class Warranty
*/
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
/**
* @ORM\Column(type="string", length=25)
@ -145,6 +159,28 @@ class Warranty
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)
{
$this->status = $status;