merge new and edit methods in CommunityController
rvr-nextgen/pipeline/pr-master This commit looks good
rvr-nextgen/pipeline/pr-master This commit looks good
This commit is contained in:
2 files changed
+49
-82
No files matched your search
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user