-
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 #2 from rigonlucas/user-list-with-collection
Response collection for user
- Loading branch information
Showing
13 changed files
with
481 additions
and
8 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
app/Http/Controllers/V1/Users/AccountUserListController.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,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()); | ||
} | ||
} |
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,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 | ||
); | ||
} | ||
} |
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,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
181
core/Support/Collections/Paginations/Simple/HasDefaultPagination.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,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; | ||
} | ||
|
||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Core\Support\Collections\Paginations; | ||
|
||
interface toArrayInterface | ||
{ | ||
public function toArray(): array; | ||
} |
10 changes: 10 additions & 0 deletions
10
core/Support/Exceptions/MentodMustBeImplementedException.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,10 @@ | ||
<?php | ||
|
||
namespace Core\Support\Exceptions; | ||
|
||
use Exception; | ||
|
||
class MentodMustBeImplementedException extends Exception | ||
{ | ||
|
||
} |
Oops, something went wrong.