Commit a5126fcf authored by Jacob Priddy's avatar Jacob Priddy 👌

Refactor door group adding to not be url based

parent d558671e
......@@ -239,55 +239,75 @@ class DoorsController extends ApiController
}
/**
* Add Door to Group
* Add Doors to Groups
*
* This endpoint attaches a door to a group.
* This endpoint attaches a list of doors to a list of groups by ids.
*
* @authenticated
* @urlParam doorId required the door ID to attach. Example: 2
* @urlParam groupId required the group ID to attach to. Example: 8
* @bodyParam doorIds[0] string[] required The list of door Ids to attach to. Example: 2
* @bodyParam doorIds[1] string[] required The list of door Ids to attach to. Example: 1
* @bodyParam groupIds[0] string[] required The list of group Ids to attach to. Example: 8
* @bodyParam groupIds[1] string[] required The list of group Ids to attach to. Example: 7
*
* @param \Source\UseCases\DoorGroup\AddDoorToGroup\AddDoorToGroupUseCase $addDoorToGroup
* @param string $doorId
* @param string $groupId
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function addDoorToGroup(AddDoorToGroupUseCase $addDoorToGroup, string $doorId, string $groupId): JsonResponse
public function addDoorsToGroups(AddDoorToGroupUseCase $addDoorToGroup): JsonResponse
{
$this->authorizer->protect(Permissions::MANAGE_DOORS);
$this->authorizer->protectOne([Permissions::MANAGE_DOORS, Permissions::MANAGE_GROUPS]);
$this->validate($this->request, [
'groupIds' => 'array|required',
'doorIds' => 'array|required',
]);
$presenter = new AddDoorToGroupAPIPresenter();
$addDoorToGroup->addDoorToGroup($doorId, $groupId, $presenter);
$addDoorToGroup->addDoorToGroup(
$this->request->input('doorIds'),
$this->request->input('groupIds'),
$presenter
);
return $this->respondWithData($presenter->getViewModel());
}
/**
* Remove Door from Group
* Remove Doors from Groups
*
* This endpoint removes a door from a group.
* This endpoint removes a list of doors from a list of groups by ids.
*
* @authenticated
* @urlParam doorId required the door to detach. Example: 2
* @urlParam groupId required the group to detach from. Example: 8
* @bodyParam doorIds[0] string[] required The list of door Ids to attach to. Example: 2
* @bodyParam doorIds[1] string[] required The list of door Ids to attach to. Example: 1
* @bodyParam groupIds[0] string[] required The list of group Ids to attach to. Example: 8
* @bodyParam groupIds[1] string[] required The list of group Ids to attach to. Example: 7
*
* @param \Source\UseCases\DoorGroup\RemoveDoorFromGroup\RemoveDoorFromGroupUseCase $removeDoorFromGroup
* @param string $doorId
* @param string $groupId
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function removeDoorFromGroup(RemoveDoorFromGroupUseCase $removeDoorFromGroup, string $doorId, string $groupId): JsonResponse
public function removeDoorsFromGroups(RemoveDoorFromGroupUseCase $removeDoorFromGroup): JsonResponse
{
$this->authorizer->protect(Permissions::MANAGE_DOORS);
$this->authorizer->protectOne([Permissions::MANAGE_DOORS, Permissions::MANAGE_GROUPS]);
$this->validate($this->request, [
'groupIds' => 'array|required',
'doorIds' => 'array|required',
]);
$presenter = new RemoveDoorFromGroupAPIPresenter();
$removeDoorFromGroup->removeDoorFromGroup($doorId, $groupId, $presenter);
$removeDoorFromGroup->removeDoorFromGroup(
$this->request->input('doorIds'),
$this->request->input('groupIds'),
$presenter
);
return $this->respondWithData($presenter->getViewModel());
}
......
......@@ -72,8 +72,13 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::delete('{doorId}', [DoorsController::class, 'delete'])->name('doors.delete');
Route::get('{doorId}/groups', [DoorsController::class, 'getGroupsForDoor'])->name('doors.groups');
Route::post('{doorId}/group/{groupId}', [DoorsController::class, 'addDoorToGroup']);
Route::delete('{doorId}/group/{groupId}', [DoorsController::class, 'removeDoorFromGroup']);
});
Route::group([
'prefix' => 'group/door',
], static function () {
Route::post('/attach', [DoorsController::class, 'addDoorsToGroups'])->name('group-door.attach');
Route::post('/detach', [DoorsController::class, 'removeDoorsFromGroups'])->name('group-door.detach');
});
Route::group([
......
......@@ -3,6 +3,7 @@
namespace Source\UseCases\DoorGroup\AddDoorToGroup;
use Source\UseCases\BasePresenter;
use Source\UseCases\DoorGroup\ResponseModel;
class APIPresenter extends BasePresenter implements Presenter
{
......@@ -11,7 +12,7 @@ class APIPresenter extends BasePresenter implements Presenter
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['message'] = $responseModel->getMessage();
$this->viewModel['messages'] = $responseModel->getMessages();
}
/** @inheritDoc */
......
......@@ -2,6 +2,8 @@
namespace Source\UseCases\DoorGroup\AddDoorToGroup;
use Source\UseCases\DoorGroup\ResponseModel;
use Source\Exceptions\EntityNotFoundException;
use Source\Gateways\DoorGroup\DoorGroupRepository;
class AddDoorToGroup implements AddDoorToGroupUseCase
......@@ -19,11 +21,20 @@ class AddDoorToGroup implements AddDoorToGroupUseCase
/**
* @inheritDoc
*/
public function addDoorToGroup(string $doorId, string $groupId, Presenter $presenter): void
public function addDoorToGroup(array $doorIds, array $groupIds, Presenter $presenter): void
{
$this->repository->addDoorToGroup($doorId, $groupId);
$response = new ResponseModel();
$response = new ResponseModel('Success');
foreach ($groupIds as $groupId) {
foreach ($doorIds as $doorId) {
try {
$this->repository->addDoorToGroup($doorId, $groupId);
$response->addMessage("Attached door $doorId to group $groupId");
} catch (EntityNotFoundException $e) {
$response->addMessage("Failed to attach $doorId to $groupId. " . $e->getMessage());
}
}
}
$presenter->present($response);
}
......
......@@ -6,10 +6,9 @@ namespace Source\UseCases\DoorGroup\AddDoorToGroup;
interface AddDoorToGroupUseCase
{
/**
* @param string $doorId
* @param string $groupId
* @param array $doorIds
* @param array $groupIds
* @param \Source\UseCases\DoorGroup\AddDoorToGroup\Presenter $presenter
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function addDoorToGroup(string $doorId, string $groupId, Presenter $presenter): void;
public function addDoorToGroup(array $doorIds, array $groupIds, Presenter $presenter): void;
}
......@@ -2,6 +2,8 @@
namespace Source\UseCases\DoorGroup\AddDoorToGroup;
use Source\UseCases\DoorGroup\ResponseModel;
interface Presenter
{
/**
......
<?php
namespace Source\UseCases\DoorGroup\AddDoorToGroup;
class ResponseModel
{
/**
* @var string
*/
protected string $message;
public function __construct(string $message)
{
$this->message = $message;
}
/**
* @return string
*/
public function getMessage(): string
{
return $this->message;
}
}
......@@ -3,6 +3,7 @@
namespace Source\UseCases\DoorGroup\RemoveDoorFromGroup;
use Source\UseCases\BasePresenter;
use Source\UseCases\DoorGroup\ResponseModel;
class APIPresenter extends BasePresenter implements Presenter
{
......@@ -11,7 +12,7 @@ class APIPresenter extends BasePresenter implements Presenter
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['message'] = $responseModel->getMessage();
$this->viewModel['messages'] = $responseModel->getMessages();
}
/** @inheritDoc */
......
......@@ -2,6 +2,8 @@
namespace Source\UseCases\DoorGroup\RemoveDoorFromGroup;
use Source\UseCases\DoorGroup\ResponseModel;
interface Presenter
{
/**
......
......@@ -2,6 +2,8 @@
namespace Source\UseCases\DoorGroup\RemoveDoorFromGroup;
use Source\UseCases\DoorGroup\ResponseModel;
use Source\Exceptions\EntityNotFoundException;
use Source\Gateways\DoorGroup\DoorGroupRepository;
class RemoveDoorFromGroup implements RemoveDoorFromGroupUseCase
......@@ -19,11 +21,20 @@ class RemoveDoorFromGroup implements RemoveDoorFromGroupUseCase
/**
* @inheritDoc
*/
public function removeDoorFromGroup(string $doorId, string $groupId, Presenter $presenter): void
public function removeDoorFromGroup(array $doorIds, array $groupIds, Presenter $presenter): void
{
$this->repository->removeDoorFromGroup($doorId, $groupId);
$response = new ResponseModel();
$response = new ResponseModel('Success');
foreach ($groupIds as $groupId) {
foreach ($doorIds as $doorId) {
try {
$this->repository->removeDoorFromGroup($doorId, $groupId);
$response->addMessage("Successfully added door $doorId to group $groupId");
} catch (EntityNotFoundException $e) {
$response->addMessage("Could not add door $doorId to group $groupId. " . $e->getMessage());
}
}
}
$presenter->present($response);
}
......
......@@ -6,10 +6,9 @@ namespace Source\UseCases\DoorGroup\RemoveDoorFromGroup;
interface RemoveDoorFromGroupUseCase
{
/**
* @param string $doorId
* @param string $groupId
* @param string[] $doorIds
* @param string[] $groupIds
* @param \Source\UseCases\DoorGroup\RemoveDoorFromGroup\Presenter $presenter
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function removeDoorFromGroup(string $doorId, string $groupId, Presenter $presenter): void;
public function removeDoorFromGroup(array $doorIds, array $groupIds, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\DoorGroup\RemoveDoorFromGroup;
class ResponseModel
{
/**
* @var string
*/
protected string $message;
public function __construct(string $message)
{
$this->message = $message;
}
/**
* @return string
*/
public function getMessage(): string
{
return $this->message;
}
}
<?php
namespace Source\UseCases\DoorGroup;
class ResponseModel
{
/**
* @var string[]
*/
protected array $messages = [];
/**
* @param string $message
*/
public function addMessage(string $message): void
{
$this->messages[] = $message;
}
/**
* @return string[]
*/
public function getMessages(): array
{
return $this->messages;
}
}
......@@ -50,7 +50,9 @@ class AddDoorToGroupApiTest extends AuthenticatesWithApplicationTestCase
protected function handleTest(string $doorId, string $groupId): void
{
$this->response = $this->postJson('api/doors/' . $doorId . '/group/' . $groupId, [
$this->response = $this->postJson('api/group/door/attach', [
'doorIds' => [$doorId],
'groupIds' => [$groupId],
'api_token' => $this->authToken,
]);
}
......
......@@ -50,7 +50,9 @@ class RemoveDoorFromGroupApiTest extends AuthenticatesWithApplicationTestCase
protected function handleTest(string $doorId, string $groupId): void
{
$this->response = $this->deleteJson('api/doors/' . $doorId . '/group/' . $groupId, [
$this->response = $this->postJson('api/group/door/detach', [
'groupIds' => [$groupId],
'doorIds' => [$doorId],
'api_token' => $this->authToken,
]);
}
......
Markdown is supported
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