forked from laravel/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request laravel#50 from eurides-eu/feature/add-organizatio…
…n-budgets-endpoints Organization budgets endpoints
- Loading branch information
Showing
15 changed files
with
578 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\OrganizationBudgets\CreateOrganizationBudgetRequest; | ||
use App\Http\Requests\OrganizationBudgets\DeleteOrganizationBudgetRequest; | ||
use App\Organizations\Budgets\Commands\CreateOrganizationBudget; | ||
use App\Organizations\Budgets\Commands\DeleteOrganizationBudget; | ||
use App\Organizations\Budgets\Commands\UpdateOrganizationBudget; | ||
use App\Organizations\Budgets\Repositories\ReadRepository; | ||
use App\Organizations\Budgets\Transformers\OrganizationBudgetTransformer; | ||
use App\Organizations\Repositories\ReadRepository as OrganizationReadRepository; | ||
|
||
class OrganizationBudgetsController extends ApiController | ||
{ | ||
/** | ||
* @var ReadRepository | ||
*/ | ||
protected $readRepository; | ||
|
||
/** | ||
* @var OrganizationReadRepository | ||
*/ | ||
protected $organizationReadRepository; | ||
|
||
/** | ||
* OrganizationBudgetsController constructor. | ||
* | ||
* @param OrganizationBudgetTransformer $organizationBudgetTransformer | ||
* @param ReadRepository $readRepository | ||
* @param OrganizationReadRepository $organizationReadRepository | ||
*/ | ||
public function __construct( | ||
OrganizationBudgetTransformer $organizationBudgetTransformer, | ||
ReadRepository $readRepository, | ||
OrganizationReadRepository $organizationReadRepository) | ||
{ | ||
$this->setTransformer($organizationBudgetTransformer); | ||
$this->readRepository = $readRepository; | ||
$this->organizationReadRepository = $organizationReadRepository; | ||
} | ||
|
||
/** | ||
* @param string $organizationId | ||
* | ||
* @return \Psr\Http\Message\ResponseInterface | ||
*/ | ||
public function all(string $organizationId) | ||
{ | ||
$budgets = $this->readRepository->allForOrganization($organizationId); | ||
|
||
return $this->responsePaginator($budgets); | ||
} | ||
|
||
/** | ||
* @param string $organizationId | ||
* @param string $organizationBudgetId | ||
* @param CreateOrganizationBudgetRequest $request | ||
* | ||
* @return \Psr\Http\Message\ResponseInterface | ||
*/ | ||
public function update(string $organizationId, string $organizationBudgetId, CreateOrganizationBudgetRequest $request) | ||
{ | ||
$organizationBudget = $this->readRepository->findForOrganization($organizationId, $organizationBudgetId); | ||
|
||
$budget = $this->dispatch(new UpdateOrganizationBudget( | ||
$organizationBudget, | ||
$request->get('name'), | ||
$request->get('maxAmount'), | ||
$request->get('reference'), | ||
$request->get('approverEmail'), | ||
$request->get('autoApproveAmountLimit'), | ||
$request->get('autoApproveMonthlyLimit'), | ||
$request->get('autoApproveAfter') | ||
)); | ||
|
||
return $this->responseItem($budget); | ||
} | ||
|
||
/** | ||
* @param string $organizationId | ||
* @param CreateOrganizationBudgetRequest $request | ||
* | ||
* @return \Psr\Http\Message\ResponseInterface | ||
*/ | ||
public function create(string $organizationId, CreateOrganizationBudgetRequest $request) | ||
{ | ||
$organization = $this->organizationReadRepository->find($organizationId); | ||
|
||
$budget = $this->dispatch(new CreateOrganizationBudget( | ||
$organization, | ||
$request->get('name'), | ||
$request->get('maxAmount'), | ||
$request->get('reference'), | ||
$request->get('approverEmail'), | ||
$request->get('autoApproveAmountLimit'), | ||
$request->get('autoApproveMonthlyLimit'), | ||
$request->get('autoApproveAfter') | ||
)); | ||
|
||
return $this->responseCreated($budget); | ||
} | ||
|
||
/** | ||
* @param string $organizationId | ||
* @param string $organizationBudgetId | ||
* @param DeleteOrganizationBudgetRequest $request | ||
* | ||
* @return \Zend\Diactoros\Response\JsonResponse | ||
*/ | ||
public function delete(string $organizationId, string $organizationBudgetId, DeleteOrganizationBudgetRequest $request) | ||
{ | ||
$organizationBudget = $this->readRepository->findForOrganization($organizationId, $organizationBudgetId); | ||
|
||
$this->dispatch(new DeleteOrganizationBudget($organizationBudget)); | ||
|
||
return $this->responseNoContent(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
app/Http/Requests/OrganizationBudgets/CreateOrganizationBudgetRequest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\OrganizationBudgets; | ||
|
||
use App\Http\Requests\FormRequest; | ||
use App\Organizations\Rules\IsOrganizationBudget; | ||
|
||
class CreateOrganizationBudgetRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'name' => 'string|required', | ||
'approverEmail' => 'string|email', | ||
'maxAmount' => 'numeric|required', | ||
'autoApproveAmountLimit' => 'numeric', | ||
'autoApproveMonthlyLimit' => 'numeric', | ||
'autoApproveAfter' => 'string', | ||
'defaultBudgetId' => [new IsOrganizationBudget($this->route('organizationId'))], | ||
]; | ||
} | ||
|
||
protected function validationData() | ||
{ | ||
return array_merge($this->request->all(), [ | ||
'defaultBudgetId' => $this->route('organizationBudgetId'), | ||
]); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
app/Http/Requests/OrganizationBudgets/DeleteOrganizationBudgetRequest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\OrganizationBudgets; | ||
|
||
use App\Http\Requests\FormRequest; | ||
use App\Organizations\Budgets\Rules\CanBudgetBeDeleted; | ||
use App\Organizations\Repositories\ReadRepository; | ||
use App\Organizations\Rules\IsOrganizationBudget; | ||
|
||
class DeleteOrganizationBudgetRequest extends FormRequest | ||
{ | ||
/** | ||
* @var ReadRepository | ||
*/ | ||
protected $organizationReadRepository; | ||
|
||
/** | ||
* DeleteOrganizationBudgetRequest constructor. | ||
* | ||
* @param ReadRepository $organizationReadRepository | ||
*/ | ||
public function __construct(ReadRepository $organizationReadRepository) | ||
{ | ||
$this->organizationReadRepository = $organizationReadRepository; | ||
} | ||
|
||
/** | ||
* Prevent deleting default organization budget. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
$organizationId = $this->route('organizationId'); | ||
|
||
return [ | ||
'organizationBudgetId' => [ | ||
new IsOrganizationBudget($organizationId), | ||
new CanBudgetBeDeleted($organizationId), | ||
], | ||
]; | ||
} | ||
|
||
protected function validationData() | ||
{ | ||
return array_merge($this->request->all(), [ | ||
'organizationBudgetId' => $this->route('organizationBudgetId'), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
app/Organizations/Budgets/CommandHandlers/DeleteOrganizationBudget.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App\Organizations\Budgets\CommandHandlers; | ||
|
||
use App\Organizations\Budgets\Commands\DeleteOrganizationBudget as Command; | ||
use App\Organizations\Budgets\Repositories\WriteRepository; | ||
|
||
class DeleteOrganizationBudget | ||
{ | ||
/** | ||
* @var WriteRepository | ||
*/ | ||
protected $repository; | ||
|
||
/** | ||
* DeleteOrganizationBudget constructor. | ||
* | ||
* @param WriteRepository $repository | ||
*/ | ||
public function __construct(WriteRepository $repository) | ||
{ | ||
$this->repository = $repository; | ||
} | ||
|
||
public function handle(Command $command) | ||
{ | ||
$this->repository->delete($command->getOrganizationBudget()); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
app/Organizations/Budgets/CommandHandlers/UpdateOrganizationBudget.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace App\Organizations\Budgets\CommandHandlers; | ||
|
||
use App\Organizations\Budgets\Commands\UpdateOrganizationBudget as Command; | ||
use App\Organizations\Budgets\Repositories\WriteRepository; | ||
|
||
class UpdateOrganizationBudget | ||
{ | ||
/** | ||
* @var WriteRepository | ||
*/ | ||
protected $writeRepository; | ||
|
||
/** | ||
* UpdateOrganizationBudget constructor. | ||
* | ||
* @param WriteRepository $writeRepository | ||
*/ | ||
public function __construct(WriteRepository $writeRepository) | ||
{ | ||
$this->writeRepository = $writeRepository; | ||
} | ||
|
||
public function handle(Command $command) | ||
{ | ||
return $this->writeRepository->update( | ||
$command->getOrganizationBudget(), | ||
[ | ||
'name' => $command->getName(), | ||
'reference' => $command->getReference(), | ||
'max_amount' => $command->getMaxAmount(), | ||
'approver_email' => $command->getApproverEmail(), | ||
'auto_approve_amount_limit' => $command->getAutoApproveAmountLimit(), | ||
'auto_approve_monthly_limit' => $command->getAutoApproveMonthlyLimit(), | ||
'auto_approve_after' => $command->getAutoApproveAfter(), | ||
] | ||
); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
app/Organizations/Budgets/Commands/DeleteOrganizationBudget.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace App\Organizations\Budgets\Commands; | ||
|
||
use App\Models\OrganizationBudget; | ||
|
||
class DeleteOrganizationBudget | ||
{ | ||
/** | ||
* @var OrganizationBudget | ||
*/ | ||
protected $organizationBudget; | ||
|
||
/** | ||
* DeleteOrganizationBudget constructor. | ||
* | ||
* @param OrganizationBudget $organizationBudget | ||
*/ | ||
public function __construct(OrganizationBudget $organizationBudget) | ||
{ | ||
$this->organizationBudget = $organizationBudget; | ||
} | ||
|
||
/** | ||
* @return OrganizationBudget | ||
*/ | ||
public function getOrganizationBudget(): OrganizationBudget | ||
{ | ||
return $this->organizationBudget; | ||
} | ||
} |
Oops, something went wrong.