wip: continue implementation

This commit is contained in:
Björn Fromme
2024-11-27 11:31:48 +01:00
parent b5d52b0053
commit 84ab6446ea
81 changed files with 9127 additions and 898 deletions
+16 -6
View File
@@ -7,13 +7,14 @@ use App\BusProNet\DataLoader\PickupDataLoader;
use App\BusProNet\DataLoader\TravelDataLoader;
use App\Form\BookingType;
use App\Form\Model\BookingData;
use App\Htmx\HxRedirectResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
class EditController extends AbstractController
{
@@ -21,6 +22,7 @@ class EditController extends AbstractController
private readonly ApiClient $apiClient,
private readonly TravelDataLoader $travelDataLoader,
private readonly PickupDataLoader $pickupDataLoader,
private readonly CacheInterface $cache,
private readonly Security $security,
) {
}
@@ -35,8 +37,14 @@ class EditController extends AbstractController
return $this->security->logout();
}
// Fetch original bookingData data via API
$bookingData = $this->apiClient->getBooking($bpnUser->getEmail(), $bpnUser->getPassword(), $id);
// Fetch original bookingData data via API and cache result for a short ttl
$cacheKeyList = sprintf('bpn_bookings_%s', sha1($bpnUser->getUserIdentifier()));
$cacheKeySingle = sprintf('bpn_booking_%d', $id);
$bookingData = $this->cache->get($cacheKeySingle, function (ItemInterface $item) use ($bpnUser, $id) {
$item->expiresAfter(300);
return $this->apiClient->getBooking($bpnUser->getEmail(), $bpnUser->getPassword(), $id);
});
if (null === $bookingData) {
$this->addFlash('error', 'Buchungsdaten nicht (mehr) verfügbar');
@@ -68,10 +76,9 @@ class EditController extends AbstractController
$formData = BookingData::fromBooking($bookingData, $travelData);
$form = $this->createForm(BookingType::class, $formData, [
'attr' => ['novalidate' => 'novalidate'],
'hx_post' => $this->generateUrl('app_booking_edit', ['id' => $id]),
'hx_target' => '#app',
'hx_swap' => 'innerHTML show:top',
'attr' => ['novalidate' => 'novalidate'],
]);
$form->handleRequest($request);
@@ -79,9 +86,12 @@ class EditController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) {
$this->apiClient->updateBooking($formData, false);
$this->cache->delete($cacheKeySingle);
$this->cache->delete($cacheKeyList);
$this->addFlash('success', 'Buchung erfolgreich aktualisiert');
return new HxRedirectResponse($this->generateUrl('app_booking_edit', ['id' => $id]));
return $this->redirectToRoute('app_booking_edit', ['id' => $id]);
}
return $this->render('booking/edit.html.twig', [
+15 -1
View File
@@ -3,17 +3,22 @@
namespace App\Controller\Booking;
use App\BusProNet\ApiClient;
use App\BusProNet\DataLoader\TravelDataLoader;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
class IndexController extends AbstractController
{
public function __construct(
private readonly ApiClient $apiClient,
private readonly TravelDataLoader $travelDataLoader,
private readonly CacheInterface $cache,
private readonly Security $security,
) {
}
@@ -28,7 +33,16 @@ class IndexController extends AbstractController
return $this->security->logout();
}
$bookings = $this->apiClient->getBookings($bpnUser->getEmail(), $bpnUser->getPassword());
// Cache bookings for a short ttl
$cacheKey = sprintf('bpn_bookings_%s', sha1($bpnUser->getUserIdentifier()));
$bookings = $this->cache->get($cacheKey, function (ItemInterface $item) use ($bpnUser) {
$item->expiresAfter(300);
$bookings = $this->apiClient->getBookings($bpnUser->getEmail(), $bpnUser->getPassword());
$this->travelDataLoader->patchBookings($bookings);
return $bookings;
});
return $this->render('booking/index.html.twig', [
'bookings' => $bookings->getItems(),
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
class IndexController extends AbstractController
{
#[Route('/', name: 'app_index')]
#[IsGranted('PUBLIC_ACCESS')]
public function index(): Response
{
return $this->render('index/index.html.twig');
}
}
+28 -1
View File
@@ -42,7 +42,7 @@ class PersonalDataController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) {
try {
$this->apiClient->updatePersonalData($bpnUser->getEmail(), $bpnUser->getPassword(), $personalData);
$this->addFlash('success', 'Personal data updated.');
$this->addFlash('success', 'Deine persönlichen Daten wurden aktualisiert');
} catch (ApiClientException $e) {
$this->addFlash('error', $e->getMessage());
}
@@ -55,4 +55,31 @@ class PersonalDataController extends AbstractController
'form' => $form->createView(),
]);
}
#[Route('/personal-data/newsletter', name: 'app_personal_data_newsletter', methods: ['POST'])]
#[IsGranted('ROLE_USER')]
public function newsletter(Request $request): Response
{
$bpnUser = $request->getSession()->get('bpn_user');
if (null === $bpnUser) {
return $this->security->logout();
}
$personalData = $this
->apiClient
->getPersonalData($bpnUser->getEmail(), $bpnUser->getPassword())
;
$personalData->communication->newsletter = !$personalData->communication->newsletter;
try {
$this->apiClient->updateNewsletterRegistration($bpnUser->getEmail(), $bpnUser->getPassword(), $personalData);
$this->addFlash('success', 'Deine Anmeldung zum Newsletter wurde aktualisiert');
} catch (ApiClientException $e) {
$this->addFlash('error', $e->getMessage());
}
return $this->redirectToRoute('app_personal_data');
}
}
+1 -1
View File
@@ -10,7 +10,7 @@ use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
#[Route('/', name: 'app_login')]
#[Route('/login', name: 'app_login')]
#[IsGranted('PUBLIC_ACCESS')]
public function login(AuthenticationUtils $authenticationUtils): Response
{