From 620a750465dcc0c655f9d02fe36814a4582d5cc6 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 4 Apr 2019 09:07:41 +0000 Subject: [PATCH] Add CAPI call to add a new battery. #201 --- catalyst/api-bundle/Command/TestCommand.php | 11 +++ .../routes/{warranty_api.yaml => capi.yaml} | 6 ++ src/Controller/CAPI/BatteryController.php | 68 +++++++++++++++++++ 3 files changed, 85 insertions(+) rename config/routes/{warranty_api.yaml => capi.yaml} (94%) diff --git a/catalyst/api-bundle/Command/TestCommand.php b/catalyst/api-bundle/Command/TestCommand.php index 03d1fe85..66ec639d 100644 --- a/catalyst/api-bundle/Command/TestCommand.php +++ b/catalyst/api-bundle/Command/TestCommand.php @@ -75,6 +75,17 @@ class TestCommand extends Command ]; $api->post('/capi/warranties/' . $id . '/claim', $params); + // add battery + $sku = 'WZMB31QT-CPP00-S'; + $brand = '4'; + $size = '1'; + $params = [ + 'sku' => $sku, + 'brand' => $brand, + 'size' => $size, + ]; + $api->post('/capi/batteries', $params); + /* // plate warranty diff --git a/config/routes/warranty_api.yaml b/config/routes/capi.yaml similarity index 94% rename from config/routes/warranty_api.yaml rename to config/routes/capi.yaml index 85075440..a340041c 100644 --- a/config/routes/warranty_api.yaml +++ b/config/routes/capi.yaml @@ -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 diff --git a/src/Controller/CAPI/BatteryController.php b/src/Controller/CAPI/BatteryController.php index a8197bab..dc035c58 100644 --- a/src/Controller/CAPI/BatteryController.php +++ b/src/Controller/CAPI/BatteryController.php @@ -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', + 'size', + ]; + + $msg = $this->checkRequiredParameters($req, $params); + error_log('msg - ' . $msg); + if ($msg) + return new APIResponse(false, $msg); + + $sku = $req->request->get('sku'); + $brand = $req->request->get('brand'); + $size = $req->request->get('size'); + + // 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); + if ($batt_brand == null) + return new APIResponse(false, 'Invalid brand.'); + + // check if size exists + $batt_size = $em->getRepository(SAPBatterySize::class)->find($size); + 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; + } }