Skip to content

Commit

Permalink
Merge pull request laravel#48 from eurides-eu/feature/roles-endpoints
Browse files Browse the repository at this point in the history
Feature/roles endpoints
  • Loading branch information
jaureguivictoria authored May 22, 2018
2 parents bc8aa6d + c8ddabe commit edd18f1
Show file tree
Hide file tree
Showing 20 changed files with 863 additions and 44 deletions.
2 changes: 1 addition & 1 deletion app/Entities/Repositories/EloquentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class EloquentRepository
/**
* @var Model
*/
private $items;
protected $items;

/**
* @param Model $items
Expand Down
109 changes: 109 additions & 0 deletions app/Http/Controllers/OrganizationRolesController.php
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 app/Http/Requests/Organizations/OrganizationRoleRequest.php
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)],
];
}
}
11 changes: 10 additions & 1 deletion app/Models/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Organization extends Model
* @var array
*/
protected $fillable = [
'name', 'reference_code',
'name',
'reference_code',
];

/**
Expand Down Expand Up @@ -64,4 +65,12 @@ public function defaultBudget()
{
return $this->hasOne(OrganizationBudget::class, 'id', 'default_budget_id');
}

/*
* Get the roles of the organization.
*/
public function roles()
{
return $this->hasMany(OrganizationRole::class);
}
}
47 changes: 47 additions & 0 deletions app/Models/OrganizationRole.php
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();
}
}
8 changes: 8 additions & 0 deletions app/Models/OrganizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ public function organization()
{
return $this->belongsTo(Organization::class);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function roles()
{
return $this->belongsToMany(OrganizationRole::class, 'organization_role_services')->withTimestamps();
}
}
43 changes: 43 additions & 0 deletions app/Organizations/Roles/CommandHandlers/CreateOrganizationRole.php
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 app/Organizations/Roles/CommandHandlers/DeleteOrganizationRole.php
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 app/Organizations/Roles/CommandHandlers/UpdateOrganizationRole.php
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;
}
}
Loading

0 comments on commit edd18f1

Please sign in to comment.