Commit cca6001a authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Add getting groups for a specific door

parent f543ac4c
......@@ -9,6 +9,8 @@ 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\DoorGroup\GetDoorGroups\APIPresenter as GetDoorGroupsAPIPresenter;
use Source\UseCases\DoorGroup\GetDoorGroups\GetDoorGroupsUseCase;
use Source\UseCases\Doors\GenerateDoorToken\GenerateDoorTokenUseCase;
use Source\UseCases\Doors\GetDoor\APIPresenter as GetDoorAPIPresenter;
use Source\UseCases\Doors\CreateDoor\APIPresenter as CreateDoorAPIPresenter;
......@@ -139,4 +141,22 @@ class DoorsController extends ApiController
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\DoorGroup\GetDoorGroups\GetDoorGroupsUseCase $doorGroups
* @param string $doorId
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function getGroupsForDoor(GetDoorGroupsUseCase $doorGroups, string $doorId): JsonResponse
{
$this->authorizer->protect(Permissions::MANAGE_DOORS);
$presenter = new GetDoorGroupsAPIPresenter();
$doorGroups->getGroupsForDoor($doorId, $presenter);
return $this->respondWithData($presenter->getViewModel());
}
}
......@@ -30,6 +30,7 @@ use Source\UseCases\Groups\GetAllGroups\GetAllGroupsUseCaseServiceProvider;
use Source\UseCases\GroupUser\GetGroupUsers\GetGroupUsersUseCaseServiceProvider;
use Source\UseCases\GroupUser\GetUserGroups\GetUserGroupsUseCaseServiceProvider;
use Source\UseCases\DoorGroup\GetGroupDoors\GetGroupDoorsUseCaseServiceProvider;
use Source\UseCases\DoorGroup\GetDoorGroups\GetDoorGroupsUseCaseServiceProvider;
use Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupUseCaseServiceProvider;
use Source\UseCases\Doors\GenerateDoorToken\GenerateDoorTokenUseCaseServiceProvider;
use Source\UseCases\GroupUser\RemoveUserFromGroup\RemoveUserFromGroupUseCaseServiceProvider;
......@@ -77,6 +78,7 @@ class AppServiceProvider extends ServiceProvider
RemoveUserFromGroupUseCaseServiceProvider::class,
// DoorGroup
GetDoorGroupsUseCaseServiceProvider::class,
GetGroupDoorsUseCaseServiceProvider::class,
// Doors
......
......@@ -59,7 +59,7 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::post('/{doorId}/regenerate-token', [DoorsController::class, 'regenerateToken']);
Route::delete('{doorId}', [DoorsController::class, 'delete']);
// Route::get('{doorId}/groups', [DoorsController::class, 'getGroupsForDoor']);
Route::get('{doorId}/groups', [DoorsController::class, 'getGroupsForDoor']);
// Route::post('{doorId}/group/{groupId}', [DoorsController::class, 'addDoorToGroup']);
// Route::delete('{doorId}/group/{groupId}', [DoorsController::class, 'removeDoorFromGroup']);
});
......
<?php
namespace Source\UseCases\DoorGroup\GetDoorGroups;
use Source\Entities\Group;
use Source\UseCases\BasePresenter;
class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['groups'] = array_map(static function (Group $group) {
return [
'id' => $group->getId(),
'title' => $group->getTitle(),
'description' => $group->getDescription(),
];
}, $responseModel->getGroups());
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
<?php
namespace Source\UseCases\DoorGroup\GetDoorGroups;
use Source\Gateways\DoorGroup\DoorGroupRepository;
class GetDoorGroups implements GetDoorGroupsUseCase
{
/**
* @var \Source\Gateways\DoorGroup\DoorGroupRepository
*/
protected DoorGroupRepository $repository;
/**
* @param \Source\Gateways\DoorGroup\DoorGroupRepository $repository
*/
public function __construct(DoorGroupRepository $repository)
{
$this->repository = $repository;
}
/**
* @inheritDoc
*/
public function getGroupsForDoor(string $doorId, Presenter $presenter): void
{
$groups = $this->repository->getGroupsForDoor($doorId);
$response = new ResponseModel($groups);
$presenter->present($response);
}
}
<?php
namespace Source\UseCases\DoorGroup\GetDoorGroups;
interface GetDoorGroupsUseCase
{
/**
* @param string $doorId
* @param \Source\UseCases\DoorGroup\GetDoorGroups\Presenter $presenter
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function getGroupsForDoor(string $doorId, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\DoorGroup\GetDoorGroups;
use Illuminate\Contracts\Foundation\Application;
use Source\Gateways\DoorGroup\DoorGroupRepository;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
/**
* Service provider must be registered in AppServiceProvider
*/
class GetDoorGroupsUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(GetDoorGroupsUseCase::class, static function (Application $app) {
return new GetDoorGroups($app->make(DoorGroupRepository::class));
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [GetDoorGroupsUseCase::class];
}
}
<?php
namespace Source\UseCases\DoorGroup\GetDoorGroups;
interface Presenter
{
/**
* @param ResponseModel $responseModel
* @return void
*/
public function present(ResponseModel $responseModel): void;
/**
* @return array
*/
public function getViewModel(): array;
}
<?php
namespace Source\UseCases\DoorGroup\GetDoorGroups;
class ResponseModel
{
/**
* @var \Source\Entities\Group[]
*/
protected array $groups;
/**
* @param \Source\Entities\Group[] $groups
*/
public function __construct(array $groups)
{
$this->groups = $groups;
}
/**
* @return \Source\Entities\Group[]
*/
public function getGroups(): array
{
return $this->groups;
}
}
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