merge new and edit methods in CommunityController #33

Merged
bence merged 1 commits from feature/unify-saves-in-communitycontroller into master 2023-04-26 09:46:09 +02:00
2 changed files with 49 additions and 82 deletions
Showing only changes of commit 49fe85dcde - Show all commits

View File

@ -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)) { if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) {
return null; return null;
} }
$user = $this->userRepository->getById(\Container::$request->post('user_id')); $communityMemberId = \Container::$request->query('community_member_id');
if ($communityMemberId) {
$communityMember = new CommunityMember(); $communityMember = $this->communityMemberRepository->getById($communityMemberId);
$communityMember->setCommunity($community); if ($communityMember->getUserId() === $ownCommunityMember->getUserId()) {
$communityMember->setUser($user); return new JsonContent([
$communityMember->setOwner((bool)\Container::$request->post('owner')); 'error' => ['errorText' => 'Own user cannot be edited.']
\Container::$persistentDataManager->saveToDb($communityMember); ]);
}
return new JsonContent(['success' => true]); } else {
} $user = $this->userRepository->getById(\Container::$request->post('user_id'));
$communityMember = new CommunityMember();
public function editMember(): ?IContent $communityMember->setCommunity($community);
{ $communityMember->setUser($user);
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.']
]);
} }
$communityMember->setOwner((bool)\Container::$request->post('owner')); $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)) { if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) {
return null; 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); $existingCurrency = $this->currencyRepository->getByCommunityAndCurrencyCode($community, $code);
if ($existingCurrency !== null) { if ($existingCurrency !== null && $currency->getId() !== $existingCurrency->getId()) {
return new JsonContent([ return new JsonContent([
'error' => ['errorText' => 'A currency with the same code exists for this community.'] 'error' => ['errorText' => 'A currency with the same code exists for this community.']
]); ]);
} }
$currency = new Currency();
$currency->setCommunity($community);
$currency->setCode($code); $currency->setCode($code);
$currency->setRoundDigits($roundDigits); $currency->setRoundDigits($roundDigits);
\Container::$persistentDataManager->saveToDb($currency); \Container::$persistentDataManager->saveToDb($currency);
@ -181,20 +178,6 @@ class CommunityController implements IAuthenticationRequired
return new JsonContent(['success' => true]); 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 public function deleteCurrency(): ?IContent
{ {
if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { 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)) { if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) {
return null; return null;
@ -246,8 +229,14 @@ class CommunityController implements IAuthenticationRequired
]); ]);
} }
$currencyExchangeRate = new CurrencyExchangeRate(); $currencyExchangeRateId = \Container::$request->query('currency_exchange_rate_id');
$currencyExchangeRate->setCurrency($currency); if ($currencyExchangeRateId){
$currencyExchangeRate = $this->currencyExchangeRatesRepository->getById($currencyExchangeRateId);
} else {
$currencyExchangeRate = new CurrencyExchangeRate();
$currencyExchangeRate->setCurrency($currency);
}
$currencyExchangeRate->setExchangeRate($exchangeRate); $currencyExchangeRate->setExchangeRate($exchangeRate);
$currencyExchangeRate->setValidFromDate(new DateTime(\Container::$request->post('valid_from'))); $currencyExchangeRate->setValidFromDate(new DateTime(\Container::$request->post('valid_from')));
\Container::$persistentDataManager->saveToDb($currencyExchangeRate); \Container::$persistentDataManager->saveToDb($currencyExchangeRate);
@ -255,26 +244,6 @@ class CommunityController implements IAuthenticationRequired
return new JsonContent(['success' => true]); 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 public function deleteCurrencyExchangeRate(): ?IContent
{ {
if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) { if (!$this->checkPermission(\Container::$request->query('communityId'), true, $community, $ownCommunityMember)) {
@ -294,15 +263,6 @@ class CommunityController implements IAuthenticationRequired
public function saveCommunity(): ?IContent 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'); $name = \Container::$request->post('name');
$currency = \Container::$request->post('currency'); $currency = \Container::$request->post('currency');
if (strlen($name) === 0 || strlen($currency) === 0 || strlen($currency) > 3) { if (strlen($name) === 0 || strlen($currency) === 0 || strlen($currency) > 3) {
@ -311,11 +271,18 @@ class CommunityController implements IAuthenticationRequired
]); ]);
} }
$community->setName($name); $communityId = \Container::$request->query('communityId');
$community->setCurrency($currency); if ($communityId){
if (!$communityId) { if (!$this->checkPermission($communityId, true, $community, $ownCommunityMember)) {
return null;
}
} else {
$community = new Community();
$community->setCreatedDate(new DateTime()); $community->setCreatedDate(new DateTime());
} }
$community->setName($name);
$community->setCurrency($currency);
\Container::$persistentDataManager->saveToDb($community); \Container::$persistentDataManager->saveToDb($community);
if (!$communityId) { if (!$communityId) {

12
web.php
View File

@ -63,17 +63,17 @@ Container::$routeCollection->group('communities', function (RouteCollection $rou
$routeCollection->get('community-edit', 'edit', [CommunityController::class, 'getCommunityEdit']); $routeCollection->get('community-edit', 'edit', [CommunityController::class, 'getCommunityEdit']);
$routeCollection->post('community-edit-action', 'edit', [CommunityController::class, 'saveCommunity']); $routeCollection->post('community-edit-action', 'edit', [CommunityController::class, 'saveCommunity']);
$routeCollection->get('community-members', 'members', [CommunityController::class, 'getMembersEdit']); $routeCollection->get('community-members', 'members', [CommunityController::class, 'getMembersEdit']);
$routeCollection->post('community-members-new', 'newMember', [CommunityController::class, 'newMember']); $routeCollection->post('community-members-new', 'newMember', [CommunityController::class, 'saveMember']);
$routeCollection->post('community-members-edit', 'editMember', [CommunityController::class, 'editMember']); $routeCollection->post('community-members-edit', 'editMember', [CommunityController::class, 'saveMember']);
$routeCollection->post('community-members-delete', 'deleteMember', [CommunityController::class, 'deleteMember']); $routeCollection->post('community-members-delete', 'deleteMember', [CommunityController::class, 'deleteMember']);
$routeCollection->get('community-currencies', 'currencies', [CommunityController::class, 'getCurrenciesEdit']); $routeCollection->get('community-currencies', 'currencies', [CommunityController::class, 'getCurrenciesEdit']);
$routeCollection->post('community-currencies-new', 'newCurrency', [CommunityController::class, 'newCurrency']); $routeCollection->post('community-currencies-new', 'newCurrency', [CommunityController::class, 'saveCurrency']);
$routeCollection->post('community-currencies-edit', 'editCurrency', [CommunityController::class, 'editCurrency']); $routeCollection->post('community-currencies-edit', 'editCurrency', [CommunityController::class, 'saveCurrency']);
$routeCollection->post('community-currencies-delete', 'deleteCurrency', [CommunityController::class, 'deleteCurrency']); $routeCollection->post('community-currencies-delete', 'deleteCurrency', [CommunityController::class, 'deleteCurrency']);
$routeCollection->group('currencyExchangeRates', function (RouteCollection $routeCollection) { $routeCollection->group('currencyExchangeRates', function (RouteCollection $routeCollection) {
$routeCollection->get('community-currency-exchange-rates', '{code}', [CommunityController::class, 'getCurrencyExchangeRates']); $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-new', '{code}/new', [CommunityController::class, 'saveCurrencyExchangeRate']);
$routeCollection->post('community-currency-exchange-rates-edit', '{code}/edit', [CommunityController::class, 'editCurrencyExchangeRate']); $routeCollection->post('community-currency-exchange-rates-edit', '{code}/edit', [CommunityController::class, 'saveCurrencyExchangeRate']);
$routeCollection->post('community-currency-exchange-rates-delete', '{code}/delete', [CommunityController::class, 'deleteCurrencyExchangeRate']); $routeCollection->post('community-currency-exchange-rates-delete', '{code}/delete', [CommunityController::class, 'deleteCurrencyExchangeRate']);
}); });
}); });