From 4782bd7fe849e4d134e0d7150f2ad790d1fbde0d Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Sun, 7 Jul 2019 08:29:22 +0000 Subject: [PATCH] Add API call to list other services. #228 --- config/routes/api.yaml | 5 ++++ src/Controller/APIController.php | 51 ++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/config/routes/api.yaml b/config/routes/api.yaml index 50e50957..50d6f3a5 100644 --- a/config/routes/api.yaml +++ b/config/routes/api.yaml @@ -124,3 +124,8 @@ api_location_support: path: /api/location_support controller: App\Controller\APIController:locationSupport methods: [GET] + +api_service_list: + path: /api/services + controller: App\Controller\APIController:listServices + methods: [GET] diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 7a39d50b..ace7e540 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -38,6 +38,7 @@ use App\Entity\Promo; use App\Entity\Battery; use App\Entity\RiderRating; use App\Entity\JOEvent; +use App\Entity\Service; use DateTime; use Exception; @@ -1802,4 +1803,54 @@ class APIController extends Controller return $res->getReturnResponse(); } + + public function listServices(Request $req) + { + $required_params = []; + $em = $this->getDoctrine()->getManager(); + $res = $this->checkParamsAndKey($req, $em, $required_params); + if ($res->isError()) + return $res->getReturnResponse(); + + // services + $results = $em->getRepository(Service::class)->findAll(); + if (empty($results)) + { + $res->setError(true) + ->setErrorMessage('No services available.'); + return $res->getReturnResponse(); + } + + $services = []; + foreach ($results as $result) + { + // get partners + $partners = []; + $service_partners = $result->getPartners(); + foreach($service_partners as $sp) + { + $partners[] = [ + 'id' => $sp->getID(), + 'name' => $sp->getName(), + 'branch' => $sp->getBranch(), + 'address' => $sp->getAddress(), + 'contact_nums' => $sp->getContactNumbers(), + 'time_open' => $sp->getTimeOpen()->format("d M Y g:i A"), + 'time_close' => $sp->getTimeClose()->format("d M Y g:i A"), + ]; + } + + $services[] = [ + 'id' => $result->getID(), + 'name' => $result->getName(), + 'partners' => $partners, + ]; + } + + $data['services'] = $services; + + $res->setData($data); + + return $res->getReturnResponse(); + } }