From 7beec510a1e6bcf3b480502ecd90d6e2d61737c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Mon, 24 Jul 2023 02:00:59 +0200 Subject: [PATCH] create endpoints to get current event and current event new transaction --- src/Controller/EventRedirectController.php | 70 ++++++++++++++++++++++ src/Repository/EventRepository.php | 10 ++++ views/event_redirect/no_event.php | 8 +++ web.php | 5 ++ 4 files changed, 93 insertions(+) create mode 100644 src/Controller/EventRedirectController.php create mode 100644 views/event_redirect/no_event.php diff --git a/src/Controller/EventRedirectController.php b/src/Controller/EventRedirectController.php new file mode 100644 index 0000000..32a84a9 --- /dev/null +++ b/src/Controller/EventRedirectController.php @@ -0,0 +1,70 @@ +eventRepository = new EventRepository(); + } + + public function isAuthenticationRequired(): bool + { + return true; + } + + public function getEvent() + { + $currentEvent = $this->getCurrentEvent(); + if ($currentEvent === null) { + return new HtmlContent('event_redirect/no_event'); + } + + return new Redirect( + \Container::$routeCollection->getRoute('community.event') + ->generateLink([ + 'communitySlug' => $currentEvent->getCommunity()->getSlug(), + 'eventSlug' => $currentEvent->getSlug() + ]), + IRedirect::TEMPORARY + ); + } + + public function getEventNewTransaction() + { + $currentEvent = $this->getCurrentEvent(); + if ($currentEvent === null) { + return new HtmlContent('event_redirect/no_event'); + } + + return new Redirect( + \Container::$routeCollection->getRoute('community.transactions.new') + ->generateLink([ + 'communitySlug' => $currentEvent->getCommunity()->getSlug(), + 'event' => $currentEvent->getSlug() + ]), + IRedirect::TEMPORARY + ); + } + + private function getCurrentEvent(): ?Event + { + /** + * @var User $user + */ + $user = Container::$request->user(); + + return $this->eventRepository->getCurrentByUser($user, new DateTime(), 30, true, ['community']); + } +} diff --git a/src/Repository/EventRepository.php b/src/Repository/EventRepository.php index e40e083..e1aa287 100644 --- a/src/Repository/EventRepository.php +++ b/src/Repository/EventRepository.php @@ -53,6 +53,16 @@ class EventRepository yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class, $useRelations, $withRelations); } + public function getCurrentByUser(User $user, DateTime $from, int $days, bool $useRelations = false, array $withRelations = []): ?Event + { + $select = $this->selectAllByUser($user); + $this->selectUpcomingAndRecent($select, $from, $days); + $select->orderBy('start', 'DESC'); + $select->limit(1, 0); + + return Container::$persistentDataManager->selectFromDb($select, Event::class, $useRelations, $withRelations); + } + public function getPagedByCommunity(Community $community, int $page, int $itemsPerPage, bool $useRelations = false, array $withRelations = []): Generator { $select = $this->selectAllByCommunity($community); diff --git a/views/event_redirect/no_event.php b/views/event_redirect/no_event.php new file mode 100644 index 0000000..9c71404 --- /dev/null +++ b/views/event_redirect/no_event.php @@ -0,0 +1,8 @@ +@extends(templates/layout_normal) + +@section(main) +

No event found

+
+

No upcoming or recent event was found. Back to start.

+
+@endsection diff --git a/web.php b/web.php index 5b77f8c..0fdce80 100644 --- a/web.php +++ b/web.php @@ -13,6 +13,7 @@ use RVR\Controller\UserController; use RVR\Controller\UserSearchController; use RVR\Controller\CommunityController; use RVR\Controller\TransactionController; +use RVR\Controller\EventRedirectController; use RVR\Controller\EventController; use RVR\Repository\UserRepository; @@ -62,6 +63,10 @@ Container::$routeCollection->group('account', function (RouteCollection $routeCo $routeCollection->get('account.googleAuthenticate-action', 'googleAuthenticate/code', [UserController::class, 'authenticateWithGoogle']); }); Container::$routeCollection->get('searchUser', 'searchUser', [UserSearchController::class, 'searchUser']); +Container::$routeCollection->group('now', function (RouteCollection $routeCollection) { + $routeCollection->get('now.event', '', [EventRedirectController::class, 'getEvent']); + $routeCollection->get('now.transactions.new', 'transactions/new', [EventRedirectController::class, 'getEventNewTransaction']); +}); Container::$routeCollection->group('communities', function (RouteCollection $routeCollection) { $routeCollection->get('community.new', 'new', [CommunityController::class, 'getCommunityNew']); $routeCollection->post('community.new-action', 'new', [CommunityController::class, 'saveCommunity']);