Commit 3edce393 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Add pagination to doors and also searching

parent 62478b6b
......@@ -8,7 +8,7 @@ use Source\UseCases\Doors\GetDoor\GetDoorUseCase;
use Source\UseCases\Doors\CreateDoor\CreateDoorUseCase;
use Source\UseCases\Doors\DeleteDoor\DeleteDoorUseCase;
use Source\UseCases\Doors\UpdateDoor\UpdateDoorUseCase;
use Source\UseCases\Doors\GetAllDoors\GetAllDoorsUseCase;
use Source\UseCases\Doors\GetDoors\GetDoorsUseCase;
use Source\UseCases\DoorGroup\GetDoorGroups\GetDoorGroupsUseCase;
use Source\UseCases\DoorGroup\AddDoorToGroup\AddDoorToGroupUseCase;
use Source\UseCases\Doors\GenerateDoorToken\GenerateDoorTokenUseCase;
......@@ -17,7 +17,7 @@ use Source\UseCases\Doors\CreateDoor\APIPresenter as CreateDoorAPIPresenter;
use Source\UseCases\Doors\DeleteDoor\APIPresenter as DeleteDoorAPIPresenter;
use Source\UseCases\Doors\UpdateDoor\APIPresenter as UpdateDoorAPIPresenter;
use Source\UseCases\DoorGroup\RemoveDoorFromGroup\RemoveDoorFromGroupUseCase;
use Source\UseCases\Doors\GetAllDoors\APIPresenter as GetAllDoorsAPIPresenter;
use Source\UseCases\Doors\GetDoors\APIPresenter as GetAllDoorsAPIPresenter;
use Source\UseCases\DoorGroup\GetDoorGroups\APIPresenter as GetDoorGroupsAPIPresenter;
use Source\UseCases\DoorGroup\AddDoorToGroup\APIPresenter as AddDoorToGroupAPIPresenter;
use Source\UseCases\DoorGroup\RemoveDoorFromGroup\APIPresenter as RemoveDoorFromGroupAPIPresenter;
......@@ -25,20 +25,27 @@ use Source\UseCases\DoorGroup\RemoveDoorFromGroup\APIPresenter as RemoveDoorFrom
class DoorsController extends ApiController
{
/**
* @param \Source\UseCases\Doors\GetAllDoors\GetAllDoorsUseCase $getDoors
* @param \Source\UseCases\Doors\GetDoors\GetDoorsUseCase $getDoors
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Illuminate\Validation\ValidationException
*/
public function index(GetAllDoorsUseCase $getDoors): JsonResponse
public function index(GetDoorsUseCase $getDoors): JsonResponse
{
$this->authorizer->protect(Permissions::MANAGE_DOORS);
$this->validate($this->request, [
'query' => 'string'
]);
$presenter = new GetAllDoorsAPIPresenter();
$getDoors->all($presenter);
$getDoors->query($this->request->input('query'), $presenter);
return $this->respondWithData($presenter->getViewModel());
return $this->respondWithData($presenter->getViewModel([
'query' => $this->request->input('query'),
]));
}
/**
......
......@@ -30,7 +30,7 @@ use Source\UseCases\Users\CreateUser\CreateUserUseCaseServiceProvider;
use Source\UseCases\Users\DeleteUser\DeleteUserUseCaseServiceProvider;
use Source\UseCases\Users\UpdateUser\UpdateUserUseCaseServiceProvider;
use Source\Gateways\DoorSchedule\DoorScheduleRepositoryServiceProvider;
use Source\UseCases\Doors\GetAllDoors\GetAllDoorsUseCaseServiceProvider;
use Source\UseCases\Doors\GetDoors\GetDoorsUseCaseServiceProvider;
use Source\UseCases\Users\GetAllUsers\GetAllUsersUseCaseServiceProvider;
use Source\Gateways\RecurrenceSet\RecurrenceSetRepositoryServiceProvider;
use Source\UseCases\Door\UpdateBinary\UpdateBinaryUseCaseServiceProvider;
......@@ -142,7 +142,7 @@ class AppServiceProvider extends ServiceProvider
CreateDoorUseCaseServiceProvider::class,
DeleteDoorUseCaseServiceProvider::class,
UpdateDoorUseCaseServiceProvider::class,
GetAllDoorsUseCaseServiceProvider::class,
GetDoorsUseCaseServiceProvider::class,
UserAuthenticateUseCaseServiceProvider::class,
GenerateDoorTokenUseCaseServiceProvider::class,
......
......@@ -19,7 +19,7 @@ class LocalDoorScheduleRepository extends InMemoryDoorScheduleRepository
* then we can add all the schedules to the doors here.
*/
try {
foreach ($doors->all() as $door) {
foreach ($doors->query() as $door) {
foreach ($doorGroups->getGroupsForDoor($door->getId()) as $group) {
foreach ($schedules->allForGroup($group->getId()) as $schedule) {
$this->attachScheduleToDoor($door->getId(), $schedule);
......
......@@ -28,7 +28,7 @@ class LocalDoorUserRepository extends InMemoryDoorUserRepository
}
}
foreach ($doors->all() as $door) {
foreach ($doors->query() as $door) {
try {
foreach ($doorGroup->getGroupsForDoor($door->getId()) as $group) {
$this->addGroupToDoor($door->getId(), $group);
......
......@@ -104,9 +104,18 @@ class DatabaseDoorsRepository implements DoorsRepository
/**
* @inheritDoc
*/
public function all(): array
public function query(?string $query = null): array
{
$doors = \App\Door::all()->values()->all();
$doors = [];
if (!$query) {
$doors = \App\Door::all()->values()->all();
} else {
$doors = \App\Door::query()
->where('location', 'LIKE', "%$query%")
->where('name', 'LIKE', "%$query%")
->where('version', 'LIKE', "%$query%")
->get()->values()->all();
}
return array_map(
static function (\App\Door $door) {
......
......@@ -19,11 +19,12 @@ interface DoorsRepository
public function create(Door $door): ?Door;
/**
* Get all doors
* Queries doors, if query is empty or null it gets all doors.
*
* @param string|null $query
* @return Door[]
*/
public function all(): array;
public function query(?string $query = null): array;
/**
* Attempt to find a door by a token
......
......@@ -50,6 +50,7 @@ class InMemoryDoorsRepository implements DoorsRepository
if ($door->hasIdOf($doorId)) {
$door->setVersion($version);
$door->setLastSeenAt($seen);
return;
}
}
......@@ -72,9 +73,17 @@ class InMemoryDoorsRepository implements DoorsRepository
/**
* @inheritDoc
*/
public function all(): array
public function query(?string $query = null): array
{
return $this->doors;
if (!$query) {
return $this->doors;
}
return array_filter($this->doors, static function (Door $door) use ($query) {
return strpos($door->getLocation(), $query) !== false
|| strpos($door->getName(), $query) !== false
|| strpos($door->getVersion(), $query) !== false;
});
}
/**
......
......@@ -8,11 +8,11 @@ trait Paginates
{
/**
* @param array $items
* @param int $perPage
* @param array $appends
* @param int $perPage
* @return array
*/
protected function paginate(array $items, int $perPage = 50, array $appends = []): array
protected function paginate(array $items, array $appends = [], int $perPage = 50): array
{
$currentPage = LengthAwarePaginator::resolveCurrentPage();
......
......@@ -21,8 +21,8 @@ class APIPresenter extends BasePresenter implements Presenter
}
/** @inheritDoc */
public function getViewModel(array $appends = [], int $perPage = 50): array
public function getViewModel(array $appends = []): array
{
return $this->paginate($this->attempts, $perPage, $appends);
return $this->paginate($this->attempts, $appends);
}
}
......@@ -12,8 +12,7 @@ interface Presenter
/**
* @param array $appends
* @param int $perPage
* @return array
*/
public function getViewModel(array $appends = [], int $perPage = 50): array;
public function getViewModel(array $appends = []): array;
}
<?php
namespace Source\UseCases\Doors\GetAllDoors;
interface GetAllDoorsUseCase
{
/**
* @param \Source\UseCases\Doors\GetAllDoors\Presenter $presenter
*/
public function all(Presenter $presenter): void;
}
<?php
namespace Source\UseCases\Doors\GetAllDoors;
namespace Source\UseCases\Doors\GetDoors;
use Source\Entities\Door;
use Source\Sanitize\Paginates;
use Source\UseCases\BasePresenter;
class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
use Paginates;
protected array $doors = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['doors'] = array_map(static function (Door $door) {
$this->doors = array_map(static function (Door $door) {
return [
'id' => $door->getId(),
'name' => $door->getName(),
......@@ -22,8 +25,8 @@ class APIPresenter extends BasePresenter implements Presenter
}
/** @inheritDoc */
public function getViewModel(): array
public function getViewModel(array $appends = []): array
{
return $this->viewModel;
return $this->paginate($this->doors, $appends);
}
}
<?php
namespace Source\UseCases\Doors\GetAllDoors;
namespace Source\UseCases\Doors\GetDoors;
use Source\Gateways\Doors\DoorsRepository;
class GetAllDoors implements GetAllDoorsUseCase
class GetDoors implements GetDoorsUseCase
{
/**
* @var \Source\Gateways\Doors\DoorsRepository
......@@ -19,11 +19,11 @@ class GetAllDoors implements GetAllDoorsUseCase
/**
* @inheritDoc
*/
public function all(Presenter $presenter): void
public function query(?string $query, Presenter $presenter): void
{
$response = new ResponseModel();
foreach ($this->doorsRepository->all() as $door) {
foreach ($this->doorsRepository->query($query) as $door) {
$response->addDoor($door);
}
......
<?php
namespace Source\UseCases\Doors\GetDoors;
interface GetDoorsUseCase
{
/**
* @param string|null $query
* @param \Source\UseCases\Doors\GetDoors\Presenter $presenter
*/
public function query(?string $query, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\Doors\GetAllDoors;
namespace Source\UseCases\Doors\GetDoors;
use Illuminate\Support\ServiceProvider;
use Source\Gateways\Doors\DoorsRepository;
......@@ -11,7 +11,7 @@ use Illuminate\Contracts\Support\DeferrableProvider;
/**
* Service provider must be registered in AppServiceProvider
*/
class GetAllDoorsUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
class GetDoorsUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
......@@ -20,8 +20,8 @@ class GetAllDoorsUseCaseServiceProvider extends ServiceProvider implements Defer
*/
public function register()
{
$this->app->bind(GetAllDoorsUseCase::class, static function (Application $app) {
return new GetAllDoors($app->make(DoorsRepository::class));
$this->app->bind(GetDoorsUseCase::class, static function (Application $app) {
return new GetDoors($app->make(DoorsRepository::class));
});
}
......@@ -39,6 +39,6 @@ class GetAllDoorsUseCaseServiceProvider extends ServiceProvider implements Defer
*/
public function provides()
{
return [GetAllDoorsUseCase::class];
return [GetDoorsUseCase::class];
}
}
<?php
namespace Source\UseCases\Doors\GetAllDoors;
namespace Source\UseCases\Doors\GetDoors;
interface Presenter
{
......@@ -11,7 +11,8 @@ interface Presenter
public function present(ResponseModel $responseModel): void;
/**
* @param array $appends
* @return array
*/
public function getViewModel(): array;
public function getViewModel(array $appends = []): array;
}
<?php
namespace Source\UseCases\Doors\GetAllDoors;
namespace Source\UseCases\Doors\GetDoors;
use Source\Entities\Door;
......
......@@ -37,7 +37,7 @@ class DoorDatabaseTest extends DatabaseTestCase
$this->assertNull($this->doors->getByToken(new HashedSearchable('i am autistic')));
$this->assertNotNull($this->doors->get($d->getId()));
$this->assertNull($this->doors->get('asdf'));
$this->assertCount(1, $this->doors->all());
$this->assertCount(1, $this->doors->query());
$this->assertNotNull($this->doors->findByName('name'));
$this->assertNull($this->doors->findByName('ree'));
$this->assertNull($this->doors->findByName(null));
......@@ -51,9 +51,9 @@ class DoorDatabaseTest extends DatabaseTestCase
{
$d = $this->doors->create(new Door(0, 'loc', 'name', new HashedSearchable('token')));
$this->assertFalse($this->doors->delete('ah idk if im gonna finish this project. Im sorry'));
$this->assertCount(1, $this->doors->all());
$this->assertCount(1, $this->doors->query());
$this->assertTrue($this->doors->delete($d->getId()));
$this->assertCount(0, $this->doors->all());
$this->assertCount(0, $this->doors->query());
}
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment