Skip to content

Commit

Permalink
Merge pull request #2 from rigonlucas/user-list-with-collection
Browse files Browse the repository at this point in the history
Response collection for user
  • Loading branch information
rigonlucas authored Aug 24, 2024
2 parents c0e9379 + 0128632 commit 5ae93bd
Show file tree
Hide file tree
Showing 13 changed files with 481 additions and 8 deletions.
37 changes: 37 additions & 0 deletions app/Http/Controllers/V1/Users/AccountUserListController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Controllers\V1\Users;

use App\Http\Controllers\Controller;
use Core\Application\User\Commons\Gateways\UserRepositoryInterface;
use Core\Services\Framework\FrameworkContract;
use Core\Support\Exceptions\MentodMustBeImplementedException;
use Core\Support\Http\ResponseStatus;
use Core\Support\Permissions\UserRoles;
use Illuminate\Http\Request;

class AccountUserListController extends Controller
{
public function __construct(
private readonly FrameworkContract $framework,
private readonly UserRepositoryInterface $userRepository
) {
}


/**
* @throws MentodMustBeImplementedException
*/
public function __invoke(Request $request)
{
$user = $this->framework->auth()->user();
if ($user->hasNotPermission(UserRoles::ADMIN)) {
abort(ResponseStatus::FORBIDDEN->value);
}
$accountEntity = $user->getAccount();

$users = $this->userRepository->accountUserList($accountEntity);

return response()->json($users->paginated());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Core\Application\User\Commons\Gateways;

use Core\Domain\Collections\User\UserCollection;
use Core\Domain\Entities\Account\AccountEntity;
use Core\Domain\Entities\User\UserEntity;

interface UserRepositoryInterface
Expand All @@ -15,4 +17,6 @@ public function findByEmail(string $email): ?UserEntity;
public function existsEmail(string $email): bool;

public function existsId(int $id): bool;

public function accountUserList(AccountEntity $account): UserCollection;
}
36 changes: 36 additions & 0 deletions core/Domain/Collections/User/UserCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Core\Domain\Collections\User;

use Core\Domain\Entities\User\UserEntity;
use Core\Support\Collections\CollectionBase;
use Core\Support\Collections\Paginations\Simple\HasDefaultPagination;

class UserCollection extends CollectionBase
{
use HasDefaultPagination;

public function add(UserEntity $user): self
{
$this->items[] = $user;
return $this;
}

public function toArray(): array
{
return array_map(
fn(UserEntity $user) => [
'uuid' => $user->getUuid(),
'name' => $user->getName(),
'email' => $user->getEmail(),
'account' => [
'uuid' => $user->getAccount()->getUuid(),
'name' => $user->getAccount()->getName(),
],
'birthday' => $user->getBirthday()->getTimestamp(),
'role' => $user->getRoleName(),
],
$this->items
);
}
}
48 changes: 48 additions & 0 deletions core/Support/Collections/CollectionBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Core\Support\Collections;

use ArrayIterator;
use Core\Support\Collections\Paginations\toArrayInterface;
use Core\Support\Exceptions\MentodMustBeImplementedException;
use Countable;
use IteratorAggregate;
use Traversable;

class CollectionBase implements IteratorAggregate, Countable, toArrayInterface
{
protected array $items = [];

public function count(): int
{
return count($this->items);
}

public function getIterator(): Traversable
{
return new ArrayIterator($this->items);
}

public function remove(int $index): void
{
unset($this->items[$index]);
}

public function exists(int $index): bool
{
return isset($this->items[$index]);
}

public function getItens(): array
{
return $this->items;
}

/**
* @throws MentodMustBeImplementedException
*/
public function toArray(): array
{
throw new MentodMustBeImplementedException('Method toArray not implemented');
}
}
181 changes: 181 additions & 0 deletions core/Support/Collections/Paginations/Simple/HasDefaultPagination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<?php

namespace Core\Support\Collections\Paginations\Simple;

use Core\Support\Exceptions\MentodMustBeImplementedException;
use Exception;

trait HasDefaultPagination
{
private int $currentPage = 1;
private ?string $firstPageUrl = null;
private int $from = 1;
private int $lastPage = 1;
private ?string $lastPageUrl = null;
private array $links = [];
private ?string $nextPageUrl = null;
private ?string $path = null;
private int $perPage = 15;
private ?string $prevPageUrl = null;
private int $to = 1;
private int $total = 0;

/**
* @throws MentodMustBeImplementedException
* @throws Exception
*/
public function paginated(): array
{
if (!method_exists($this, 'toArray')) {
throw new MentodMustBeImplementedException('Method toArray() not found in class ' . get_class($this));
}
return [
'current_page' => $this->getCurrentPage(),
'data' => $this->toArray(),
'first_page_url' => $this->getFirstPageUrl(),
'from' => $this->getFrom(),
'last_page' => $this->getLastPage(),
'last_page_url' => $this->getLastPageUrl(),
'links' => $this->getLinks(),
'next_page_url' => $this->getNextPageUrl(),
'path' => $this->getPath(),
'per_page' => $this->getPerPage(),
'prev_page_url' => $this->getPrevPageUrl(),
'to' => $this->getTo(),
'total' => $this->getTotal(),
];
}

public function getCurrentPage(): int
{
return $this->currentPage;
}

public function setCurrentPage(int $currentPage): self
{
$this->currentPage = $currentPage;
return $this;
}

public function getFirstPageUrl(): ?string
{
return $this->firstPageUrl;
}

public function setFirstPageUrl(?string $firstPageUrl): self
{
$this->firstPageUrl = $firstPageUrl;
return $this;
}

public function getFrom(): int
{
return $this->from;
}

public function setFrom(int $from): self
{
$this->from = $from;
return $this;
}

public function getLastPage(): int
{
return $this->lastPage;
}

public function setLastPage(int $lastPage): self
{
$this->lastPage = $lastPage;
return $this;
}

public function getLastPageUrl(): ?string
{
return $this->lastPageUrl;
}

public function setLastPageUrl(?string $lastPageUrl): self
{
$this->lastPageUrl = $lastPageUrl;
return $this;
}

public function getLinks(): array
{
return $this->links;
}

public function setLinks(array $links): self
{
$this->links = $links;
return $this;
}

public function getNextPageUrl(): ?string
{
return $this->nextPageUrl;
}

public function setNextPageUrl(?string $nextPageUrl): self
{
$this->nextPageUrl = $nextPageUrl;
return $this;
}

public function getPath(): ?string
{
return $this->path;
}

public function setPath(?string $path): self
{
$this->path = $path;
return $this;
}

public function getPerPage(): int
{
return $this->perPage;
}

public function setPerPage(int $perPage): self
{
$this->perPage = $perPage;
return $this;
}

public function getPrevPageUrl(): ?string
{
return $this->prevPageUrl;
}

public function setPrevPageUrl(?string $prevPageUrl): self
{
$this->prevPageUrl = $prevPageUrl;
return $this;
}

public function getTo(): int
{
return $this->to;
}

public function setTo(int $to): self
{
$this->to = $to;
return $this;
}

public function getTotal(): int
{
return $this->total;
}

public function setTotal(int $total): self
{
$this->total = $total;
return $this;
}

}
8 changes: 8 additions & 0 deletions core/Support/Collections/Paginations/toArrayInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Core\Support\Collections\Paginations;

interface toArrayInterface
{
public function toArray(): array;
}
10 changes: 10 additions & 0 deletions core/Support/Exceptions/MentodMustBeImplementedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Core\Support\Exceptions;

use Exception;

class MentodMustBeImplementedException extends Exception
{

}
Loading

0 comments on commit 5ae93bd

Please sign in to comment.