Delete calculated scores on score sheet changes, deletions, and additions.

Addresses #68
This commit is contained in:
Matt Young 2024-10-31 12:07:49 -05:00
parent f8d778dbcc
commit e93ae750e4
2 changed files with 30 additions and 0 deletions

View File

@ -121,6 +121,11 @@ class Entry extends Model
return $this->hasOne(Seat::class); return $this->hasOne(Seat::class);
} }
public function calculatedScores(): HasMany
{
return $this->hasMany(CalculatedScore::class);
}
public function scopeForSeating(Builder $query): void public function scopeForSeating(Builder $query): void
{ {
$query->where('for_seating', 1); $query->where('for_seating', 1);

View File

@ -16,6 +16,22 @@ class ScoreSheet extends Model
protected $casts = ['subscores' => 'json']; protected $casts = ['subscores' => 'json'];
protected static function boot()
{
parent::boot();
static::created(function ($scoreSheet) {
$scoreSheet->deleteRelatedCalculatedScores();
});
static::updated(function ($scoreSheet) {
$scoreSheet->deleteRelatedCalculatedScores();
});
static::deleted(function ($scoreSheet) {
$scoreSheet->deleteRelatedCalculatedScores();
});
}
public function entry(): BelongsTo public function entry(): BelongsTo
{ {
return $this->belongsTo(Entry::class); return $this->belongsTo(Entry::class);
@ -37,9 +53,18 @@ class ScoreSheet extends Model
'audition_id' // Local key on the intermediate model (Entry) 'audition_id' // Local key on the intermediate model (Entry)
); );
} }
public function getSubscore($id) public function getSubscore($id)
{ {
return $this->subscores[$id]['score'] ?? false; return $this->subscores[$id]['score'] ?? false;
// this function is used at resources/views/tabulation/entry_score_sheet.blade.php // this function is used at resources/views/tabulation/entry_score_sheet.blade.php
} }
public function deleteRelatedCalculatedScores(): void
{
$entry = $this->entry;
if ($entry) {
$entry->calculatedScores()->delete();
}
}
} }