diff --git a/catalyst/api-bundle/Command/TestCommand.php b/catalyst/api-bundle/Command/TestCommand.php index 9a12caad..179b55b0 100644 --- a/catalyst/api-bundle/Command/TestCommand.php +++ b/catalyst/api-bundle/Command/TestCommand.php @@ -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'); diff --git a/catalyst/api-bundle/Controller/APIController.php b/catalyst/api-bundle/Controller/APIController.php index fc38e32b..7f359680 100644 --- a/catalyst/api-bundle/Controller/APIController.php +++ b/catalyst/api-bundle/Controller/APIController.php @@ -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; + } } diff --git a/config/routes/warranty_api.yaml b/config/routes/warranty_api.yaml index bfc4d731..7b46b960 100644 --- a/config/routes/warranty_api.yaml +++ b/config/routes/warranty_api.yaml @@ -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] diff --git a/src/Controller/CAPI/BatteryController.php b/src/Controller/CAPI/BatteryController.php index f99d0ad5..e42a220b 100644 --- a/src/Controller/CAPI/BatteryController.php +++ b/src/Controller/CAPI/BatteryController.php @@ -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) { diff --git a/src/Controller/CAPI/TestController.php b/src/Controller/CAPI/TestController.php index 0143166a..e1a5ea08 100644 --- a/src/Controller/CAPI/TestController.php +++ b/src/Controller/CAPI/TestController.php @@ -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() { diff --git a/src/Controller/CAPI/VehicleController.php b/src/Controller/CAPI/VehicleController.php index 67527bb3..0d9ab174 100644 --- a/src/Controller/CAPI/VehicleController.php +++ b/src/Controller/CAPI/VehicleController.php @@ -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) { diff --git a/src/Controller/CAPI/WarrantyController.php b/src/Controller/CAPI/WarrantyController.php index 433f710f..969aec50 100644 --- a/src/Controller/CAPI/WarrantyController.php +++ b/src/Controller/CAPI/WarrantyController.php @@ -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); + } } diff --git a/src/Entity/Warranty.php b/src/Entity/Warranty.php index 040c6c49..a7b538af 100644 --- a/src/Entity/Warranty.php +++ b/src/Entity/Warranty.php @@ -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;