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

Add routes for the override feature

parent 97de72c2
Pipeline #8840 canceled with stages
in 46 seconds
<?php
namespace App\Http\Controllers;
use App\Guards\ApiGuard;
use Source\Entities\Override;
use Illuminate\Http\JsonResponse;
use Source\Authorization\Permissions;
use Source\UseCases\Overrides\ApiPresenter;
use Source\UseCases\Overrides\OverrideCreate\OverrideCreateUseCase;
use Source\UseCases\Overrides\OverridesForDoor\OverridesForDoorUseCase;
use Source\UseCases\Overrides\OverridesForDateRange\OverridesForDateRangeUseCase;
use Source\UseCases\Overrides\OverrideCreate\ApiPresenter as OverrideCreateApiPresenter;
class OverridesController extends ApiController
{
/**
* @param \Source\UseCases\Overrides\OverrideCreate\OverrideCreateUseCase $overrideCreate
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Exception
*/
public function create(OverrideCreateUseCase $overrideCreate, ApiGuard $apiGuard): JsonResponse
{
$this->authorizer->protect(Permissions::MANAGE_DOORS);
$this->validate($this->request, [
'reason' => 'required|string|max:1024',
'door_id' => 'required|integer',
'type' => 'required|integer|between:' . Override::TYPE_OPEN . ',' . Override::TYPE_NORMAL,
'start' => 'required|date|before:end',
'end' => 'required|date|after:start',
]);
$presenter = new OverrideCreateApiPresenter();
$attributes = $this->request->all();
$attributes['user_id'] = $apiGuard->id();
$overrideCreate->create($attributes, $presenter);
$this->setStatusCode($presenter->getStatusCode());
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param string $doorId
* @param \Source\UseCases\Overrides\OverridesForDoor\OverridesForDoorUseCase $overridesForDoor
* @return \Illuminate\Http\JsonResponse
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function forDoor(string $doorId, OverridesForDoorUseCase $overridesForDoor): JsonResponse
{
$this->authorizer->protect(Permissions::LOGS_READ);
$presenter = new ApiPresenter();
$overridesForDoor->getOverridesForDoor($doorId, $presenter);
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\Overrides\OverridesForDateRange\OverridesForDateRangeUseCase $overridesInDateRange
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Exception
*/
public function index(OverridesForDateRangeUseCase $overridesInDateRange): JsonResponse
{
$this->authorizer->protect(Permissions::LOGS_READ);
$this->validate($this->request, [
'start' => 'required|date|before:end',
'end' => 'required|date|after:start',
]);
$presenter = new ApiPresenter();
$overridesInDateRange->getOverridesForDateRange(
$this->request->input('start'),
$this->request->input('end'),
$presenter
);
return $this->respondWithData($presenter->getViewModel());
}
}
......@@ -10,6 +10,7 @@ use App\Http\Controllers\TokensController;
use App\Http\Controllers\EntriesController;
use App\Http\Controllers\AttemptsController;
use App\Http\Controllers\SchedulesController;
use App\Http\Controllers\OverridesController;
/*
|--------------------------------------------------------------------------
......@@ -76,6 +77,8 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::get('{doorId}/groups', [DoorsController::class, 'getGroupsForDoor']);
Route::post('{doorId}/group/{groupId}', [DoorsController::class, 'addDoorToGroup']);
Route::delete('{doorId}/group/{groupId}', [DoorsController::class, 'removeDoorFromGroup']);
Route::get('{doorId}/overrides', [OverridesController::class, 'forDoor']);
});
Route::group([
......@@ -110,6 +113,14 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::get('/', [AttemptsController::class, 'index']);
});
Route::group([
'prefix' => 'overrides',
], static function () {
Route::post('/', [OverridesController::class, 'create']);
Route::get('/', [OverridesController::class, 'index']);
});
Route::group([
'prefix' => 'me',
], static function () {
......
......@@ -8,7 +8,7 @@ use Source\UseCases\Overrides\Presenter;
interface OverridesForDoorUseCase
{
/**
* Gets all overrides that ever occured on a door
* Gets all overrides that ever occurred on a door
*
* @param string $doorId
* @param \Source\UseCases\Overrides\Presenter $presenter
......
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