Skip to content

Commit

Permalink
Added Models, migrations, factories and seeders
Browse files Browse the repository at this point in the history
  • Loading branch information
troccoli committed Jul 20, 2024
1 parent 44c29e6 commit 49bbd3a
Show file tree
Hide file tree
Showing 40 changed files with 1,226 additions and 12 deletions.
20 changes: 20 additions & 0 deletions app/Models/Builders/CompetitionBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Models\Builders;

use App\Models\Season;
use Illuminate\Database\Eloquent\Builder;

class CompetitionBuilder extends Builder
{
public function inSeason(Season|string $season): self
{
if ($season instanceof Season) {
$season = $season->getKey();
}

$this->query->where('season_id', $season);

return $this;
}
}
20 changes: 20 additions & 0 deletions app/Models/Builders/DivisionBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Models\Builders;

use App\Models\Competition;
use Illuminate\Database\Eloquent\Builder;

class DivisionBuilder extends Builder
{
public function inCompetition(Competition|string $competition): self
{
if ($competition instanceof Competition) {
$competition = $competition->getKey();
}

$this->query->where('competition_id', $competition);

return $this;
}
}
20 changes: 20 additions & 0 deletions app/Models/Builders/FixtureBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Models\Builders;

use App\Models\Division;
use Illuminate\Database\Eloquent\Builder;

class FixtureBuilder extends Builder
{
public function inDivision(Division|string $division): self
{
if ($division instanceof Division) {
$division = $division->getKey();
}

$this->query->where('division_id', $division);

return $this;
}
}
37 changes: 37 additions & 0 deletions app/Models/Club.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* @property string $name
* @property-read Collection $teams
* @property-read ?Venue $venue
*/
class Club extends Model
{
use HasFactory,
HasUuids;

/** @var array<int, string> */
protected $fillable = [
'name',
'venue_id',
];

public function teams(): HasMany
{
return $this->hasMany(Team::class);
}

public function venue(): BelongsTo
{
return $this->belongsTo(Venue::class);
}
}
46 changes: 46 additions & 0 deletions app/Models/Competition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Models;

use App\Models\Builders\CompetitionBuilder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* @property string $season_id
* @property string $name
* @property-read Season $season
* @property-read Collection $divisions
*
* @method CompetitionBuilder query()
*/
class Competition extends Model
{
use HasFactory,
HasUuids;

/** @var array<int, string> */
protected $fillable = [
'season_id',
'name',
];

public function newEloquentBuilder($query): CompetitionBuilder
{
return new CompetitionBuilder($query);
}

public function season(): BelongsTo
{
return $this->belongsTo(Season::class);
}

public function divisions(): HasMany
{
return $this->hasMany(Division::class);
}
}
57 changes: 57 additions & 0 deletions app/Models/Division.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Models;

use App\Models\Builders\DivisionBuilder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
* @property string $competition_id
* @property string $name
* @property int $display_order
* @property-read Competition $competition
* @property-read Collection $teams
* @property-read Collection $fixtures
*
* @method DivisionBuilder query()
*/
class Division extends Model
{
use HasFactory,
HasUuids,
SoftDeletes;

/** @var array<int, string> */
protected $fillable = [
'competition_id',
'name',
'display_order',
];

public function newEloquentBuilder($query): DivisionBuilder
{
return new DivisionBuilder($query);
}

public function competition(): BelongsTo
{
return $this->belongsTo(Competition::class);
}

public function teams(): BelongsToMany
{
return $this->belongsToMany(Team::class);
}

public function fixtures(): HasMany
{
return $this->hasMany(Fixture::class);
}
}
97 changes: 97 additions & 0 deletions app/Models/Fixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace App\Models;

use App\Models\Builders\FixtureBuilder;
use Carbon\CarbonImmutable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;

/**
* @property int $match_number
* @property string $division_id
* @property string home_team_id
* @property string away_team_id
* @property CarbonImmutable $match_date
* @property-read CarbonImmutable $match_datetime
* @property string venue_id
* @property-read Division $division
* @property-read Team homeTeam
* @property-read Team awayTeam
* @property-read Venue $venue
*/
class Fixture extends Model
{
use HasFactory,
HasUuids,
SoftDeletes;

protected $guarded = [
'id',
self::CREATED_AT,
self::UPDATED_AT,
'deleted_at',
];

protected $casts = [
'match_date' => 'immutable_date',
];

public function newEloquentBuilder($query): Builder
{
return new FixtureBuilder($query);
}

protected function matchDatetime(): Attribute
{
return Attribute::make(
get: function (mixed $value, array $attributes): CarbonImmutable {
/** @var Carbon $matchDatetime */
$matchDatetime = $attributes['match_date']->copy()->toMutable();
$matchDatetime->setTimeFrom($attributes['start_time']);

return $matchDatetime->toImmutable();
},
);
}

protected function startTime(): Attribute
{
return Attribute::make(
set: function (string|Carbon $value): string {
if ($value instanceof Carbon) {
return $value->format('H:i');
}

return $value;
},
);
}

public function division(): BelongsTo
{
return $this->belongsTo(Division::class);
}

public function homeTeam(): HasOne
{
return $this->hasOne(related: Team::class, foreignKey: 'id', localKey: 'home_team_id');
}

public function awayTeam(): HasOne
{
return $this->hasOne(related: Team::class, foreignKey: 'id', localKey: 'away_team_id');
}

public function venue(): BelongsTo
{
return $this->belongsTo(Venue::class);
}
}
37 changes: 37 additions & 0 deletions app/Models/Season.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* @property int $year
* @property-read string $name
* @property-read Collection $competitions
*/
class Season extends Model
{
use HasFactory,
HasUuids;

/** @var array<int, string> */
protected $fillable = [
'year',
];

protected static function booted(): void
{
static::saving(function (Season $season) {
$season->name = sprintf('%4u/%02u', $season->year, ($season->year + 1) % 100);
});
}

public function competitions(): HasMany
{
return $this->hasMany(Competition::class);
}
}
Loading

0 comments on commit 49bbd3a

Please sign in to comment.