Skip to content

Commit

Permalink
Merge pull request laravel#49 from eurides-eu/feature/user-endpoints
Browse files Browse the repository at this point in the history
Feature/user overview endpoint
  • Loading branch information
dieterve authored May 23, 2018
2 parents edd18f1 + 22bdd9b commit b79487f
Show file tree
Hide file tree
Showing 13 changed files with 265 additions and 24 deletions.
17 changes: 5 additions & 12 deletions app/Console/Commands/Users/CreateUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,14 @@ class CreateUser extends Command
*
* @var string
*/
protected $signature = 'eurides:user:create {name} {email} {organization_id}';
protected $signature = 'eurides:user:create {first_name} {last_name} {email} {organization_id}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a user for an organization. Arguments: {name} {email} {organization_id}';

/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}
protected $description = 'Create a user for an organization.';

/**
* Execute the console command.
Expand All @@ -39,11 +31,12 @@ public function __construct()
*/
public function handle()
{
$name = $this->argument('name');
$firstName = $this->argument('first_name');
$lastName = $this->argument('last_name');
$email = $this->argument('email');
$organizationId = $this->argument('organization_id');

$user = $this->dispatch(new CreateUserCommand($name, $email, $organizationId));
$user = $this->dispatch(new CreateUserCommand($firstName, $lastName, $email, $organizationId));

$this->info("User created successfully => ID {$user->id}");
}
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/OrganizationRolesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function __construct(
public function all($organizationId)
{
$organization = $this->readRepository->find($organizationId);

$roles = $this->rolesReadRepository->allForOrganization($organization->id);

return $this->responsePaginator($roles);
Expand Down
57 changes: 57 additions & 0 deletions app/Http/Controllers/OrganizationUsersController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Http\Controllers;

use App\Organizations\Repositories\ReadRepository;
use App\Organizations\Roles\Transformers\UserTransformer;
use App\Users\Queries\FindUsersForOrganizationQuery;
use App\Users\Repositories\UsersReadRepository;
use Illuminate\Http\Request;

class OrganizationUsersController extends ApiController
{
/**
* @var ReadRepository
*/
private $organizationsReadRepository;

/**
* @var UsersReadRepository
*/
private $usersReadRepository;

/**
* @param UserTransformer $userTransformer
* @param ReadRepository $organizationsReadRepository
* @param UsersReadRepository $usersReadRepository
*/
public function __construct(
UserTransformer $userTransformer,
ReadRepository $organizationsReadRepository,
UsersReadRepository $usersReadRepository
) {
$this->setTransformer($userTransformer);
$this->organizationsReadRepository = $organizationsReadRepository;
$this->usersReadRepository = $usersReadRepository;
}

/**
* @param Request $request
* @param string $organizationId
* @param FindUsersForOrganizationQuery $query
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function all(Request $request, $organizationId, FindUsersForOrganizationQuery $query)
{
$organization = $this->organizationsReadRepository->find($organizationId);

if ($roleId = $request->get('roleId')) {
$query->filterByRoleId($roleId);
}

$users = $query->find($organization);

return $this->responsePaginator($users);
}
}
9 changes: 9 additions & 0 deletions app/Models/OrganizationRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Traits\UuidModel;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

Expand Down Expand Up @@ -37,6 +38,14 @@ public function organization()
return $this->belongsTo(Organization::class);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function users()
{
return $this->hasMany(User::class);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
Expand Down
26 changes: 24 additions & 2 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App;

use App\Models\Organization;
use App\Models\OrganizationRole;
use App\Traits\UuidModel;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
Expand All @@ -20,7 +21,19 @@ class User extends Authenticatable
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'organization_id',
'first_name',
'last_name',
'email',
'password',
'gender',
'birthdate',
'language',
'address_street',
'address_number',
'address_box',
'address_city',
'address_postal_code',
'address_country_code',
];

/**
Expand All @@ -29,7 +42,8 @@ class User extends Authenticatable
* @var array
*/
protected $hidden = [
'password', 'remember_token',
'password',
'remember_token',
];

/**
Expand All @@ -39,4 +53,12 @@ public function organization()
{
return $this->belongsTo(Organization::class);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function role()
{
return $this->belongsTo(OrganizationRole::class);
}
}
3 changes: 2 additions & 1 deletion app/Users/CommandHandlers/CreateUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public function handle(Command $command)
}

return User::create([
'name' => $command->getName(),
'first_name' => $command->getFirstName(),
'last_name' => $command->getLastName(),
'email' => $command->getEmail(),
'organization_id' => $command->getOrganizationId(),
]);
Expand Down
29 changes: 21 additions & 8 deletions app/Users/Commands/CreateUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ class CreateUser
/**
* @var string
*/
protected $name;
protected $firstName;

/**
* @var string
*/
protected $lastName;

/**
* @var string
Expand All @@ -20,25 +25,33 @@ class CreateUser
protected $organizationId;

/**
* CreateUser constructor.
*
* @param string $name
* @param string $firstName
* @param string $lastName
* @param string $email
* @param string $organizationId
*/
public function __construct(string $name, string $email, string $organizationId)
public function __construct(string $firstName, string $lastName, string $email, string $organizationId)
{
$this->name = $name;
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->email = $email;
$this->organizationId = $organizationId;
}

/**
* @return string
*/
public function getName(): string
public function getFirstName(): string
{
return $this->firstName;
}

/**
* @return string
*/
public function getLastName(): string
{
return $this->name;
return $this->lastName;
}

/**
Expand Down
37 changes: 37 additions & 0 deletions app/Users/Queries/FindUsersForOrganizationQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Users\Queries;

use App\Models\Organization;
use App\User;

class FindUsersForOrganizationQuery
{
/**
* @var User
*/
private $items;

public function __construct()
{
$this->items = User::query();
}

/**
* @param string $roleId
*/
public function filterByRoleId(string $roleId)
{
$this->items = $this->items->where('role_id', $roleId);
}

/**
* @param Organization $organization
*
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
*/
public function find(Organization $organization)
{
return $this->items->where('organization_id', $organization->id)->paginate();
}
}
17 changes: 17 additions & 0 deletions app/Users/Repositories/UsersReadRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Users\Repositories;

use App\Entities\Repositories\EloquentRepository;
use App\User;

class UsersReadRepository extends EloquentRepository
{
/**
* @param User $items
*/
public function __construct(User $items)
{
parent::__construct($items);
}
}
52 changes: 52 additions & 0 deletions app/Users/Transformers/UserTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Organizations\Roles\Transformers;

use App\User;
use League\Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{
/**
* List of resources possible to include.
*
* @var array
*/
protected $defaultIncludes = [
'role',
];

/**
* @param User $user
*
* @return array
*/
public function transform(User $user)
{
return [
'id' => $user->id,
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'email' => $user->email,
'language' => $user->language,
'gender' => $user->gender,
'birthDate' => $user->birthdate,
'addressStreet' => $user->address_street,
'addressNumber' => $user->address_number,
'addressBox' => $user->address_box,
'addressPostalCode' => $user->address_postal_code,
'addressCity' => $user->address_city,
'addressCountryCode' => $user->address_country_code,
];
}

/**
* @param User $user
*
* @return \League\Fractal\Resource\Item
*/
public function includeRole(User $user)
{
return $this->item($user->role, new OrganizationRoleTransformer());
}
}
3 changes: 2 additions & 1 deletion database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

$factory->define(App\User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'email' => $faker->unique()->safeEmail,
'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret
'remember_token' => str_random(10),
Expand Down
Loading

0 comments on commit b79487f

Please sign in to comment.