Merge branch '201-new-capi-call' into 'master'

Resolve "new CAPI call"

Closes #201

See merge request jankstudio/resq!235
This commit is contained in:
Kendrick Chan 2019-04-05 06:09:23 +00:00
commit 2c5b0b250c
3 changed files with 85 additions and 0 deletions

View file

@ -75,6 +75,17 @@ class TestCommand extends Command
];
$api->post('/capi/warranties/' . $id . '/claim', $params);
// add battery
$sku = 'WZMB31QT-CPP00-S';
$brand_id = '4';
$size_id = '1';
$params = [
'sku' => $sku,
'brand_id' => $brand_id,
'size_id' => $size_id,
];
$api->post('/capi/batteries', $params);
/*
// plate warranty

View file

@ -30,6 +30,12 @@ capi_battery_sizes:
controller: App\Controller\CAPI\BatteryController::getSizes
methods: [GET]
# add battery
capi_battery_add:
path: /capi/batteries
controller: App\Controller\CAPI\BatteryController::addBattery
methods: [POST]
# vehicle api

View file

@ -75,4 +75,72 @@ class BatteryController extends APIController
return new APIResponse(true, 'Battery sizes loaded.', $data);
}
public function addBattery(Request $req, EntityManagerInterface $em)
{
// required parameters
$params = [
'sku',
'brand_id',
'size_id',
];
$msg = $this->checkRequiredParameters($req, $params);
error_log('msg - ' . $msg);
if ($msg)
return new APIResponse(false, $msg);
$sku = $req->request->get('sku');
$brand_id = $req->request->get('brand_id');
$size_id = $req->request->get('size_id');
// check if sku already exists
$batt = $em->getRepository(SAPBattery::class)->find($sku);
if ($batt != null)
return new APIResponse(false, 'Battery SKU already exists.');
// check if brand exists
$batt_brand = $em->getRepository(SAPBatteryBrand::class)->find($brand_id);
if ($batt_brand == null)
return new APIResponse(false, 'Invalid brand.');
// check if size exists
$batt_size = $em->getRepository(SAPBatterySize::class)->find($size_id);
if ($batt_size == null)
return new APIResponse(false, 'Invalid size.');
$new_batt = new SAPBattery();
$new_batt->setID($sku)
->setBrand($batt_brand)
->setSize($batt_size);
try
{
$em->persist($new_batt);
$em->flush();
}
catch (UniqueConstraintViolationException $e)
{
return new APIResponse(false, 'Battery SKU already exists.');
}
// return the new battery data
$data = [
'battery' => $this->generateBatteryData($new_batt),
];
return new APIResponse(true, 'Battery added.', $data);
}
protected function generateBatteryData(SAPBattery $batt)
{
$data = [
'sku' => $batt->getID(),
'brand' => $batt->getBrand()->getID(),
'size' => $batt->getSize()->getID(),
];
return $data;
}
}