Add validation and deletion for price tier. #780

This commit is contained in:
Korina Cordero 2023-12-21 16:24:37 +08:00
parent fa3cf12be1
commit 8c810bf27a
3 changed files with 134 additions and 17 deletions

View file

@ -139,17 +139,18 @@ class PriceTierController extends Controller
*/ */
public function addSubmit(Request $req, EntityManagerInterface $em, ValidatorInterface $validator) public function addSubmit(Request $req, EntityManagerInterface $em, ValidatorInterface $validator)
{ {
// initialize error list
$error_array = [];
$pt = new PriceTier(); $pt = new PriceTier();
// TODO: add validation for supported area $error_array = $this->validateRequest($em, $req);
$this->setObject($pt, $req); $this->setObject($pt, $req);
// validate // validate
$errors = $validator->validate($pt); $errors = $validator->validate($pt);
// initialize error list
$error_array = [];
// add errors to list // add errors to list
foreach ($errors as $error) { foreach ($errors as $error) {
$error_array[$error->getPropertyPath()] = $error->getMessage(); $error_array[$error->getPropertyPath()] = $error->getMessage();
@ -180,13 +181,13 @@ class PriceTierController extends Controller
/** /**
* @Menu(selected="price_tier_list") * @Menu(selected="price_tier_list")
* @ParamConverter("price_tier", class="App\Entity\PriceTier") * @ParamConverter("pt", class="App\Entity\PriceTier")
* @IsGranted("price_tier.update") * @IsGranted("price_tier.update")
*/ */
public function updateForm($id, EntityManagerInterface $em, PriceTier $pt) public function updateForm($id, EntityManagerInterface $em, PriceTier $pt)
{ {
// get the supported areas // get the supported areas
$sets = $this->generateFormSets($em); $sets = $this->generateFormSets($em, $pt);
$params = [ $params = [
'obj' => $pt, 'obj' => $pt,
@ -198,31 +199,141 @@ class PriceTierController extends Controller
return $this->render('price-tier/form.html.twig', $params); return $this->render('price-tier/form.html.twig', $params);
} }
/**
* @ParamConverter("pt", class="App\Entity\PriceTier")
* @IsGranted("price_tier.update")
*/
public function updateSubmit(Request $req, EntityManagerInterface $em, ValidatorInterface $validator, PriceTier $pt)
{
// initialize error list
$error_array = [];
// clear supported areas of price tier
$this->clearPriceTierSupportedAreas($em, $pt);
$error_array = $this->validateRequest($em, $req);
$this->setObject($pt, $req);
// validate
$errors = $validator->validate($pt);
// add errors to list
foreach ($errors as $error) {
$error_array[$error->getPropertyPath()] = $error->getMessage();
}
// check if any errors were found
if (!empty($error_array)) {
// return validation failure response
return $this->json([
'success' => false,
'errors' => $error_array
], 422);
}
// set the price tier id for the selected supported areas
$this->updateSupportedAreas($em, $pt, $req);
// validated! save the entity
$em->flush();
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
/**
* @ParamConverter("pt", class="App\Entity\PriceTier")
* @IsGranted("price_tier.delete")
*/
public function deleteSubmit(EntityManagerInterface $em, PriceTier $pt)
{
// clear supported areas of price tier
$this->clearPriceTierSupportedAreas($em, $pt);
// delete this object
$em->remove($pt);
$em->flush();
// response
$response = new Response();
$response->setStatusCode(Response::HTTP_OK);
$response->send();
}
protected function validateRequest(EntityManagerInterface $em, Request $req)
{
// get areas
$areas = $req->request->get('areas');
// check if no areas selected aka empty
if (!empty($areas))
{
foreach ($areas as $area_id)
{
$supported_area = $em->getRepository(SupportedArea::class)->find($area_id);
if ($supported_area == null)
return ['areas' => 'Invalid area'];
// check if supported area already belongs to a price tier
if ($supported_area->getPriceTier() != null)
return ['areas' => 'Area already belongs to a price tier.'];
}
}
return null;
}
protected function setObject(PriceTier $obj, Request $req) protected function setObject(PriceTier $obj, Request $req)
{ {
// clear supported areas first
$obj->clearSupportedAreas();
$obj->setName($req->request->get('name')); $obj->setName($req->request->get('name'));
} }
public function updateSupportedAreas(EntityManagerInterface $em, PriceTier $obj, Request $req) protected function clearPriceTierSupportedAreas(EntityManagerInterface $em, PriceTier $obj)
{
// find the supported areas set with the price tier
$areas = $em->getRepository(SupportedArea::class)->findBy(['price_tier' => $obj]);
if (!empty($areas))
{
// set the price tier id for the supported areas to null
foreach ($areas as $area)
{
$area->setPriceTier(null);
}
$em->flush();
}
}
protected function updateSupportedAreas(EntityManagerInterface $em, PriceTier $obj, Request $req)
{ {
// get the selected areas // get the selected areas
$areas = $req->request->get('areas'); $areas = $req->request->get('areas');
foreach ($areas as $area_id) // check if no areas selected aka empty
if (!empty($areas))
{ {
// get supported area foreach ($areas as $area_id)
$supported_area = $em->getRepository(SupportedArea::class)->find($area_id); {
// get supported area
$supported_area = $em->getRepository(SupportedArea::class)->find($area_id);
if ($supported_area != null) if ($supported_area != null)
$supported_area->setPriceTier($obj); $supported_area->setPriceTier($obj);
}
} }
} }
protected function generateFormSets(EntityManagerInterface $em) protected function generateFormSets(EntityManagerInterface $em, PriceTier $pt = null)
{ {
// get the supported areas // get the supported areas with no price tier id or price tier id is set to the one that is being updated
// TODO: filter out the supported areas that already have a price tier id? but if we're editing, we need those price tiers $areas = $em->getRepository(SupportedArea::class)->findBy(['price_tier' => array(null, $pt)]);
$areas = $em->getRepository(SupportedArea::class)->findAll();
$areas_set = []; $areas_set = [];
foreach ($areas as $area) foreach ($areas as $area)
{ {

View file

@ -74,6 +74,12 @@ class PriceTier
return $str_supported_areas; return $str_supported_areas;
} }
public function clearSupportedAreas()
{
$this->supported_areas->clear();
return $this;
}
public function getItemPrices() public function getItemPrices()
{ {
return $this->item_prices; return $this->item_prices;

View file

@ -91,7 +91,7 @@ class SupportedArea
return $this->coverage_area; return $this->coverage_area;
} }
public function setPriceTier(PriceTier $price_tier) public function setPriceTier(PriceTier $price_tier = null)
{ {
$this->price_tier = $price_tier; $this->price_tier = $price_tier;
return $this; return $this;