Add session register rider api call #119
This commit is contained in:
parent
87c6ca1926
commit
9d8a3fe6e4
2 changed files with 237 additions and 0 deletions
22
config/routes/rider_api.yaml
Normal file
22
config/routes/rider_api.yaml
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# rider app api
|
||||
|
||||
rapi_register:
|
||||
path: /rapi/register
|
||||
controller: App\Controller\RAPIController::register
|
||||
methods: [POST]
|
||||
|
||||
rapi_login:
|
||||
path: /rapi/login
|
||||
controller: App\Controller\RAPIController::login
|
||||
methods: [POST]
|
||||
|
||||
rapi_get_status:
|
||||
path: /rapi/status
|
||||
controller: App\Controller\RAPIController::getStatus
|
||||
methods: [GET]
|
||||
|
||||
rapi_set_status:
|
||||
path: /rapi/status
|
||||
controller: App\Controller\RAPIController::setStatus
|
||||
methods: [POST]
|
||||
|
||||
215
src/Controller/RAPIController.php
Normal file
215
src/Controller/RAPIController.php
Normal file
|
|
@ -0,0 +1,215 @@
|
|||
<?php
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Doctrine\DBAL\DBALException;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
|
||||
use CrEOF\Spatial\PHP\Types\Geometry\Point;
|
||||
|
||||
use App\Ramcar\APIResult;
|
||||
use App\Ramcar\JOStatus;
|
||||
use App\Ramcar\InvoiceCriteria;
|
||||
use App\Ramcar\ServiceType;
|
||||
use App\Ramcar\WarrantyClass;
|
||||
use App\Ramcar\APIRiderStatus;
|
||||
use App\Ramcar\TransactionOrigin;
|
||||
use App\Ramcar\TradeInType;
|
||||
|
||||
use App\Service\InvoiceCreator;
|
||||
|
||||
use App\Entity\RiderSession;
|
||||
use App\Entity\Customer;
|
||||
use App\Entity\VehicleManufacturer;
|
||||
use App\Entity\Vehicle;
|
||||
use App\Entity\CustomerVehicle;
|
||||
use App\Entity\JobOrder;
|
||||
use App\Entity\Promo;
|
||||
use App\Entity\Battery;
|
||||
use App\Entity\RiderRating;
|
||||
|
||||
use DateTime;
|
||||
|
||||
|
||||
// Rider API controller
|
||||
class RAPIController extends Controller
|
||||
{
|
||||
protected $session;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// one device = one session, since we have control over the devices
|
||||
// when a rider logs in, we just change the rider assigned to the device
|
||||
// when a rider logs out, we remove the rider assigned to the device
|
||||
$this->session = null;
|
||||
}
|
||||
|
||||
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
|
||||
protected function checkAPIKey($em, $api_key)
|
||||
{
|
||||
// find the api key (session id)
|
||||
$session = $em->getRepository(RiderSession::class)->find($api_key);
|
||||
if ($session == null)
|
||||
return null;
|
||||
|
||||
return $session;
|
||||
}
|
||||
|
||||
protected function checkParamsAndKey(Request $req, $em, $params)
|
||||
{
|
||||
// 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
|
||||
$sess = $this->checkAPIKey($em, $req->query->get('api_key'));
|
||||
if ($sess == null)
|
||||
{
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Invalid API Key');
|
||||
return $res;
|
||||
}
|
||||
|
||||
// store session
|
||||
$this->session = $sess;
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
public function register(Request $req)
|
||||
{
|
||||
$res = new APIResult();
|
||||
|
||||
// confirm parameters
|
||||
$required_params = [
|
||||
'phone_number',
|
||||
'device_push_id'
|
||||
];
|
||||
|
||||
$missing = $this->checkMissingParameters($req, $required_params);
|
||||
if (count($missing) > 0)
|
||||
{
|
||||
$params = implode(', ', $missing);
|
||||
$res->setError(true)
|
||||
->setErrorMessage('Missing parameter(s): ' . $params);
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
// retry until we get a unique id
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
// instantiate session
|
||||
$sess = new RiderSession();
|
||||
$sess->setPhoneNumber($req->request->get('phone_number'))
|
||||
->setDevicePushID($req->request->get('device_push_id'));
|
||||
|
||||
// reopen in case we get an exception
|
||||
if (!$em->isOpen())
|
||||
{
|
||||
$em = $em->create(
|
||||
$em->getConnection(),
|
||||
$em->getConfiguration()
|
||||
);
|
||||
}
|
||||
|
||||
// save
|
||||
$em->persist($sess);
|
||||
$em->flush();
|
||||
}
|
||||
catch (DBALException $e)
|
||||
{
|
||||
error_log($e->getMessage());
|
||||
// delay one second and try again
|
||||
sleep(1);
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
// return data
|
||||
$data = [
|
||||
'session_id' => $sess->getID()
|
||||
];
|
||||
$res->setData($data);
|
||||
|
||||
|
||||
// response
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
public function login(Request $req)
|
||||
{
|
||||
$required_params = [
|
||||
'user',
|
||||
'pass',
|
||||
];
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$res = $this->checkParamsAndKey($req, $em, $required_params);
|
||||
if ($res->isError())
|
||||
return $res->getReturnResponse();
|
||||
|
||||
return $res->getReturnResponse();
|
||||
}
|
||||
|
||||
public function getStatus()
|
||||
{
|
||||
}
|
||||
|
||||
public function setStatus(Request $req)
|
||||
{
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue