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; namespace App\Controller;
use App\Ramcar\CustomerClassification;
use App\Ramcar\FuelType;
use App\Ramcar\VehicleStatusCondition;
use App\Ramcar\CrudException; use App\Ramcar\CrudException;
use App\Service\CustomerHandlerInterface; use App\Service\CustomerHandlerInterface;
@ -20,7 +17,6 @@ use App\Entity\BatteryManufacturer;
use Doctrine\ORM\Query; use Doctrine\ORM\Query;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Catalyst\MenuBundle\Annotation\Menu; use Catalyst\MenuBundle\Annotation\Menu;
@ -138,7 +134,14 @@ class CustomerController extends Controller
{ {
$this->denyAccessUnlessGranted('customer.update', null, 'No access.'); $this->denyAccessUnlessGranted('customer.update', null, 'No access.');
try
{
$result = $cust_handler->updateCustomer($req, $id); $result = $cust_handler->updateCustomer($req, $id);
}
catch (CrudException $e)
{
throw new CrudException($e->getMessage());
}
if (isset($result['id'])) 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.'); $this->denyAccessUnlessGranted('customer.delete', null, 'No access.');
// get row data try
$em = $this->getDoctrine()->getManager(); {
$row = $em->getRepository(Customer::class)->find($id); $cust_handler->deleteCustomer($id);
}
if (empty($row)) catch (NotFoundHttpException $e)
throw $this->createNotFoundException('The item does not exist'); {
throw $this->createNotFoundException($e->getMessage());
// delete this row }
$em->remove($row);
$em->flush();
// response // response
$response = new Response(); $response = new Response();

View file

@ -316,7 +316,14 @@ class CMBCustomerHandler implements CustomerHandlerInterface
// custom validation for vehicles // custom validation for vehicles
$vehicles = json_decode($req->request->get('vehicles')); $vehicles = json_decode($req->request->get('vehicles'));
try
{
$this->updateVehicles($em, $cust, $vehicles); $this->updateVehicles($em, $cust, $vehicles);
}
catch (CrudException $e)
{
throw new CrudException($e->getMessage());
}
// validate // validate
$errors = $this->validator->validate($cust); $errors = $this->validator->validate($cust);
@ -352,6 +359,21 @@ class CMBCustomerHandler implements CustomerHandlerInterface
return $result; 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) public function getTwigTemplate($id)
{ {
if (isset($this->template_hash[$id])) if (isset($this->template_hash[$id]))

View file

@ -356,6 +356,21 @@ class ResqCustomerHandler implements CustomerHandlerInterface
return $result; 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) public function getTwigTemplate($id)
{ {
if (isset($this->template_hash[$id])) if (isset($this->template_hash[$id]))

View file

@ -24,6 +24,9 @@ interface CustomerHandlerInterface
// update customer and customer vehicle // update customer and customer vehicle
public function updateCustomer(Request $req, int $id); public function updateCustomer(Request $req, int $id);
// delete customer
public function deleteCustomer(int $id);
// get template to display // get template to display
public function getTwigTemplate(string $id); public function getTwigTemplate(string $id);
} }