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

Make some put routs that are supposed to be patch, patch,

Also Add override get and update endpoints
parent 45f1ce0f
Pipeline #8957 passed with stages
in 2 minutes and 55 seconds
......@@ -8,7 +8,9 @@ use Source\Entities\Override;
use Illuminate\Http\JsonResponse;
use Source\Authorization\Permissions;
use Source\UseCases\Overrides\ApiPresenter;
use Source\UseCases\Overrides\OverrideGet\OverrideGetUseCase;
use Source\UseCases\Overrides\OverrideCreate\OverrideCreateUseCase;
use Source\UseCases\Overrides\OverrideUpdate\OverrideUpdateUseCase;
use Source\UseCases\Overrides\OverridesForDoor\OverridesForDoorUseCase;
use Source\UseCases\Overrides\OverridesForDateRange\OverridesForDateRangeUseCase;
use Source\UseCases\Overrides\OverrideCreate\ApiPresenter as OverrideCreateApiPresenter;
......@@ -22,6 +24,7 @@ class OverridesController extends ApiController
* @throws \Illuminate\Validation\ValidationException
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Exception
*/
public function create(OverrideCreateUseCase $overrideCreate, ApiGuard $apiGuard): JsonResponse
{
......@@ -42,7 +45,51 @@ class OverridesController extends ApiController
$overrideCreate->create($attributes, $presenter);
$this->setStatusCode($presenter->getStatusCode());
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param string $overrideId
* @param \Source\UseCases\Overrides\OverrideUpdate\OverrideUpdateUseCase $overrideUpdate
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Exception
*/
public function update(string $overrideId, OverrideUpdateUseCase $overrideUpdate): JsonResponse
{
$this->authorizer->protect(Permissions::MANAGE_DOORS);
$this->validate($this->request, [
'reason' => 'string|max:1024',
'start' => 'date|required_if:end|before:end',
'end' => 'date|required_if:start|after:start',
]);
$presenter = new OverrideCreateApiPresenter();
$overrideUpdate->update($overrideId, $this->request->all(), $presenter);
$this->setStatusCode($presenter->getStatusCode());
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param string $overrideId
* @param \Source\UseCases\Overrides\OverrideGet\OverrideGetUseCase $overrideGet
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function get(string $overrideId, OverrideGetUseCase $overrideGet): JsonResponse
{
$this->authorizer->protect(Permissions::LOGS_READ);
$presenter = new OverrideCreateApiPresenter();
$overrideGet->get($overrideId, $presenter);
$this->setStatusCode($presenter->getStatusCode());
return $this->respondWithData($presenter->getViewModel());
}
......
......@@ -41,6 +41,7 @@ use Source\UseCases\Tokens\UpdateToken\UpdateTokenUseCaseServiceProvider;
use Source\UseCases\Attempts\GetAttempts\GetAttemptsUseCaseServiceProvider;
use Source\UseCases\Groups\GetAllGroups\GetAllGroupsUseCaseServiceProvider;
use Source\UseCases\Tokens\GetAllTokens\GetAllTokensUseCaseServiceProvider;
use Source\UseCases\Overrides\OverrideGet\OverrideGetUseCaseServiceProvider;
use Source\UseCases\Schedules\ScheduleGet\ScheduleGetUseCaseServiceProvider;
use Source\UseCases\Door\StatusResponse\StatusResponseUseCaseServiceProvider;
use Source\UseCases\Schedules\SchedulesGet\SchedulesGetUseCaseServiceProvider;
......@@ -52,6 +53,7 @@ use Source\UseCases\TokenUser\GetUserTokens\GetUserTokensUseCaseServiceProvider;
use Source\UseCases\DoorGroup\AddDoorToGroup\AddDoorToGroupUseCaseServiceProvider;
use Source\UseCases\GroupUser\AddUserToGroup\AddUserToGroupUseCaseServiceProvider;
use Source\UseCases\Overrides\OverrideCreate\OverrideCreateUseCaseServiceProvider;
use Source\UseCases\Overrides\OverrideUpdate\OverrideUpdateUseCaseServiceProvider;
use Source\UseCases\Schedules\ScheduleCreate\ScheduleCreateUseCaseServiceProvider;
use Source\UseCases\Schedules\ScheduleUpdate\ScheduleUpdateUseCaseServiceProvider;
use Source\UseCases\Doors\GenerateDoorToken\GenerateDoorTokenUseCaseServiceProvider;
......@@ -172,7 +174,9 @@ class AppServiceProvider extends ServiceProvider
DoorUserGroupMapUseCaseServiceProvider::class,
// Overrides
OverrideGetUseCaseServiceProvider::class,
OverrideCreateUseCaseServiceProvider::class,
OverrideUpdateUseCaseServiceProvider::class,
OverridesForDoorUseCaseServiceProvider::class,
OverridesForDateRangeUseCaseServiceProvider::class,
];
......
......@@ -66,7 +66,7 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::get('/', [DoorsController::class, 'index']);
Route::post('/', [DoorsController::class, 'store']);
Route::get('{doorId}', [DoorsController::class, 'get']);
Route::put('{doorId}', [DoorsController::class, 'update']);
Route::patch('{doorId}', [DoorsController::class, 'update']);
Route::post('/{doorId}/regenerate-token', [DoorsController::class, 'regenerateToken']);
Route::delete('{doorId}', [DoorsController::class, 'delete']);
......@@ -96,7 +96,7 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::get('/', [TokensController::class, 'index']);
Route::get('{tokenId}', [TokensController::class, 'get']);
Route::post('/', [TokensController::class, 'store']);
Route::put('{tokenId}', [TokensController::class, 'update']);
Route::patch('{tokenId}', [TokensController::class, 'update']);
Route::post('{tokenId}/expire', [TokensController::class, 'expire']);
});
......@@ -118,6 +118,8 @@ Route::group(['middleware' => 'auth:api'], static function () {
], static function () {
Route::post('/', [OverridesController::class, 'create']);
Route::get('/', [OverridesController::class, 'index']);
Route::patch('{overrideId}', [OverridesController::class, 'update']);
Route::get('{overrideId}', [OverridesController::class, 'get']);
});
......
......@@ -176,6 +176,6 @@ class Override
*/
public function hasIdOf(string $id): bool
{
return $this->getId() === $id;
return $this->getId() === (int)$id;
}
}
......@@ -161,6 +161,9 @@ QUERY;
throw new EntityNotFoundException('Override with id "' . $overrideId . '" does not exist.');
}
$o->setAttribute('user_id', $override->getUserId());
$o->setAttribute('door_id', $override->getDoorId());
$o->setAttribute('type', $override->getType());
$o->setAttribute('start', $override->getStart());
$o->setAttribute('end', $override->getEnd());
$o->setAttribute('reason', $override->getReason());
......@@ -171,4 +174,19 @@ QUERY;
return self::toOverride($o);
}
/**
* @inheritDoc
*/
public function get(string $overrideId): ?Override
{
/** @var \App\Override $o */
$o = \App\Override::query()->find($this->castToInt($overrideId));
if (!$o) {
return null;
}
return self::toOverride($o);
}
}
......@@ -70,4 +70,18 @@ class InMemoryOverridesRepository implements OverridesRepository
return $override;
}
/**
* @inheritDoc
*/
public function get(string $overrideId): ?Override
{
foreach ($this->overrides as $override) {
if ($override->hasIdOf($overrideId)) {
return $override;
}
}
return null;
}
}
......@@ -26,7 +26,7 @@ class LocalOverridesRepository extends InMemoryOverridesRepository
));
$this->addOverride(new Override(
1,
2,
'No one is allowed in the bat cave!',
LocalUsersRepository::getAdminUser()->getId(),
LocalDoorsRepository::getTheBatCave()->getId(),
......
......@@ -38,7 +38,7 @@ interface OverridesRepository
public function addOverride(Override $override): ?Override;
/**
* Will only update start, end, and reason.
* Update an override
*
* @param string $overrideId
* @param \Source\Entities\Override $override
......@@ -46,4 +46,12 @@ interface OverridesRepository
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function updateOverride(string $overrideId, Override $override): ?Override;
/**
* Retrieves an override
*
* @param string $overrideId
* @return \Source\Entities\Override|null
*/
public function get(string $overrideId): ?Override;
}
<?php
namespace Source\UseCases\Overrides\OverrideGet;
use Source\Exceptions\EntityNotFoundException;
use Source\Gateways\Overrides\OverridesRepository;
use Source\UseCases\Overrides\OverrideCreate\Presenter;
use Source\UseCases\Overrides\OverrideCreate\ResponseModel;
class OverrideGet implements OverrideGetUseCase
{
/**
* @var \Source\Gateways\Overrides\OverridesRepository
*/
protected OverridesRepository $overrides;
public function __construct(OverridesRepository $overrides)
{
$this->overrides = $overrides;
}
/**
* @inheritDoc
*/
public function get(string $overrideId, Presenter $presenter): void
{
$override = $this->overrides->get($overrideId);
if (!$override) {
throw new EntityNotFoundException('Override does not exist.');
}
$response = new ResponseModel();
$response->setOverride($override);
$presenter->present($response);
}
}
<?php
namespace Source\UseCases\Overrides\OverrideGet;
use Source\UseCases\Overrides\OverrideCreate\Presenter;
interface OverrideGetUseCase
{
/**
* Gets a specific override
*
* @param string $overrideId
* @param \Source\UseCases\Overrides\OverrideCreate\Presenter $presenter
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function get(string $overrideId, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\Overrides\OverrideGet;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
use Source\Gateways\Overrides\OverridesRepository;
use Illuminate\Contracts\Support\DeferrableProvider;
/**
* Service provider must be registered in AppServiceProvider
*/
class OverrideGetUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(OverrideGetUseCase::class, static function (Application $app) {
return new OverrideGet($app->make(OverridesRepository::class));
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [OverrideGetUseCase::class];
}
}
<?php
namespace Source\UseCases\Overrides\OverrideUpdate;
use Carbon\Carbon;
use Source\Entities\Override;
use Source\Gateways\Doors\DoorsRepository;
use Source\Gateways\Users\UsersRepository;
use Source\Exceptions\EntityNotFoundException;
use Source\Gateways\Overrides\OverridesRepository;
use Source\UseCases\Overrides\OverrideCreate\Presenter;
use Source\UseCases\Overrides\OverrideCreate\ResponseModel;
class OverrideUpdate implements OverrideUpdateUseCase
{
/**
* @var \Source\Gateways\Overrides\OverridesRepository
*/
protected OverridesRepository $overrides;
/**
* @var \Source\Gateways\Users\UsersRepository
*/
protected UsersRepository $users;
/**
* @var \Source\Gateways\Doors\DoorsRepository
*/
protected DoorsRepository $doors;
public function __construct(OverridesRepository $overrides, UsersRepository $users, DoorsRepository $doors)
{
$this->overrides = $overrides;
$this->users = $users;
$this->doors = $doors;
}
/**
* @inheritDoc
*/
public function update(string $overrideId, array $attributes, Presenter $presenter): void
{
$override = $this->overrides->get($overrideId);
if (!$override) {
throw new EntityNotFoundException('Override does not exist.');
}
$reason = $attributes['reason'] ?? $override->getReason();
$start = $attributes['start'] ?? null;
$end = $attributes['end'] ?? null;
if ($start) {
$start = new Carbon($start);
} else {
$start = $override->getStart();
}
if ($end) {
$end = new Carbon($end);
} else {
$end = $override->getEnd();
}
$override = $this->overrides->updateOverride($overrideId, new Override(
$override->getId(),
$reason,
$override->getUserId(),
$override->getDoorId(),
$override->getType(),
$start,
$end
));
$response = new ResponseModel();
if (!$override) {
$response->addError('Something went wrong when adding the override');
} else {
$response->setOverride($override);
}
if ($response->hasError()) {
$presenter->presentError($response);
} else {
$presenter->present($response);
}
}
}
<?php
namespace Source\UseCases\Overrides\OverrideUpdate;
use Source\UseCases\Overrides\OverrideCreate\Presenter;
interface OverrideUpdateUseCase
{
/**
* Optional Attributes
* - reason: string (reason for override)
* - start: string (start of override string must be parsable by DateTime)
* - end: string (end of override string must be parsable by DateTime)
* Throws an exception if the dates are not parsable
* Will use previous entries values if any attributes are omitted
*
* @param string $overrideId
* @param array $attributes
* @param \Source\UseCases\Overrides\OverrideCreate\Presenter $presenter
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Exception
*/
public function update(string $overrideId, array $attributes, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\Overrides\OverrideUpdate;
use Illuminate\Support\ServiceProvider;
use Source\Gateways\Doors\DoorsRepository;
use Source\Gateways\Users\UsersRepository;
use Illuminate\Contracts\Foundation\Application;
use Source\Gateways\Overrides\OverridesRepository;
use Illuminate\Contracts\Support\DeferrableProvider;
/**
* Service provider must be registered in AppServiceProvider
*/
class OverrideUpdateUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(OverrideUpdateUseCase::class, static function (Application $app) {
return new OverrideUpdate(
$app->make(OverridesRepository::class),
$app->make(UsersRepository::class),
$app->make(DoorsRepository::class)
);
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [OverrideUpdateUseCase::class];
}
}
......@@ -33,9 +33,11 @@ class UpdateToken implements UpdateTokenUseCase
throw new EntityNotFoundException();
}
$date = null;
if (isset($attributes['expires_at'])) {
$date = new Carbon($attributes['expires_at']);
$expiresAt = $attributes['expires_at'] ?? null;
if ($expiresAt) {
$expiresAt = new Carbon($expiresAt);
} else {
$expiresAt = $token->getExpiresAt();
}
$token = new Token(
......@@ -43,7 +45,7 @@ class UpdateToken implements UpdateTokenUseCase
$token->getUserId(),
$token->getTokenString(),
$attributes['name'] ?? $token->getName(),
$date,
$expiresAt,
$token->getCreatedAt(),
$token->getUpdatedAt()
);
......
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