Commit 90b482e7 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Add route to attach user to group from group view

parent 37909c56
...@@ -7,6 +7,8 @@ namespace App\Http\Controllers\Web\Admin; ...@@ -7,6 +7,8 @@ namespace App\Http\Controllers\Web\Admin;
use Illuminate\View\View; use Illuminate\View\View;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Source\UseCases\DoorGroup\AddDoorToGroup\APIPresenter as DoorToGroupPresenter;
use Source\UseCases\DoorGroup\AddDoorToGroup\AddDoorToGroupSearch;
use Source\UseCases\GroupUser\AddUserToGroup\APIPresenter as UserToGroupPresenter; use Source\UseCases\GroupUser\AddUserToGroup\APIPresenter as UserToGroupPresenter;
use Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupSearch; use Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupSearch;
use Source\UseCases\GroupSchedule\AddSchedulesToGroups\ApiPresenter as ScheduleGroupPresenter; use Source\UseCases\GroupSchedule\AddSchedulesToGroups\ApiPresenter as ScheduleGroupPresenter;
...@@ -68,9 +70,23 @@ class GroupRelationController extends Controller ...@@ -68,9 +70,23 @@ class GroupRelationController extends Controller
)); ));
} }
public function addDoor(): RedirectResponse /**
* @param string $groupId
* @param \Source\UseCases\DoorGroup\AddDoorToGroup\AddDoorToGroupSearch $addDoorToGroupSearch
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Validation\ValidationException
*/
public function addDoor(string $groupId, AddDoorToGroupSearch $addDoorToGroupSearch): RedirectResponse
{ {
$this->validate($this->request, [
'door' => 'required|string',
]);
$presenter = new DoorToGroupPresenter();
$addDoorToGroupSearch->addDoorToGroup([$this->request->input('door')], [$groupId], $presenter);
return redirect()->back()->with($presenter->getViewModel());
} }
/** /**
......
...@@ -200,4 +200,14 @@ class DatabaseDoorsRepository implements DoorsRepository ...@@ -200,4 +200,14 @@ class DatabaseDoorsRepository implements DoorsRepository
{ {
return (bool)$this->get($doorId); return (bool)$this->get($doorId);
} }
/**
* @inheritDoc
*/
public function searchByLocation(string $location): array
{
$doors = \App\Door::query()->where('location', '=', $location);
return array_map(fn (\App\Door $door): Door => self::makeDoorFromDb($door), $doors->get()->values()->all());
}
} }
...@@ -81,4 +81,12 @@ interface DoorsRepository ...@@ -81,4 +81,12 @@ interface DoorsRepository
* @return bool * @return bool
*/ */
public function exists(string $doorId): bool; public function exists(string $doorId): bool;
/**
* Finds all doors with a given location
*
* @param string $location
* @return \Source\Entities\Door[]
*/
public function searchByLocation(string $location): array;
} }
...@@ -160,4 +160,12 @@ class InMemoryDoorsRepository implements DoorsRepository ...@@ -160,4 +160,12 @@ class InMemoryDoorsRepository implements DoorsRepository
{ {
return (bool)$this->get($doorId); return (bool)$this->get($doorId);
} }
/**
* @inheritDoc
*/
public function searchByLocation(string $location): array
{
return array_values(array_filter($this->doors, fn (Door $door): bool => $door->getLocation() === $location));
}
} }
<?php
namespace Source\UseCases\DoorGroup\AddDoorToGroup;
use Source\Gateways\Doors\DoorsRepository;
use Source\UseCases\DoorGroup\ResponseModel;
use Source\Exceptions\EntityNotFoundException;
use Source\Gateways\DoorGroup\DoorGroupRepository;
class AddDoorToGroupSearch implements AddDoorToGroupUseCase
{
/**
* @var \Source\Gateways\DoorGroup\DoorGroupRepository
*/
protected DoorGroupRepository $repository;
/**
* @var \Source\Gateways\Doors\DoorsRepository
*/
protected DoorsRepository $doors;
public function __construct(DoorGroupRepository $repository, DoorsRepository $doors)
{
$this->repository = $repository;
$this->doors = $doors;
}
/**
* @inheritDoc
*/
public function addDoorToGroup(array $doorIds, array $groupIds, Presenter $presenter): void
{
$response = new ResponseModel();
foreach ($groupIds as $groupId) {
/**
* Can be either name, location or ID
* We will search in that order.
*/
foreach ($doorIds as $query) {
try {
$door = $this->doors->findByName($query);
if (!$door) {
$locationDoors = $this->doors->searchByLocation($query);
if (count($locationDoors) === 1) {
$door = $locationDoors[0];
}
}
$id = $door ? $door->getId() : $query;
$this->repository->addDoorToGroup($id, $groupId);
$response->addMessage("Attached door $query to group $groupId");
} catch (EntityNotFoundException $e) {
$response->addError("Failed to attach door $query to group $groupId. " . $e->getMessage());
}
}
}
$presenter->present($response);
}
}
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