From cfbda4efe0f5ac469e72e9a92912c893e349469a Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 6 Jul 2021 05:43:13 +0000 Subject: [PATCH] Modify BatteryController. #591 --- config/api_acl.yaml | 5 + src/Controller/ResqAPI/BatteryController.php | 124 ++++--------------- src/Service/MobileAPIHandler.php | 10 ++ 3 files changed, 40 insertions(+), 99 deletions(-) diff --git a/config/api_acl.yaml b/config/api_acl.yaml index 9c951917..09f6429c 100644 --- a/config/api_acl.yaml +++ b/config/api_acl.yaml @@ -115,3 +115,8 @@ access_keys: acls: - id: mobile_promo.list label: List Mobile Promos + - id: mobile_battery + label: Mobile Battery Access + acls: + - id: mobile_battery.list + label: List Compatible Batteries diff --git a/src/Controller/ResqAPI/BatteryController.php b/src/Controller/ResqAPI/BatteryController.php index 3f44294d..8f2e2ba6 100644 --- a/src/Controller/ResqAPI/BatteryController.php +++ b/src/Controller/ResqAPI/BatteryController.php @@ -9,13 +9,13 @@ use Doctrine\ORM\Query; use Doctrine\ORM\EntityManagerInterface; use Catalyst\APIBundle\Controller\APIController; -// TODO: what do we use for response? APIResponse or APIResult? -// APIResult is what is used by APIController. APIResponse is what is used by CAPI use Catalyst\APIBundle\Response\APIResponse; use App\Ramcar\APIResult; use App\Entity\Vehicle; +use App\Service\MobileAPIHandler; + use Catalyst\APIBundle\Access\Generator as ACLGenerator; class BatteryController extends APIController @@ -27,28 +27,39 @@ class BatteryController extends APIController $this->acl_gen = $acl_gen; } - public function getCompatibleBatteries(Request $req, $vid, EntityManagerInterface $em) + public function getCompatibleBatteries(Request $req, $vid, EntityManagerInterface $em, + MobileAPIHandler $mah) { - // check required parameters and api key + $this->denyAccessUnlessGranted('mobile_battery.list', null, 'No access.'); + + // check required parameters $required_params = []; - $res = $this->checkParamsAndKey($req, $em, $required_params); - if ($res->isError()) - return $res->getReturnResponse(); + $msg = $this->checkRequiredParameters($req, $required_params); + if ($msg) + return new APIResponse(false, $msg); + + // get capi user to link to mobile user + $user_id = $this->getUser()->getID(); + + // get mobile user + $mobile_user = $mah->findMobileUser($em, $user_id); + + if ($mobile_user == null) + return new APIResponse(false, 'No mobile user found.'); // get vehicle $vehicle = $em->getRepository(Vehicle::class)->find($vid); if ($vehicle == null) - { - $res->setError(true) - ->setErrorMessage('Invalid vehicle'); - return $res->getReturnResponse(); - } + return new APIResponse(false, 'Invalid vehicle'); // batteries $batt_list = []; $batts = $vehicle->getBatteries(); foreach ($batts as $batt) { + // generate the url for image + $battery_image_url = $this->generateUrl('static_battery_image'); + // TODO: Add warranty_tnv to battery information $batt_list[] = [ 'id' => $batt->getID(), @@ -61,7 +72,7 @@ class BatteryController extends APIController 'price' => $batt->getSellingPrice(), 'wty_private' => $batt->getWarrantyPrivate(), 'wty_commercial' => $batt->getWarrantyCommercial(), - 'image_url' => $this->getBatteryImageURL($req, $batt), + 'image_url' => $mah->getBatteryImageURL($req, $batt, $battery_image_url), ]; } @@ -77,92 +88,7 @@ class BatteryController extends APIController ], 'batteries' => $batt_list, ]; - $res->setData($data); - return $res->getReturnResponse(); - } - - // TODO: since we broke the functions into separate files, we need - // to figure out how to make this accessible to all ResqAPI controllers - protected function checkParamsAndKey(Request $req, $em, $params) - { - // TODO: depends on what we decide to return - // returns APIResult object - $res = new APIResult(); - - // check for api_key in query string - $api_key = $req->query->get('api_key'); - if (empty($api_key)) - { - $res->setError(true) - ->setErrorMessage('Missing API key'); - return $res; - } - - // check missing parameters - $missing = $this->checkMissingParameters($req, $params); - if (count($missing) > 0) - { - $miss_string = implode(', ', $missing); - $res->setError(true) - ->setErrorMessage('Missing parameter(s): ' . $miss_string); - return $res; - } - - // check api key - $mobile_user = $this->checkAPIKey($em, $req->query->get('api_key')); - if ($mobile_user == null) - { - $res->setError(true) - ->setErrorMessage('Invalid API Key'); - return $res; - } - - // store session - $this->session = $sess; - - return $res; - } - - // TODO: this might not be needed if we use APIController's checkRequiredParameters - // or we put this into a service? - protected function checkMissingParameters(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') - { - $check = $req->request->get($param); - if (empty($check)) - $missing[] = $param; - } - else - return $params; - } - - return $missing; - } - - // TODO: type hint entity manager - // TODO: since we broke the functions into separate files, we need - // to figure out how to make this accessible to all ResqAPI controllers - protected function checkAPIKey($em, $api_key) - { - // find the api key (session id) - // TODO: user validation needs to be changed - $m_user = $em->getRepository(MobileUser::class)->find($api_key); - if ($m_user == null) - return null; - - return $m_user; + return new APIResponse(true, 'Compatible batteries found', $data); } } diff --git a/src/Service/MobileAPIHandler.php b/src/Service/MobileAPIHandler.php index f95ef6ad..4563eeff 100644 --- a/src/Service/MobileAPIHandler.php +++ b/src/Service/MobileAPIHandler.php @@ -86,4 +86,14 @@ class MobileAPIHandler return $warr; } + public function getBatteryImageURL($req, $batt, $battery_image_url) + { + // TODO: workaround for now, we get static image of battery based on model name + $filename = trim(strtolower($batt->getModel()->getName())) . '_mobile.jpg'; + $file_path = $req->getSchemeAndHttpHost() . $battery_image_url . '/' . $filename; + + return $file_path; + } + + }