From 49fe85dcde6606b67577473d2a9e7a05281556c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Wed, 26 Apr 2023 09:43:57 +0200 Subject: [PATCH] merge new and edit methods in CommunityController --- src/Controller/CommunityController.php | 119 +++++++++---------------- web.php | 12 +-- 2 files changed, 49 insertions(+), 82 deletions(-) diff --git a/src/Controller/CommunityController.php b/src/Controller/CommunityController.php index ef2f274..a936674 100644 --- a/src/Controller/CommunityController.php +++ b/src/Controller/CommunityController.php @@ -85,34 +85,25 @@ class CommunityController implements IAuthenticationRequired ]); } - public function newMember(): ?IContent + public function saveMember(): ?IContent { if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { return null; } - $user = $this->userRepository->getById(\Container::$request->post('user_id')); - - $communityMember = new CommunityMember(); - $communityMember->setCommunity($community); - $communityMember->setUser($user); - $communityMember->setOwner((bool)\Container::$request->post('owner')); - \Container::$persistentDataManager->saveToDb($communityMember); - - return new JsonContent(['success' => true]); - } - - public function editMember(): ?IContent - { - if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { - return null; - } - - $communityMember = $this->communityMemberRepository->getById(\Container::$request->query('community_member_id')); - if ($communityMember->getUserId() === \Container::$request->user()->getUniqueId()) { - return new JsonContent([ - 'error' => ['errorText' => 'Own user cannot be edited.'] - ]); + $communityMemberId = \Container::$request->query('community_member_id'); + if ($communityMemberId) { + $communityMember = $this->communityMemberRepository->getById($communityMemberId); + if ($communityMember->getUserId() === $ownCommunityMember->getUserId()) { + return new JsonContent([ + 'error' => ['errorText' => 'Own user cannot be edited.'] + ]); + } + } else { + $user = $this->userRepository->getById(\Container::$request->post('user_id')); + $communityMember = new CommunityMember(); + $communityMember->setCommunity($community); + $communityMember->setUser($user); } $communityMember->setOwner((bool)\Container::$request->post('owner')); @@ -151,7 +142,7 @@ class CommunityController implements IAuthenticationRequired ]); } - public function newCurrency(): ?IContent + public function saveCurrency(): ?IContent { if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { return null; @@ -165,15 +156,21 @@ class CommunityController implements IAuthenticationRequired ]); } + $currencyId = \Container::$request->query('currency_id'); + if ($currencyId){ + $currency = $this->currencyRepository->getById($currencyId); + } else { + $currency = new Currency(); + $currency->setCommunity($community); + } + $existingCurrency = $this->currencyRepository->getByCommunityAndCurrencyCode($community, $code); - if ($existingCurrency !== null) { + if ($existingCurrency !== null && $currency->getId() !== $existingCurrency->getId()) { return new JsonContent([ 'error' => ['errorText' => 'A currency with the same code exists for this community.'] ]); } - $currency = new Currency(); - $currency->setCommunity($community); $currency->setCode($code); $currency->setRoundDigits($roundDigits); \Container::$persistentDataManager->saveToDb($currency); @@ -181,20 +178,6 @@ class CommunityController implements IAuthenticationRequired return new JsonContent(['success' => true]); } - public function editCurrency(): ?IContent - { - if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { - return null; - } - - $currency = $this->currencyRepository->getById(\Container::$request->query('currency_id')); - $currency->setCode(\Container::$request->post('code')); - $currency->setRoundDigits((int)\Container::$request->post('round_digits')); - \Container::$persistentDataManager->saveToDb($currency); - - return new JsonContent(['success' => true]); - } - public function deleteCurrency(): ?IContent { if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { @@ -228,7 +211,7 @@ class CommunityController implements IAuthenticationRequired ]); } - public function newCurrencyExchangeRate(): ?IContent + public function saveCurrencyExchangeRate(): ?IContent { if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { return null; @@ -246,8 +229,14 @@ class CommunityController implements IAuthenticationRequired ]); } - $currencyExchangeRate = new CurrencyExchangeRate(); - $currencyExchangeRate->setCurrency($currency); + $currencyExchangeRateId = \Container::$request->query('currency_exchange_rate_id'); + if ($currencyExchangeRateId){ + $currencyExchangeRate = $this->currencyExchangeRatesRepository->getById($currencyExchangeRateId); + } else { + $currencyExchangeRate = new CurrencyExchangeRate(); + $currencyExchangeRate->setCurrency($currency); + } + $currencyExchangeRate->setExchangeRate($exchangeRate); $currencyExchangeRate->setValidFromDate(new DateTime(\Container::$request->post('valid_from'))); \Container::$persistentDataManager->saveToDb($currencyExchangeRate); @@ -255,26 +244,6 @@ class CommunityController implements IAuthenticationRequired return new JsonContent(['success' => true]); } - public function editCurrencyExchangeRate(): ?IContent - { - if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { - return null; - } - - $currency = $this->currencyRepository->getByCommunityAndCurrencyCode($community, \Container::$request->query('code')); - if ($currency === null) { - return null; - } - - $currencyExchangeRate = $this->currencyExchangeRatesRepository->getById(\Container::$request->query('currency_exchange_rate_id')); - $currencyExchangeRate->setCurrency($currency); - $currencyExchangeRate->setExchangeRate((float)\Container::$request->post('exchange_rate')); - $currencyExchangeRate->setValidFromDate(new DateTime(\Container::$request->post('valid_from'))); - \Container::$persistentDataManager->saveToDb($currencyExchangeRate); - - return new JsonContent(['success' => true]); - } - public function deleteCurrencyExchangeRate(): ?IContent { if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { @@ -294,15 +263,6 @@ class CommunityController implements IAuthenticationRequired public function saveCommunity(): ?IContent { - $communityId = \Container::$request->query('communityId'); - if ($communityId){ - if (!$this->checkPermission($communityId, true, $community, $ownCommunityMember)) { - return null; - } - } else { - $community = new Community(); - } - $name = \Container::$request->post('name'); $currency = \Container::$request->post('currency'); if (strlen($name) === 0 || strlen($currency) === 0 || strlen($currency) > 3) { @@ -311,11 +271,18 @@ class CommunityController implements IAuthenticationRequired ]); } - $community->setName($name); - $community->setCurrency($currency); - if (!$communityId) { + $communityId = \Container::$request->query('communityId'); + if ($communityId){ + if (!$this->checkPermission($communityId, true, $community, $ownCommunityMember)) { + return null; + } + } else { + $community = new Community(); $community->setCreatedDate(new DateTime()); } + + $community->setName($name); + $community->setCurrency($currency); \Container::$persistentDataManager->saveToDb($community); if (!$communityId) { diff --git a/web.php b/web.php index 4b7f3a8..29af58d 100644 --- a/web.php +++ b/web.php @@ -63,17 +63,17 @@ Container::$routeCollection->group('communities', function (RouteCollection $rou $routeCollection->get('community-edit', 'edit', [CommunityController::class, 'getCommunityEdit']); $routeCollection->post('community-edit-action', 'edit', [CommunityController::class, 'saveCommunity']); $routeCollection->get('community-members', 'members', [CommunityController::class, 'getMembersEdit']); - $routeCollection->post('community-members-new', 'newMember', [CommunityController::class, 'newMember']); - $routeCollection->post('community-members-edit', 'editMember', [CommunityController::class, 'editMember']); + $routeCollection->post('community-members-new', 'newMember', [CommunityController::class, 'saveMember']); + $routeCollection->post('community-members-edit', 'editMember', [CommunityController::class, 'saveMember']); $routeCollection->post('community-members-delete', 'deleteMember', [CommunityController::class, 'deleteMember']); $routeCollection->get('community-currencies', 'currencies', [CommunityController::class, 'getCurrenciesEdit']); - $routeCollection->post('community-currencies-new', 'newCurrency', [CommunityController::class, 'newCurrency']); - $routeCollection->post('community-currencies-edit', 'editCurrency', [CommunityController::class, 'editCurrency']); + $routeCollection->post('community-currencies-new', 'newCurrency', [CommunityController::class, 'saveCurrency']); + $routeCollection->post('community-currencies-edit', 'editCurrency', [CommunityController::class, 'saveCurrency']); $routeCollection->post('community-currencies-delete', 'deleteCurrency', [CommunityController::class, 'deleteCurrency']); $routeCollection->group('currencyExchangeRates', function (RouteCollection $routeCollection) { $routeCollection->get('community-currency-exchange-rates', '{code}', [CommunityController::class, 'getCurrencyExchangeRates']); - $routeCollection->post('community-currency-exchange-rates-new', '{code}/new', [CommunityController::class, 'newCurrencyExchangeRate']); - $routeCollection->post('community-currency-exchange-rates-edit', '{code}/edit', [CommunityController::class, 'editCurrencyExchangeRate']); + $routeCollection->post('community-currency-exchange-rates-new', '{code}/new', [CommunityController::class, 'saveCurrencyExchangeRate']); + $routeCollection->post('community-currency-exchange-rates-edit', '{code}/edit', [CommunityController::class, 'saveCurrencyExchangeRate']); $routeCollection->post('community-currency-exchange-rates-delete', '{code}/delete', [CommunityController::class, 'deleteCurrencyExchangeRate']); }); }); -- 2.45.2