Handle nonexistent PLZs

This commit is contained in:
Jannis Portmann 2022-01-17 17:11:38 +01:00
parent 7a3749c105
commit 8e7da7511a
2 changed files with 22 additions and 13 deletions

View file

@ -6,7 +6,6 @@ use App\Entity\Offering;
use App\Form\OfferingFormType; use App\Form\OfferingFormType;
use App\Form\OfferFilterFormType; use App\Form\OfferFilterFormType;
use App\Repository\OfferingRepository; use App\Repository\OfferingRepository;
use App\Repository\WishRepository; use App\Repository\WishRepository;
@ -34,7 +33,7 @@ class OfferController extends AbstractController
} }
#[Route('/offers', name: 'offers')] #[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 = $this->createForm(OfferFilterFormType::class);
$form->handleRequest($request); $form->handleRequest($request);
@ -47,24 +46,29 @@ class OfferController extends AbstractController
$filterPlz = $form->get('zipCode')->getData(); $filterPlz = $form->get('zipCode')->getData();
$filterCoordinate = $plzconverter->convertPlzToCoordinate($filterPlz); $filterCoordinate = $plzconverter->convertPlzToCoordinate($filterPlz);
foreach ($allOffers as $offer) { if ($filterCoordinate != null) {
$offerCoordinate = $offer->getCoordinate(); foreach ($allOffers as $offer) {
$offerCoordinate = $offer->getCoordinate();
$distance = $distanceCalculator->calculateDistance($offerCoordinate, $filterCoordinate); $distance = $distanceCalculator->calculateDistance($offerCoordinate, $filterCoordinate);
if ($distance < $filterDistance) { if ($distance < $filterDistance) {
array_push($filteredOffers, $offer); array_push($filteredOffers, $offer);
}
} }
} }
else {
$this->addFlash("error", "The PLZ was not found!");
}
} }
else { else {
$filteredOffers = $allOffers; $filteredOffers = $allOffers;
} }
return new Response($twig->render('offer/index.html.twig', [ return $this->render('offer/index.html.twig', [
'offers' => $filteredOffers, 'offers' => $filteredOffers,
'filter_form' => $form->createView() 'filter_form' => $form->createView()
])); ]);
} }
#[Route('/new', name: 'new_offer')] #[Route('/new', name: 'new_offer')]

View file

@ -9,6 +9,11 @@
{{ message }} {{ message }}
</div> </div>
{% endfor %} {% endfor %}
{% for message in app.flashes('error') %}
<div class="alert alert-danger" role="alert">
{{ message }}
</div>
{% endfor %}
<h2><i class="fas fa-filter"></i>Filter</h2> <h2><i class="fas fa-filter"></i>Filter</h2>
{{ form_start(filter_form) }} {{ form_start(filter_form) }}
@ -53,6 +58,6 @@
{% endfor %} {% endfor %}
</div> </div>
{% else %} {% else %}
<div class="alert alert-warning" role="alert">There are currently no active offers.</div> <div class="alert alert-warning" role="alert">There are no active offers with the current filter.</div>
{% endif %} {% endif %}
{% endblock %} {% endblock %}