Remove old implementation for update customer. #270

This commit is contained in:
Korina Cordero 2019-11-12 06:06:11 +00:00
parent 50e54f6acc
commit 0a8ba8ca7d

View file

@ -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.'); $this->denyAccessUnlessGranted('customer.update', null, 'No access.');
// get row data $result = $cust_handler->updateCustomer($req, $id);
$em = $this->getDoctrine()->getManager();
$cust = $em->getRepository(Customer::class)->find($id);
// make sure this row exists if (isset($result['id']))
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)
{ {
$error_array[$error->getPropertyPath()] = $error->getMessage(); $id = $result['id'];
// return successful response
return $this->json([
'success' => 'Changes have been saved!',
'id' => $id
]);
} }
else
// check if any errors were found
if (!empty($error_array) || !empty($nerror_array) || !empty($verror_array))
{ {
$error_array = $result['error_array'];
$nerror_array = $result['nerror_array'];
$verror_array = $result['verror_array'];
// return validation failure response // return validation failure response
return $this->json([ return $this->json([
'success' => false, 'success' => false,
@ -366,18 +350,6 @@ class CustomerController extends Controller
'verrors' => $verror_array 'verrors' => $verror_array
], 422); ], 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) public function destroy($id)