Skip to content

Commit

Permalink
Merge pull request laravel#50 from eurides-eu/feature/add-organizatio…
Browse files Browse the repository at this point in the history
…n-budgets-endpoints

Organization budgets endpoints
  • Loading branch information
jaureguivictoria authored May 24, 2018
2 parents b79487f + 82e6877 commit 7a5671e
Show file tree
Hide file tree
Showing 15 changed files with 578 additions and 26 deletions.
119 changes: 119 additions & 0 deletions app/Http/Controllers/OrganizationBudgetsController.php
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();
}
}
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'),
]);
}
}
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'),
]);
}
}
5 changes: 2 additions & 3 deletions app/Http/Requests/Organizations/UpdateOrganizationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace App\Http\Requests\Organizations;

use App\Http\Requests\FormRequest;
use App\Organizations\Rules\ValidOrganizationBudget;
use App\Organizations\Rules\IsOrganizationBudget;
use Illuminate\Validation\Rule;

class UpdateOrganizationRequest extends FormRequest
Expand All @@ -26,13 +26,12 @@ public function authorize()
public function rules()
{
$organizationId = $this->route('organizationId');
$defaultBudgetId = $this->input('defaultBudgetId');

return [
'name' => 'string|required',
'reference' => 'string',
'services' => ['array', Rule::in(config('eurides.services'))],
'defaultBudgetId' => ['string', 'required', new ValidOrganizationBudget($organizationId, $defaultBudgetId)],
'defaultBudgetId' => ['string', 'required', new IsOrganizationBudget($organizationId)],
];
}
}
10 changes: 5 additions & 5 deletions app/Models/OrganizationBudget.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class OrganizationBudget extends Model
* @var array
*/
protected $fillable = [
'name', 'reference', 'max_amount', 'approver_email', 'auto_approve_amount_limit', 'auto_approve_monthly_limit', 'auto_approve_after_hours',
'name', 'reference', 'max_amount', 'approver_email', 'auto_approve_amount_limit', 'auto_approve_monthly_limit', 'auto_approve_after',
];

/**
Expand All @@ -28,10 +28,10 @@ class OrganizationBudget extends Model
public static $rules = [
'name' => 'string|required',
'approver_email' => 'string',
'max_amount' => 'float|required',
'auto_approve_amount_limit' => 'float',
'auto_approve_monthly_limit' => 'float',
'auto_approve_after_hours' => 'string',
'max_amount' => 'numeric|required',
'auto_approve_amount_limit' => 'numeric',
'auto_approve_monthly_limit' => 'numeric',
'auto_approve_after' => 'string',
];

/**
Expand Down
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());
}
}
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 app/Organizations/Budgets/Commands/DeleteOrganizationBudget.php
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;
}
}
Loading

0 comments on commit 7a5671e

Please sign in to comment.