diff --git a/src/Controller/CustomerController.php b/src/Controller/CustomerController.php index 37a2b391..e12d8b19 100644 --- a/src/Controller/CustomerController.php +++ b/src/Controller/CustomerController.php @@ -2,9 +2,6 @@ namespace App\Controller; -use App\Ramcar\CustomerClassification; -use App\Ramcar\FuelType; -use App\Ramcar\VehicleStatusCondition; use App\Ramcar\CrudException; use App\Service\CustomerHandlerInterface; @@ -20,7 +17,6 @@ use App\Entity\BatteryManufacturer; use Doctrine\ORM\Query; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Catalyst\MenuBundle\Annotation\Menu; @@ -138,7 +134,14 @@ class CustomerController extends Controller { $this->denyAccessUnlessGranted('customer.update', null, 'No access.'); - $result = $cust_handler->updateCustomer($req, $id); + try + { + $result = $cust_handler->updateCustomer($req, $id); + } + catch (CrudException $e) + { + throw new CrudException($e->getMessage()); + } if (isset($result['id'])) { @@ -166,20 +169,18 @@ class CustomerController extends Controller } } - public function destroy($id) + public function destroy($id, CustomerHandlerInterface $cust_handler) { $this->denyAccessUnlessGranted('customer.delete', null, 'No access.'); - // get row data - $em = $this->getDoctrine()->getManager(); - $row = $em->getRepository(Customer::class)->find($id); - - if (empty($row)) - throw $this->createNotFoundException('The item does not exist'); - - // delete this row - $em->remove($row); - $em->flush(); + try + { + $cust_handler->deleteCustomer($id); + } + catch (NotFoundHttpException $e) + { + throw $this->createNotFoundException($e->getMessage()); + } // response $response = new Response(); diff --git a/src/Service/CustomerHandler/CMBCustomerHandler.php b/src/Service/CustomerHandler/CMBCustomerHandler.php index 0d63cce8..7abf86b2 100644 --- a/src/Service/CustomerHandler/CMBCustomerHandler.php +++ b/src/Service/CustomerHandler/CMBCustomerHandler.php @@ -316,7 +316,14 @@ class CMBCustomerHandler implements CustomerHandlerInterface // custom validation for vehicles $vehicles = json_decode($req->request->get('vehicles')); - $this->updateVehicles($em, $cust, $vehicles); + try + { + $this->updateVehicles($em, $cust, $vehicles); + } + catch (CrudException $e) + { + throw new CrudException($e->getMessage()); + } // validate $errors = $this->validator->validate($cust); @@ -352,6 +359,21 @@ class CMBCustomerHandler implements CustomerHandlerInterface return $result; } + // delete customer + public function deleteCustomer(int $id) + { + // get row data + $em = $this->em; + $row = $em->getRepository(Customer::class)->find($id); + + if (empty($row)) + throw new NotFoundHttpException('The item does not exist'); + + // delete this row + $em->remove($row); + $em->flush(); + } + public function getTwigTemplate($id) { if (isset($this->template_hash[$id])) diff --git a/src/Service/CustomerHandler/ResqCustomerHandler.php b/src/Service/CustomerHandler/ResqCustomerHandler.php index 9529c673..59d4221f 100644 --- a/src/Service/CustomerHandler/ResqCustomerHandler.php +++ b/src/Service/CustomerHandler/ResqCustomerHandler.php @@ -356,6 +356,21 @@ class ResqCustomerHandler implements CustomerHandlerInterface return $result; } + // delete customer + public function deleteCustomer(int $id) + { + // get row data + $em = $this->em; + $row = $em->getRepository(Customer::class)->find($id); + + if (empty($row)) + throw new NotFoundHttpException('The item does not exist'); + + // delete this row + $em->remove($row); + $em->flush(); + } + public function getTwigTemplate($id) { if (isset($this->template_hash[$id])) diff --git a/src/Service/CustomerHandlerInterface.php b/src/Service/CustomerHandlerInterface.php index eac3f2d8..5359d2d6 100644 --- a/src/Service/CustomerHandlerInterface.php +++ b/src/Service/CustomerHandlerInterface.php @@ -24,6 +24,9 @@ interface CustomerHandlerInterface // update customer and customer vehicle public function updateCustomer(Request $req, int $id); + // delete customer + public function deleteCustomer(int $id); + // get template to display public function getTwigTemplate(string $id); }