From e4cedd9e4d27ea211477b2e80f296aa18ac9b15b Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Thu, 10 Jan 2019 00:02:24 +0800 Subject: [PATCH] Add get all warranties api call #171 --- catalyst/api-bundle/Command/TestCommand.php | 8 +++++ config/routes/warranty_api.yaml | 6 ++++ src/Controller/CAPI/WarrantyController.php | 39 +++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/catalyst/api-bundle/Command/TestCommand.php b/catalyst/api-bundle/Command/TestCommand.php index 179b55b0..5b19d182 100644 --- a/catalyst/api-bundle/Command/TestCommand.php +++ b/catalyst/api-bundle/Command/TestCommand.php @@ -39,6 +39,7 @@ class TestCommand extends Command // test $api->get('/capi/test'); + /* // TODO: shift this out of the bundle, since it's project specific // warranty register $serial = 'AJ34LJADR12134LKJL'; @@ -53,6 +54,12 @@ class TestCommand extends Command 'date_expire' => '20191001', ]; $api->post('/capi/warranties', $params); + */ + + // get all warranties + $api->get('/capi/warranties'); + + /* // warranty find $api->get('/capi/warranties/' . $serial); @@ -62,6 +69,7 @@ class TestCommand extends Command // plate warranty $api->get('/capi/plates/' . $plate_num . '/warranties'); + */ // battery // $api->get('/capi/battery_models'); diff --git a/config/routes/warranty_api.yaml b/config/routes/warranty_api.yaml index 7b46b960..0aed61c5 100644 --- a/config/routes/warranty_api.yaml +++ b/config/routes/warranty_api.yaml @@ -60,6 +60,12 @@ capi_warranty_claim: controller: App\Controller\CAPI\WarrantyController::claim methods: [POST] +# get warranties +capi_warranty_get_all: + path: /capi/warranties + controller: App\Controller\CAPI\WarrantyController::getAll + methods: [GET] + # customer vehicle api diff --git a/src/Controller/CAPI/WarrantyController.php b/src/Controller/CAPI/WarrantyController.php index 969aec50..7a92eaff 100644 --- a/src/Controller/CAPI/WarrantyController.php +++ b/src/Controller/CAPI/WarrantyController.php @@ -72,6 +72,45 @@ class WarrantyController extends APIController return new APIResponse(true, 'Warranty found.', $data); } + public function getAll(Request $req, EntityManagerInterface $em) + { + error_log('HERE - getAll'); + + + $order = $req->query->get('order'); + if ($order == null) + $order = 'ASC'; + + $max = $req->query->get('limit'); + if ($max == null) + $max = 20; + + $start = $req->query->get('start'); + if ($start == null) + $start = 0; + + $qb = $em->createQueryBuilder(); + + $query = $qb->select('w') + ->from('App\\Entity\\Warranty', 'w') + ->orderBy('w.serial', $order) + ->setFirstResult($start) + ->setMaxResults($max) + ->getQuery(); + + $warrs = $query->getResult(); + + $warr_data = []; + foreach ($warrs as $warr) + $warr_data[] = $this->generateWarrantyData($warr); + + $data = [ + 'warranties' => $warr_data, + ]; + + return new APIResponse(true, 'Warranties found.', $data); + } + public function register(Request $req, EntityManagerInterface $em) { // required parameters