Move the delete customer to the service. #270

This commit is contained in:
Korina Cordero 2019-11-12 07:48:35 +00:00
parent fb6823d25c
commit cc3db5b58c
4 changed files with 58 additions and 17 deletions

View file

@ -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();

View file

@ -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]))

View file

@ -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]))

View file

@ -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);
}