Commit 7e6efe87 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Get rid of attempt count for door as it won't be needed with pagination

parent 3c57ee43
......@@ -7,37 +7,10 @@ use Source\Authorization\Permissions;
use Source\UseCases\Attempts\GetAttempts\GetAttemptsUseCase;
use Source\UseCases\Attempts\GetAttemptsForDoor\GetAttemptsForDoorUseCase;
use Source\UseCases\Attempts\GetAttempts\APIPresenter as GetAttemptsAPIPresenter;
use Source\UseCases\Attempts\GetAttemptCountForDoor\GetAttemptCountForDoorUseCase;
use Source\UseCases\Attempts\GetAttemptCountForDoor\APIPresenter as AttemptCountAPIPresenter;
use Source\UseCases\Attempts\GetAttemptsForDoor\APIPresenter as GetAttemptsForDoorAPIPresenter;
class AttemptsController extends ApiController
{
/**
* @param \Source\UseCases\Attempts\GetAttemptCountForDoor\GetAttemptCountForDoorUseCase $count
* @param string $doorId
* @return \Illuminate\Http\JsonResponse
* @throws \Illuminate\Validation\ValidationException
* @throws \Source\Exceptions\AuthorizationException
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Exception
*/
public function count(GetAttemptCountForDoorUseCase $count, string $doorId): JsonResponse
{
$this->authorizer->protect(Permissions::LOGS_READ);
$this->validate($this->request, [
'start' => 'required|date|before:end',
'end' => 'required|date|after:start',
]);
$presenter = new AttemptCountAPIPresenter();
$count->count($doorId, $this->request->input('start'), $this->request->input('end'), $presenter);
return $this->respondWithData($presenter->getViewModel());
}
/**
* @param \Source\UseCases\Attempts\GetAttempts\GetAttemptsUseCase $attempts
* @return \Illuminate\Http\JsonResponse
......
......@@ -69,7 +69,6 @@ use Source\UseCases\GroupSchedule\SchedulesForGroup\SchedulesForGroupUseCaseServ
use Source\UseCases\GroupUser\RemoveUserFromGroup\RemoveUserFromGroupUseCaseServiceProvider;
use Source\UseCases\Entries\GetEntriesForDateRange\GetEntriesForDateRangeUseCaseServiceProvider;
use Source\UseCases\Overrides\OverridesForDateRange\OverridesForDateRangeUseCaseServiceProvider;
use Source\UseCases\Attempts\GetAttemptCountForDoor\GetAttemptCountForDoorUseCaseServiceProvider;
use Source\UseCases\Entries\GetEntriesForDoorAndUser\GetEntriesForDoorAndUserUseCaseServiceProvider;
use Source\UseCases\GroupSchedule\ActiveSchedulesForGroup\ActiveSchedulesForGroupUseCaseServiceProvider;
use Source\UseCases\Door\Authenticate\AuthenticateUseCaseServiceProvider as DoorAuthenticateUseCaseServiceProvider;
......@@ -162,7 +161,6 @@ class AppServiceProvider extends ServiceProvider
// Attempts
GetAttemptsUseCaseServiceProvider::class,
GetAttemptsForDoorUseCaseServiceProvider::class,
GetAttemptCountForDoorUseCaseServiceProvider::class,
// Entries
GetEntriesForDoorUseCaseServiceProvider::class,
......
......@@ -73,7 +73,6 @@ Route::group(['middleware' => 'auth:api'], static function () {
Route::get('{doorId}/entries', [EntriesController::class, 'doorEntries']);
Route::get('{doorId}/attempts', [AttemptsController::class, 'doorAttempts']);
Route::get('{doorId}/attempts/count', [AttemptsController::class, 'count']);
Route::get('{doorId}/groups', [DoorsController::class, 'getGroupsForDoor']);
Route::post('{doorId}/group/{groupId}', [DoorsController::class, 'addDoorToGroup']);
......
......@@ -28,12 +28,4 @@ interface AttemptsRepository
* @return \Source\Entities\Attempt[]
*/
public function getForDoorBetween(string $doorId, Carbon $begin, Carbon $end): array;
/**
* @param string $doorId
* @param \Carbon\Carbon $begin
* @param \Carbon\Carbon $end
* @return int
*/
public function getCountForDoorBetween(string $doorId, Carbon $begin, Carbon $end): int;
}
......@@ -52,16 +52,6 @@ class DatabaseAttemptsRepository implements AttemptsRepository
}, $attempts);
}
/**
* @inheritDoc
*/
public function getCountForDoorBetween(string $doorId, Carbon $begin, Carbon $end): int
{
return \App\Attempt::query()->where('door_id', $this->castToInt($doorId))
->whereBetween('created_at', [$begin, $end])
->count();
}
/**
* @inheritDoc
*/
......
......@@ -50,14 +50,4 @@ class InMemoryAttemptsRepository implements AttemptsRepository
return $attempt->isBetween($begin, $end) && $attempt->hasDoorIdOf($doorId);
}));
}
/**
* @inheritDoc
*/
public function getCountForDoorBetween(string $doorId, Carbon $begin, Carbon $end): int
{
return count(array_filter($this->attempts, static function (Attempt $attempt) use ($doorId, $begin, $end) {
return $attempt->isBetween($begin, $end) && $attempt->hasDoorIdOf($doorId);
}));
}
}
<?php
namespace Source\UseCases\Attempts\GetAttemptCountForDoor;
use Source\UseCases\BasePresenter;
class APIPresenter extends BasePresenter implements Presenter
{
protected array $viewModel = [];
/** @inheritDoc */
public function present(ResponseModel $responseModel): void
{
$this->viewModel['count'] = $responseModel->getCount();
}
/** @inheritDoc */
public function getViewModel(): array
{
return $this->viewModel;
}
}
<?php
namespace Source\UseCases\Attempts\GetAttemptCountForDoor;
use Carbon\Carbon;
use Source\Gateways\Doors\DoorsRepository;
use Source\Exceptions\EntityNotFoundException;
use Source\Gateways\Attempts\AttemptsRepository;
class GetAttemptCountForDoor implements GetAttemptCountForDoorUseCase
{
/**
* @var \Source\Gateways\Attempts\AttemptsRepository
*/
protected AttemptsRepository $attempts;
/**
* @var \Source\Gateways\Doors\DoorsRepository
*/
protected DoorsRepository $doors;
public function __construct(AttemptsRepository $attempts, DoorsRepository $doors)
{
$this->attempts = $attempts;
$this->doors = $doors;
}
/**
* @inheritDoc
*/
public function count(string $doorId, string $begin, string $end, Presenter $presenter): void
{
if (!$this->doors->exists($doorId)) {
throw new EntityNotFoundException();
}
$count = $this->attempts->getCountForDoorBetween($doorId, new Carbon($begin), new Carbon($end));
$responseModel = new ResponseModel($count);
$presenter->present($responseModel);
}
}
<?php
namespace Source\UseCases\Attempts\GetAttemptCountForDoor;
interface GetAttemptCountForDoorUseCase
{
/**
* Begin and end must be parsable strings by datetime
*
* @param string $doorId
* @param string $begin
* @param string $end
* @param \Source\UseCases\Attempts\GetAttemptCountForDoor\Presenter $presenter
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Exception
*/
public function count(string $doorId, string $begin, string $end, Presenter $presenter): void;
}
<?php
namespace Source\UseCases\Attempts\GetAttemptCountForDoor;
use Illuminate\Support\ServiceProvider;
use Source\Gateways\Doors\DoorsRepository;
use Illuminate\Contracts\Foundation\Application;
use Source\Gateways\Attempts\AttemptsRepository;
use Illuminate\Contracts\Support\DeferrableProvider;
/**
* Service provider must be registered in AppServiceProvider
*/
class GetAttemptCountForDoorUseCaseServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(GetAttemptCountForDoorUseCase::class, static function (Application $app) {
return new GetAttemptCountForDoor($app->make(AttemptsRepository::class), $app->make(DoorsRepository::class));
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
}
/**
* @return array
*/
public function provides()
{
return [GetAttemptCountForDoorUseCase::class];
}
}
<?php
namespace Source\UseCases\Attempts\GetAttemptCountForDoor;
interface Presenter
{
/**
* @param ResponseModel $responseModel
* @return void
*/
public function present(ResponseModel $responseModel): void;
/**
* @return array
*/
public function getViewModel(): array;
}
<?php
namespace Source\UseCases\Attempts\GetAttemptCountForDoor;
class ResponseModel
{
/**
* @var int
*/
protected int $count;
public function __construct(int $count)
{
$this->count = $count;
}
/**
* @return int
*/
public function getCount(): int
{
return $this->count;
}
}
......@@ -75,26 +75,4 @@ class AttemptDatabaseTest extends DatabaseTestCase
$attempts = $this->repository->getForDoorBetween($d1->getId(), Carbon::now()->subMinute(), Carbon::now()->addDay());
$this->assertCount(1, $attempts);
}
/**
* @test
* @throws \Source\Exceptions\EntityExistsException
*/
public function it_gets_number_of_attempts_for_a_door(): void
{
$d1 = $this->createDoor('d1');
$d2 = $this->createDoor('d2');
$a = new \App\Attempt();
$a->setAttribute('door_id', $d1->getId());
$a->setCreatedAt(Carbon::now()->subDay());
$a->save();
for ($i = 0; $i < 3; $i++) {
$this->repository->add(new Attempt(0, $d1->getId()));
$this->repository->add(new Attempt(0, $d2->getId()));
}
$this->assertEquals(3, $this->repository->getCountForDoorBetween($d1
->getId(), Carbon::now()->subMinute(), Carbon::now()->addMinute()));
}
}
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