diff --git a/src/Controller/CustomerController.php b/src/Controller/CustomerController.php index 3d32002a..d485a611 100644 --- a/src/Controller/CustomerController.php +++ b/src/Controller/CustomerController.php @@ -320,44 +320,28 @@ class CustomerController extends Controller } } - public function updateSubmit(Request $req, ValidatorInterface $validator, $id) + public function updateSubmit(Request $req, CustomerHandlerInterface $cust_handler, $id) { $this->denyAccessUnlessGranted('customer.update', null, 'No access.'); - // get row data - $em = $this->getDoctrine()->getManager(); - $cust = $em->getRepository(Customer::class)->find($id); + $result = $cust_handler->updateCustomer($req, $id); - // make sure this row exists - if (empty($cust)) - throw $this->createNotFoundException('The item does not exist'); - - $this->setObject($cust, $req); - - // initialize error lists - $error_array = []; - $nerror_array = []; - $verror_array = []; - - // TODO: validate mobile numbers - // TODO: validate vehicles - - // custom validation for vehicles - $vehicles = json_decode($req->request->get('vehicles')); - $this->updateVehicles($em, $cust, $vehicles); - - // validate - $errors = $validator->validate($cust); - - // add errors to list - foreach ($errors as $error) + if (isset($result['id'])) { - $error_array[$error->getPropertyPath()] = $error->getMessage(); + $id = $result['id']; + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!', + 'id' => $id + ]); } - - // check if any errors were found - if (!empty($error_array) || !empty($nerror_array) || !empty($verror_array)) + else { + $error_array = $result['error_array']; + $nerror_array = $result['nerror_array']; + $verror_array = $result['verror_array']; + // return validation failure response return $this->json([ 'success' => false, @@ -366,18 +350,6 @@ class CustomerController extends Controller 'verrors' => $verror_array ], 422); } - else - { - // validated! save the entity. do a persist anyway to save child entities - $em->persist($cust); - $em->flush(); - - // return successful response - return $this->json([ - 'success' => 'Changes have been saved!', - 'id' => $cust->getID() - ]); - } } public function destroy($id)