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#48 from eurides-eu/feature/roles-endpoints
Feature/roles endpoints
- Loading branch information
Showing
20 changed files
with
863 additions
and
44 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
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,109 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\Organizations\OrganizationRoleRequest; | ||
use App\Organizations\Repositories\ReadRepository; | ||
use App\Organizations\Roles\Commands\CreateOrganizationRole; | ||
use App\Organizations\Roles\Commands\DeleteOrganizationRole; | ||
use App\Organizations\Roles\Commands\UpdateOrganizationRole; | ||
use App\Organizations\Roles\Repositories\RolesReadRepository; | ||
use App\Organizations\Roles\Transformers\OrganizationRoleTransformer; | ||
|
||
class OrganizationRolesController extends ApiController | ||
{ | ||
/** | ||
* @var RolesReadRepository | ||
*/ | ||
private $rolesReadRepository; | ||
|
||
/** | ||
* @var ReadRepository | ||
*/ | ||
private $readRepository; | ||
|
||
/** | ||
* @param OrganizationRoleTransformer $roleTransformer | ||
* @param ReadRepository $readRepository | ||
* @param RolesReadRepository $rolesReadRepository | ||
*/ | ||
public function __construct( | ||
OrganizationRoleTransformer $roleTransformer, | ||
ReadRepository $readRepository, | ||
RolesReadRepository $rolesReadRepository | ||
) { | ||
$this->setTransformer($roleTransformer); | ||
$this->readRepository = $readRepository; | ||
$this->rolesReadRepository = $rolesReadRepository; | ||
} | ||
|
||
/** | ||
* @param string $organizationId | ||
* | ||
* @return \Psr\Http\Message\ResponseInterface | ||
*/ | ||
public function all($organizationId) | ||
{ | ||
$organization = $this->readRepository->find($organizationId); | ||
$roles = $this->rolesReadRepository->allForOrganization($organization->id); | ||
|
||
return $this->responsePaginator($roles); | ||
} | ||
|
||
/** | ||
* @param OrganizationRoleRequest $request | ||
* @param string $organizationId | ||
* | ||
* @return \Psr\Http\Message\ResponseInterface | ||
*/ | ||
public function create(OrganizationRoleRequest $request, $organizationId) | ||
{ | ||
$organization = $this->readRepository->find($organizationId); | ||
|
||
$role = $this->dispatch(new CreateOrganizationRole( | ||
$organization, | ||
$request->get('name'), | ||
$request->get('services') | ||
)); | ||
|
||
return $this->responseCreated($role); | ||
} | ||
|
||
/** | ||
* @param OrganizationRoleRequest $request | ||
* @param string $organizationId | ||
* @param string $roleId | ||
* | ||
* @return \Psr\Http\Message\ResponseInterface | ||
*/ | ||
public function update(OrganizationRoleRequest $request, $organizationId, $roleId) | ||
{ | ||
$organization = $this->readRepository->find($organizationId); | ||
$role = $this->rolesReadRepository->findForOrganization($roleId, $organizationId); | ||
|
||
$role = $this->dispatch(new UpdateOrganizationRole( | ||
$organization, | ||
$role, | ||
$request->get('name'), | ||
$request->get('services') | ||
)); | ||
|
||
return $this->responseItem($role); | ||
} | ||
|
||
/** | ||
* @param string $organizationId | ||
* @param string $roleId | ||
* | ||
* @return \Psr\Http\Message\ResponseInterface | ||
*/ | ||
public function delete($organizationId, $roleId) | ||
{ | ||
$organization = $this->readRepository->find($organizationId); | ||
$role = $this->rolesReadRepository->findForOrganization($roleId); | ||
|
||
$this->dispatch(new DeleteOrganizationRole($organization, $role)); | ||
|
||
return $this->responseNoContent(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
app/Http/Requests/Organizations/OrganizationRoleRequest.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,35 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Organizations; | ||
|
||
use App\Http\Requests\FormRequest; | ||
use App\Organizations\Rules\ValidOrganizationService; | ||
|
||
class OrganizationRoleRequest 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() | ||
{ | ||
$organizationId = $this->route('organizationId'); | ||
$services = (array) $this->input('services'); | ||
|
||
return [ | ||
'name' => 'string|required', | ||
'services' => ['required', 'array', new ValidOrganizationService($organizationId, $services)], | ||
]; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use App\Traits\UuidModel; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
|
||
class OrganizationRole extends Model | ||
{ | ||
use SoftDeletes; | ||
use UuidModel; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'name', | ||
]; | ||
|
||
/** | ||
* Validation rules. | ||
* | ||
* @var array | ||
*/ | ||
public static $rules = [ | ||
'name' => 'required', | ||
]; | ||
|
||
/** | ||
* Get the users of the organization. | ||
*/ | ||
public function organization() | ||
{ | ||
return $this->belongsTo(Organization::class); | ||
} | ||
|
||
/** | ||
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | ||
*/ | ||
public function services() | ||
{ | ||
return $this->belongsToMany(OrganizationService::class, 'organization_role_services')->withTimestamps(); | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
app/Organizations/Roles/CommandHandlers/CreateOrganizationRole.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,43 @@ | ||
<?php | ||
|
||
namespace App\Organizations\Roles\CommandHandlers; | ||
|
||
use App\Models\OrganizationRole; | ||
use App\Organizations\Roles\Commands\CreateOrganizationRole as Command; | ||
use App\Organizations\Roles\Repositories\WriteRepository; | ||
|
||
class CreateOrganizationRole | ||
{ | ||
/** | ||
* @var WriteRepository | ||
*/ | ||
private $writeRepository; | ||
|
||
/** | ||
* @param WriteRepository $writeRepository | ||
*/ | ||
public function __construct(WriteRepository $writeRepository) | ||
{ | ||
$this->writeRepository = $writeRepository; | ||
} | ||
|
||
/** | ||
* @param Command $command | ||
* | ||
* @return OrganizationRole | ||
*/ | ||
public function handle(Command $command) | ||
{ | ||
$organization = $command->getOrganization(); | ||
$services = $organization->services()->whereIn('service', $command->getServices())->get(); | ||
|
||
$role = $this->writeRepository->addRole( | ||
$organization, | ||
['name' => $command->getName()] | ||
); | ||
|
||
$role->services()->sync($services); | ||
|
||
return $role; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
app/Organizations/Roles/CommandHandlers/DeleteOrganizationRole.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,30 @@ | ||
<?php | ||
|
||
namespace App\Organizations\Roles\CommandHandlers; | ||
|
||
use App\Organizations\Roles\Commands\DeleteOrganizationRole as Command; | ||
use App\Organizations\Roles\Repositories\WriteRepository; | ||
|
||
class DeleteOrganizationRole | ||
{ | ||
/** | ||
* @var WriteRepository | ||
*/ | ||
private $writeRepository; | ||
|
||
/** | ||
* @param WriteRepository $writeRepository | ||
*/ | ||
public function __construct(WriteRepository $writeRepository) | ||
{ | ||
$this->writeRepository = $writeRepository; | ||
} | ||
|
||
/** | ||
* @param Command $command | ||
*/ | ||
public function handle(Command $command) | ||
{ | ||
$this->writeRepository->delete($command->getRole()); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
app/Organizations/Roles/CommandHandlers/UpdateOrganizationRole.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,43 @@ | ||
<?php | ||
|
||
namespace App\Organizations\Roles\CommandHandlers; | ||
|
||
use App\Models\OrganizationRole; | ||
use App\Organizations\Roles\Commands\UpdateOrganizationRole as Command; | ||
use App\Organizations\Roles\Repositories\WriteRepository; | ||
|
||
class UpdateOrganizationRole | ||
{ | ||
/** | ||
* @var WriteRepository | ||
*/ | ||
private $writeRepository; | ||
|
||
/** | ||
* @param WriteRepository $writeRepository | ||
*/ | ||
public function __construct(WriteRepository $writeRepository) | ||
{ | ||
$this->writeRepository = $writeRepository; | ||
} | ||
|
||
/** | ||
* @param Command $command | ||
* | ||
* @return OrganizationRole | ||
*/ | ||
public function handle(Command $command) | ||
{ | ||
$organization = $command->getOrganization(); | ||
$services = $organization->services()->whereIn('service', $command->getServices())->get(); | ||
|
||
$role = $this->writeRepository->update( | ||
$command->getRole(), | ||
['name' => $command->getName()] | ||
); | ||
|
||
$role->services()->sync($services); | ||
|
||
return $role; | ||
} | ||
} |
Oops, something went wrong.