Commit 9f0cfc11 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Revert "Make attempts and entries use shallow linking by location"

This reverts commit 95157362.
parent 8b358f2e
Pipeline #12518 passed with stages
in 2 minutes and 25 seconds
...@@ -3,8 +3,17 @@ ...@@ -3,8 +3,17 @@
namespace App; namespace App;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Attempt extends Model class Attempt extends Model
{ {
protected $fillable = ['door_location']; protected $fillable = ['door_id'];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function door(): BelongsTo
{
return $this->belongsTo(Door::class);
}
} }
...@@ -20,6 +20,22 @@ class Door extends Authenticatable ...@@ -20,6 +20,22 @@ class Door extends Authenticatable
return $this->belongsToMany(Group::class); return $this->belongsToMany(Group::class);
} }
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function entries(): HasMany
{
return $this->hasMany(Entry::class);
}
/**
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function attempts(): HasMany
{
return $this->hasMany(Attempt::class);
}
/** /**
* @return \Illuminate\Database\Eloquent\Relations\HasMany * @return \Illuminate\Database\Eloquent\Relations\HasMany
*/ */
...@@ -36,6 +52,18 @@ class Door extends Authenticatable ...@@ -36,6 +52,18 @@ class Door extends Authenticatable
// Detach all groups // Detach all groups
$door->groups()->detach(); $door->groups()->detach();
// Delete all entries
/** @var \App\Entry $entry */
foreach ($door->entries() as $entry) {
$entry->delete();
}
// Delete all attempts
/** @var \App\Attempt $attempt */
foreach ($door->attempts() as $attempt) {
$attempt->delete();
}
// Delete all overrides // Delete all overrides
/** @var \App\Override $override */ /** @var \App\Override $override */
foreach ($door->overrides() as $override) { foreach ($door->overrides() as $override) {
......
...@@ -7,7 +7,15 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; ...@@ -7,7 +7,15 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Entry extends Model class Entry extends Model
{ {
protected $fillable = ['door_location', 'user_id', 'success']; protected $fillable = ['door_id', 'user_id', 'success'];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function door(): BelongsTo
{
return $this->belongsTo(Door::class);
}
/** /**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
......
...@@ -17,7 +17,7 @@ class AttemptsController extends ApiController ...@@ -17,7 +17,7 @@ class AttemptsController extends ApiController
/** /**
* Get Attempts * Get Attempts
* *
* This route filters attempts based off of starting date, ending date, or door location. * This route filters attempts based off of starting date, ending date, or door id.
* If only start is supplied, all attempts after the start date are given. If only end * If only start is supplied, all attempts after the start date are given. If only end
* is supplied, all attempts before the start date are given. If both dates are supplied, * is supplied, all attempts before the start date are given. If both dates are supplied,
* all attempts between the given dates are returned. This route is paginated. * all attempts between the given dates are returned. This route is paginated.
...@@ -26,9 +26,9 @@ class AttemptsController extends ApiController ...@@ -26,9 +26,9 @@ class AttemptsController extends ApiController
* @paginated * @paginated
* @queryParam start The beginning date to filter attempts by. Example: 2000-06-02 08:11:45 * @queryParam start The beginning date to filter attempts by. Example: 2000-06-02 08:11:45
* @queryParam end The ending date to filter attempts by. Example: 2920-06-02 08:11:45 * @queryParam end The ending date to filter attempts by. Example: 2920-06-02 08:11:45
* @queryParam door_location The door location to filter on. Example: The Amazon * @queryParam door_id The door id to filter on. Example: 1
* *
* @response 422 {"message":"The given data was invalid.","errors":{"start":["The start is not a valid date."],"end":["The end is not a valid date."],"door_location":["The door location must be a string."]}} * @response 422 {"message":"The given data was invalid.","errors":{"start":["The start is not a valid date."],"end":["The end is not a valid date."],"door_id":["The door id must be an integer."]}}
* *
* @param \Source\UseCases\Attempts\GetAttempts\GetAttemptsUseCase $attempts * @param \Source\UseCases\Attempts\GetAttempts\GetAttemptsUseCase $attempts
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
...@@ -44,7 +44,7 @@ class AttemptsController extends ApiController ...@@ -44,7 +44,7 @@ class AttemptsController extends ApiController
$this->validate($this->request, [ $this->validate($this->request, [
'start' => 'nullable|date', 'start' => 'nullable|date',
'end' => 'nullable|date', 'end' => 'nullable|date',
'door_location' => 'nullable|string', 'door_id' => 'nullable|integer',
]); ]);
$presenter = new GetAttemptsAPIPresenter(); $presenter = new GetAttemptsAPIPresenter();
...@@ -52,7 +52,7 @@ class AttemptsController extends ApiController ...@@ -52,7 +52,7 @@ class AttemptsController extends ApiController
$attempts->getBetweenDates( $attempts->getBetweenDates(
$this->request->input('start'), $this->request->input('start'),
$this->request->input('end'), $this->request->input('end'),
$this->request->input('door_location'), $this->request->input('door_id'),
$presenter $presenter
); );
......
...@@ -25,10 +25,10 @@ class EntriesController extends ApiController ...@@ -25,10 +25,10 @@ class EntriesController extends ApiController
* @paginated * @paginated
* @queryParam start The beginning date to filter entries by. Example: 2000-06-02 08:11:45 * @queryParam start The beginning date to filter entries by. Example: 2000-06-02 08:11:45
* @queryParam end The ending date to filter entries by. Example: 2920-06-02 08:11:45 * @queryParam end The ending date to filter entries by. Example: 2920-06-02 08:11:45
* @queryParam door_id The door id to filter entries on. Example: Amazon * @queryParam door_id The door id to filter entries on. Example: 1
* @queryParam user_id The user id to filter entries on. Example: 420 * @queryParam user_id The user id to filter entries on. Example: 420
* *
* @response 422 {"message":"The given data was invalid.","errors":{"start":["The start is not a valid date."],"end":["The end is not a valid date."],"door_location":["The door location must be a string."],"user_id":["The user id must be an integer."]}} * @response 422 {"message":"The given data was invalid.","errors":{"start":["The start is not a valid date."],"end":["The end is not a valid date."],"door_id":["The door id must be an integer."],"user_id":["The user id must be an integer."]}}
* *
* @param \Source\UseCases\Entries\GetEntries\GetEntriesUseCase $userEntries * @param \Source\UseCases\Entries\GetEntries\GetEntriesUseCase $userEntries
* @return \Illuminate\Http\JsonResponse * @return \Illuminate\Http\JsonResponse
...@@ -43,7 +43,7 @@ class EntriesController extends ApiController ...@@ -43,7 +43,7 @@ class EntriesController extends ApiController
$this->validate($this->request, [ $this->validate($this->request, [
'start' => 'nullable|date', 'start' => 'nullable|date',
'end' => 'nullable|date', 'end' => 'nullable|date',
'door_location' => 'nullable|string', 'door_id' => 'nullable|integer',
'user_id' => 'nullable|integer', 'user_id' => 'nullable|integer',
]); ]);
...@@ -51,7 +51,7 @@ class EntriesController extends ApiController ...@@ -51,7 +51,7 @@ class EntriesController extends ApiController
$userEntries->getEntries( $userEntries->getEntries(
$this->request->input('user_id'), $this->request->input('user_id'),
$this->request->input('door_location'), $this->request->input('door_id'),
$this->request->input('start'), $this->request->input('start'),
$this->request->input('end'), $this->request->input('end'),
$presenter $presenter
......
...@@ -14,14 +14,13 @@ class AttemptsController extends Controller ...@@ -14,14 +14,13 @@ class AttemptsController extends Controller
* @param \Source\UseCases\Attempts\GetAttempts\GetAttemptsUseCase $attempts * @param \Source\UseCases\Attempts\GetAttempts\GetAttemptsUseCase $attempts
* @return \Illuminate\View\View * @return \Illuminate\View\View
* @throws \Illuminate\Validation\ValidationException * @throws \Illuminate\Validation\ValidationException
* @throws \Exception
*/ */
public function index(GetAttemptsUseCase $attempts): View public function index(GetAttemptsUseCase $attempts): View
{ {
$this->validate($this->request, [ $this->validate($this->request, [
'start' => 'nullable|date', 'start' => 'nullable|date',
'end' => 'nullable|date', 'end' => 'nullable|date',
'door_location' => 'nullable|string', 'door_id' => 'nullable|integer',
]); ]);
$start = $this->request->input('start'); $start = $this->request->input('start');
......
...@@ -22,7 +22,7 @@ class EntriesController extends Controller ...@@ -22,7 +22,7 @@ class EntriesController extends Controller
$this->validate($this->request, [ $this->validate($this->request, [
'start' => 'nullable|date', 'start' => 'nullable|date',
'end' => 'nullable|date', 'end' => 'nullable|date',
'door_location' => 'nullable|string', 'door_id' => 'nullable|integer',
'user_id' => 'nullable|integer', 'user_id' => 'nullable|integer',
]); ]);
...@@ -38,7 +38,7 @@ class EntriesController extends Controller ...@@ -38,7 +38,7 @@ class EntriesController extends Controller
$entries->getEntries( $entries->getEntries(
$this->request->input('user_id'), $this->request->input('user_id'),
$this->request->input('door_location'), $this->request->input('door_id'),
$start, $start,
$end, $end,
$presenter $presenter
......
...@@ -17,8 +17,9 @@ class CreateEntriesTable extends Migration ...@@ -17,8 +17,9 @@ class CreateEntriesTable extends Migration
Schema::create('entries', static function (Blueprint $table) { Schema::create('entries', static function (Blueprint $table) {
$table->id(); $table->id();
$table->unsignedBigInteger('user_id'); $table->unsignedBigInteger('user_id');
$table->string('door_location'); $table->unsignedBigInteger('door_id');
$table->boolean('success'); $table->boolean('success');
$table->foreign('door_id')->references('id')->on('doors');
$table->foreign('user_id')->references('id')->on('users'); $table->foreign('user_id')->references('id')->on('users');
$table->timestamps(); $table->timestamps();
}); });
......
...@@ -15,7 +15,8 @@ class CreateAttemptsTable extends Migration ...@@ -15,7 +15,8 @@ class CreateAttemptsTable extends Migration
{ {
Schema::create('attempts', static function (Blueprint $table) { Schema::create('attempts', static function (Blueprint $table) {
$table->id(); $table->id();
$table->string('door_location'); $table->unsignedBigInteger('door_id');
$table->foreign('door_id')->references('id')->on('doors');
$table->timestamps(); $table->timestamps();
}); });
} }
......
...@@ -38,12 +38,6 @@ ...@@ -38,12 +38,6 @@
{{ door($row[$header])['name'] }} (ID: {{ $row[$header] }}) {{ door($row[$header])['name'] }} (ID: {{ $row[$header] }})
</a> </a>
</td> </td>
@elseif($header === 'door_shallow_link')
<td>
<a href="{{ route('web.admin.doors.index', ['query' => $row[$header]]) }}">
{{ $row[$header] }}
</a>
</td>
@elseif($header === 'schedule_id') @elseif($header === 'schedule_id')
<td> <td>
<a href="{{ route('web.admin.schedules.edit', ['scheduleId' => $row[$header]]) }}"> <a href="{{ route('web.admin.schedules.edit', ['scheduleId' => $row[$header]]) }}">
......
...@@ -15,7 +15,7 @@ class Attempt ...@@ -15,7 +15,7 @@ class Attempt
/** /**
* @var int * @var int
*/ */
protected string $doorLocation; protected int $doorId;
/** /**
* @var \Carbon\Carbon|null * @var \Carbon\Carbon|null
...@@ -29,12 +29,12 @@ class Attempt ...@@ -29,12 +29,12 @@ class Attempt
public function __construct( public function __construct(
int $id, int $id,
string $doorLocation, int $doorId,
?Carbon $createdAt = null, ?Carbon $createdAt = null,
?Carbon $updatedAt = null ?Carbon $updatedAt = null
) { ) {
$this->id = $id; $this->id = $id;
$this->doorLocation = $doorLocation; $this->doorId = $doorId;
$this->createdAt = $createdAt; $this->createdAt = $createdAt;
$this->updatedAt = $updatedAt; $this->updatedAt = $updatedAt;
} }
...@@ -48,11 +48,11 @@ class Attempt ...@@ -48,11 +48,11 @@ class Attempt
} }
/** /**
* @return string * @return int
*/ */
public function getDoorLocation(): string public function getDoorId(): int
{ {
return $this->doorLocation; return $this->doorId;
} }
/** /**
...@@ -94,15 +94,15 @@ class Attempt ...@@ -94,15 +94,15 @@ class Attempt
} }
/** /**
* @param string|null $doorLocation * @param string|null $doorId
* @return bool * @return bool
*/ */
public function hasDoorLocationLike(?string $doorLocation): bool public function hasDoorIdOf(?string $doorId): bool
{ {
if (!$doorLocation) { if (!$doorId) {
return false; return false;
} }
return stripos($this->getDoorLocation(), $doorLocation) !== false; return $this->getDoorId() === (int)$doorId;
} }
} }
...@@ -18,9 +18,9 @@ class Entry ...@@ -18,9 +18,9 @@ class Entry
protected int $userId; protected int $userId;
/** /**
* @var string * @var int
*/ */
protected string $doorLocation; protected int $doorId;
/** /**
* @var \Carbon\Carbon|null * @var \Carbon\Carbon|null
...@@ -40,14 +40,14 @@ class Entry ...@@ -40,14 +40,14 @@ class Entry
public function __construct( public function __construct(
int $id, int $id,
int $userId, int $userId,
string $doorLocation, int $doorId,
bool $success, bool $success,
?Carbon $createdAt = null, ?Carbon $createdAt = null,
?Carbon $updatedAt = null ?Carbon $updatedAt = null
) { ) {
$this->id = $id; $this->id = $id;
$this->userId = $userId; $this->userId = $userId;
$this->doorLocation = $doorLocation; $this->doorId = $doorId;
$this->success = $success; $this->success = $success;
$this->createdAt = $createdAt; $this->createdAt = $createdAt;
$this->updatedAt = $updatedAt; $this->updatedAt = $updatedAt;
...@@ -70,11 +70,11 @@ class Entry ...@@ -70,11 +70,11 @@ class Entry
} }
/** /**
* @return string * @return int
*/ */
public function getDoorLocation(): string public function getDoorId(): int
{ {
return $this->doorLocation; return $this->doorId;
} }
/** /**
...@@ -141,15 +141,15 @@ class Entry ...@@ -141,15 +141,15 @@ class Entry
} }
/** /**
* @param string|null $doorLocation * @param string|null $doorId
* @return bool * @return bool
*/ */
public function hasDoorLocationLike(?string $doorLocation): bool public function hasDoorIdOf(?string $doorId): bool
{ {
if (!$doorLocation) { if (!$doorId) {
return false; return false;
} }
return stripos($this->getDoorLocation(), $doorLocation) !== false; return $this->getDoorId() === (int)$doorId;
} }
} }
...@@ -17,8 +17,8 @@ interface AttemptsRepository ...@@ -17,8 +17,8 @@ interface AttemptsRepository
/** /**
* @param \Carbon\Carbon|null $begin * @param \Carbon\Carbon|null $begin
* @param \Carbon\Carbon|null $end * @param \Carbon\Carbon|null $end
* @param string|null $doorLocation * @param string|null $doorId
* @return \Source\Entities\Attempt[] * @return \Source\Entities\Attempt[]
*/ */
public function getBetween(?Carbon $begin = null, ?Carbon $end = null, ?string $doorLocation = null): array; public function getBetween(?Carbon $begin = null, ?Carbon $end = null, ?string $doorId = null): array;
} }
...@@ -19,7 +19,7 @@ class DatabaseAttemptsRepository implements AttemptsRepository ...@@ -19,7 +19,7 @@ class DatabaseAttemptsRepository implements AttemptsRepository
{ {
return new Attempt( return new Attempt(
$attempt->getAttribute('id'), $attempt->getAttribute('id'),
$attempt->getAttribute('door_location'), $attempt->getAttribute('door_id'),
$attempt->getAttribute('created_at'), $attempt->getAttribute('created_at'),
$attempt->getAttribute('updated_at') $attempt->getAttribute('updated_at')
); );
...@@ -28,12 +28,12 @@ class DatabaseAttemptsRepository implements AttemptsRepository ...@@ -28,12 +28,12 @@ class DatabaseAttemptsRepository implements AttemptsRepository
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function getBetween(?Carbon $begin = null, ?Carbon $end = null, ?string $doorLocation = null): array public function getBetween(?Carbon $begin = null, ?Carbon $end = null, ?string $doorId = null): array
{ {
$query = \App\Attempt::query()->orderByDesc('created_at'); $query = \App\Attempt::query()->orderByDesc('created_at');
if ($doorLocation) { if ($doorId) {
$query->where('door_location', 'ILIKE', "%$doorLocation%"); $query->where('door_id', self::castToInt($doorId));
} }
if ($begin && $end) { if ($begin && $end) {
...@@ -55,7 +55,7 @@ class DatabaseAttemptsRepository implements AttemptsRepository ...@@ -55,7 +55,7 @@ class DatabaseAttemptsRepository implements AttemptsRepository
public function add(Attempt $attempt): ?Attempt public function add(Attempt $attempt): ?Attempt
{ {
$a = new \App\Attempt(); $a = new \App\Attempt();
$a->setAttribute('door_location', $attempt->getDoorLocation()); $a->setAttribute('door_id', $attempt->getDoorId());
$a->save(); $a->save();
......
...@@ -34,11 +34,11 @@ class InMemoryAttemptsRepository implements AttemptsRepository ...@@ -34,11 +34,11 @@ class InMemoryAttemptsRepository implements AttemptsRepository
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function getBetween(?Carbon $begin = null, ?Carbon $end = null, ?string $doorLocation = null): array public function getBetween(?Carbon $begin = null, ?Carbon $end = null, ?string $doorId = null): array
{ {
return array_values(array_filter($this->attempts, static function (Attempt $attempt) use ($begin, $end, $doorLocation) { return array_values(array_filter($this->attempts, static function (Attempt $attempt) use ($begin, $end, $doorId) {
if ($doorLocation) { if ($doorId) {
return $attempt->isBetween($begin, $end) && $attempt->hasDoorLocationLike($doorLocation); return $attempt->isBetween($begin, $end) && $attempt->hasDoorIdOf($doorId);
} }
return $attempt->isBetween($begin, $end); return $attempt->isBetween($begin, $end);
})); }));
......
...@@ -13,13 +13,13 @@ class LocalAttemptsRepository extends InMemoryAttemptsRepository ...@@ -13,13 +13,13 @@ class LocalAttemptsRepository extends InMemoryAttemptsRepository
{ {
$this->add(new Attempt( $this->add(new Attempt(
1, 1,
LocalDoorsRepository::getTheBatCave()->getLocation(), LocalDoorsRepository::getTheBatCave()->getId(),
Carbon::now()->subHour() Carbon::now()->subHour()
)); ));
$this->add(new Attempt( $this->add(new Attempt(
2, 2,
LocalDoorsRepository::