Set foundation for new handling of doublers

This commit is contained in:
Matt Young 2025-06-14 11:12:36 -05:00
parent 250a3856ba
commit 34e22187dd
4 changed files with 87 additions and 1 deletions

View File

@ -11,6 +11,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
use function in_array; use function in_array;
class Audition extends Model class Audition extends Model
@ -81,7 +82,7 @@ class Audition extends Model
*/ */
public function getJudgesCountAttribute() public function getJudgesCountAttribute()
{ {
if (! isset($this->attributes['judges_count'])) { if (!isset($this->attributes['judges_count'])) {
$this->attributes['judges_count'] = $this->judges()->count(); $this->attributes['judges_count'] = $this->judges()->count();
} }
@ -129,6 +130,17 @@ class Audition extends Model
return $this->hasMany(Seat::class); return $this->hasMany(Seat::class);
} }
public function getDoublerEntries(): Collection
{
return $this->entries()
->whereIn('student_id', function ($query) {
$query->select('student_id')
->from('doubler_entry_counts')
->where('event_id', $this->event_id);
})
->get();
}
public function scopeOpen(Builder $query): void public function scopeOpen(Builder $query): void
{ {
$currentDate = Carbon::now('America/Chicago'); $currentDate = Carbon::now('America/Chicago');

View File

@ -0,0 +1,21 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class DoublerEntryCount extends Model
{
protected $table = 'doubler_entry_counts';
public function student(): BelongsTo
{
return $this->belongsTo(Student::class);
}
public function event(): BelongsTo
{
return $this->belongsTo(Event::class);
}
}

View File

@ -50,6 +50,9 @@ class Student extends Model
return $this->hasMany(HistoricalSeat::class); return $this->hasMany(HistoricalSeat::class);
} }
/**
* Returns the directors at this student's school.
*/
public function users(): HasManyThrough public function users(): HasManyThrough
{ {
return $this->hasManyThrough( return $this->hasManyThrough(
@ -62,6 +65,11 @@ class Student extends Model
); );
} }
/**
* Returns the directors at this student's school.
* Alias of users())
* '
*/
public function directors(): HasManyThrough public function directors(): HasManyThrough
{ {
return $this->users(); return $this->users();
@ -85,4 +93,14 @@ class Student extends Model
{ {
return $this->hasMany(DoublerRequest::class); return $this->hasMany(DoublerRequest::class);
} }
public function isDoublerInEvent(Event|int $event): bool
{
$eventId = $event instanceof Event ? $event->id : $event;
return DoublerEntryCount::where([
'event_id' => $eventId,
'student_id' => $this->id,
])->exists();
}
} }

View File

@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
DB::statement('
CREATE VIEW doubler_entry_counts AS
SELECT
e.event_id,
ent.student_id,
COUNT(*) as entry_count
FROM entries ent
JOIN auditions e ON e.id = ent.audition_id
GROUP BY e.event_id, ent.student_id
HAVING COUNT(*) > 1
');
}
/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};