Modify BatteryController. #591
This commit is contained in:
parent
2ce5f05885
commit
cfbda4efe0
3 changed files with 40 additions and 99 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue