Commit 039625b0 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Add door deleteion endpoint

parent b258f4e4
Pipeline #5454 failed with stages
in 2 minutes and 18 seconds
......@@ -5,6 +5,8 @@ namespace App\Http\Controllers;
use Illuminate\Http\JsonResponse;
use Source\Authorization\Permissions;
use Source\UseCases\Doors\GetDoor\GetDoorUseCase;
use Source\UseCases\Doors\DeleteDoor\APIPresenter as DeleteDoorAPIPresenter;
use Source\UseCases\Doors\DeleteDoor\DeleteDoorUseCase;
use Source\UseCases\Doors\GenerateDoorToken\GenerateDoorTokenUseCase;
use Source\UseCases\Doors\UpdateDoor\APIPresenter as UpdateDoorAPIPresenter;
use Source\UseCases\Doors\UpdateDoor\UpdateDoorUseCase;
......@@ -109,4 +111,19 @@ class DoorsController extends ApiController
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\Doors\DeleteDoor\DeleteDoorUseCase $doorDelyeeter
* @param string $doorId
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function delete(DeleteDoorUseCase $doorDelyeeter, string $doorId): JsonResponse
{
$presenter = new DeleteDoorAPIPresenter();
$doorDelyeeter->delete($doorId, $presenter);
return $this->respondWithData($presenter->getViewModel());
}
}
......@@ -19,6 +19,7 @@ use Source\UseCases\Users\DeleteUser\DeleteUserUseCaseServiceProvider;
use Source\UseCases\Users\UpdateUser\UpdateUserUseCaseServiceProvider;
use Source\UseCases\Doors\CreateDoor\CreateDoorUseCaseServiceProvider;
use Source\UseCases\Doors\UpdateDoor\UpdateDoorUseCaseServiceProvider;
use Source\UseCases\Doors\DeleteDoor\DeleteDoorUseCaseServiceProvider;
use Source\UseCases\Users\GetAllUsers\GetAllUsersUseCaseServiceProvider;
use Source\UseCases\Doors\GetAllDoors\GetAllDoorsUseCaseServiceProvider;
use Source\UseCases\Groups\CreateGroup\CreateGroupUseCaseServiceProvider;
......@@ -76,6 +77,7 @@ class AppServiceProvider extends ServiceProvider
// Doors
GetDoorUseCaseServiceProvider::class,
CreateDoorUseCaseServiceProvider::class,
DeleteDoorUseCaseServiceProvider::class,
UpdateDoorUseCaseServiceProvider::class,
GetAllDoorsUseCaseServiceProvider::class,
AuthenticateUseCaseServiceProvider::class,
......
......@@ -57,7 +57,7 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::get('{doorId}', [DoorsController::class, 'get']);
Route::put('{doorId}', [DoorsController::class, 'update']);
Route::post('/{doorId}/regenerate-token', [DoorsController::class, 'regenerateToken']);
// Route::delete('{doorId}', [DoorsController::class, 'delete']);
Route::delete('{doorId}', [DoorsController::class, 'delete']);
});
Route::group([
......
......@@ -114,4 +114,12 @@ class DatabaseDoorsRepository implements DoorsRepository
return self::makeDoorFromDb($dbDoor);
}
/**
* @inheritDoc
*/
public function delete(string $doorId): bool
{
return \App\Door::destroy((int)$doorId);
}
}
......@@ -52,4 +52,12 @@ interface DoorsRepository
* @return \Source\Entities\Door|null
*/
public function update(string $doorId, Door $door): ?Door;
/**
* Deletes a door
*
* @param string $doorId
* @return bool
*/
public function delete(string $doorId): bool;
}
......@@ -93,4 +93,16 @@ class InMemoryDoorsRepository implements DoorsRepository
return null;
}
/**
* @inheritDoc
*/
public function delete(string $doorId): bool
{
$this->doors = array_filter($this->doors, static function (Door $door) use ($doorId) {
return !$door->hasIdOf($doorId);
});
return true;
}
}
<?php
namespace Source\UseCases\Doors\DeleteDoor;
use Source\UseCases\BasePresenter;
class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['message'] = $responseModel->getMessage();
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
<?php
namespace Source\UseCases\Doors\DeleteDoor;
use Source\Gateways\Doors\DoorsRepository;
use Source\Exceptions\DeleteFailedException;
use Source\Exceptions\EntityNotFoundException;
class DeleteDoor implements DeleteDoorUseCase
{
/**
* @var \Source\Gateways\Doors\DoorsRepository
*/
protected DoorsRepository $doors;
/**
* @param \Source\Gateways\Doors\DoorsRepository $doors
*/
public function __construct(DoorsRepository $doors)
{
$this->doors = $doors;
}
/**
* @inheritDoc
*/
public function delete(string $doorId, Presenter $presenter)
{
if (!($door = $this->doors->get($doorId))) {
throw new EntityNotFoundException();
}
if (!$this->doors->delete($door->getId())) {
$text = 'Unable to delete door';
} else {
$text = 'Door deleted.';
}
$response = new ResponseModel($text);
$presenter->present($response);
}
}
<?php
namespace Source\UseCases\Doors\DeleteDoor;
interface DeleteDoorUseCase
{
/**
* Deletes a door
*
* @param string $doorId
* @param \Source\UseCases\Doors\DeleteDoor\Presenter $presenter
* @return mixed
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function delete(string $doorId, Presenter $presenter);
}
<?php
namespace Source\UseCases\Doors\DeleteDoor;
use Source\Gateways\Doors\DoorsRepository;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
/**
* Service provider must be registered in AppServiceProvider
*/
class DeleteDoorUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(DeleteDoorUseCase::class, static function (Application $app) {
return new DeleteDoor($app->make(DoorsRepository::class));
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [DeleteDoorUseCase::class];
}
}
<?php
namespace Source\UseCases\Doors\DeleteDoor;
interface Presenter
{
/**
* @param ResponseModel $responseModel
* @return void
*/
public function present(ResponseModel $responseModel): void;
/**
* @return array
*/
public function getViewModel(): array;
}
<?php
namespace Source\UseCases\Doors\DeleteDoor;
class ResponseModel
{
/**
* @var string
*/
protected string $message;
/**
* @param string $message
*/
public function __construct(string $message)
{
$this->message = $message;
}
/**
* @return string
*/
public function getMessage(): string
{
return $this->message;
}
}
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