From 8e7da7511ac51b6ff2a2493ad87a6d7942159f6a Mon Sep 17 00:00:00 2001 From: thisfro Date: Mon, 17 Jan 2022 17:11:38 +0100 Subject: [PATCH] Handle nonexistent PLZs --- src/Controller/OfferController.php | 28 ++++++++++++++++------------ templates/offer/index.html.twig | 7 ++++++- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/Controller/OfferController.php b/src/Controller/OfferController.php index b66f0d4..9b2572e 100644 --- a/src/Controller/OfferController.php +++ b/src/Controller/OfferController.php @@ -6,7 +6,6 @@ use App\Entity\Offering; use App\Form\OfferingFormType; use App\Form\OfferFilterFormType; - use App\Repository\OfferingRepository; use App\Repository\WishRepository; @@ -34,7 +33,7 @@ class OfferController extends AbstractController } #[Route('/offers', name: 'offers')] - public function distanceExample(Environment $twig, Request $request, OfferingRepository $offerRepository, PlzToCoordinate $plzconverter, DistanceCalculator $distanceCalculator): Response + public function showAll(Environment $twig, Request $request, OfferingRepository $offerRepository, PlzToCoordinate $plzconverter, DistanceCalculator $distanceCalculator): Response { $form = $this->createForm(OfferFilterFormType::class); $form->handleRequest($request); @@ -46,25 +45,30 @@ class OfferController extends AbstractController $filterDistance = $form->get('distance')->getData(); $filterPlz = $form->get('zipCode')->getData(); $filterCoordinate = $plzconverter->convertPlzToCoordinate($filterPlz); - - foreach ($allOffers as $offer) { - $offerCoordinate = $offer->getCoordinate(); - - $distance = $distanceCalculator->calculateDistance($offerCoordinate, $filterCoordinate); - - if ($distance < $filterDistance) { - array_push($filteredOffers, $offer); + + if ($filterCoordinate != null) { + foreach ($allOffers as $offer) { + $offerCoordinate = $offer->getCoordinate(); + + $distance = $distanceCalculator->calculateDistance($offerCoordinate, $filterCoordinate); + + if ($distance < $filterDistance) { + array_push($filteredOffers, $offer); + } } } + else { + $this->addFlash("error", "The PLZ was not found!"); + } } else { $filteredOffers = $allOffers; } - return new Response($twig->render('offer/index.html.twig', [ + return $this->render('offer/index.html.twig', [ 'offers' => $filteredOffers, 'filter_form' => $form->createView() - ])); + ]); } #[Route('/new', name: 'new_offer')] diff --git a/templates/offer/index.html.twig b/templates/offer/index.html.twig index ff31135..ab99fa2 100644 --- a/templates/offer/index.html.twig +++ b/templates/offer/index.html.twig @@ -9,6 +9,11 @@ {{ message }} {% endfor %} + {% for message in app.flashes('error') %} + + {% endfor %}

Filter

{{ form_start(filter_form) }} @@ -53,6 +58,6 @@ {% endfor %} {% else %} - + {% endif %} {% endblock %} \ No newline at end of file