Skip to content

Commit

Permalink
Convert backend of SubProject model to Eloquent
Browse files Browse the repository at this point in the history
  • Loading branch information
williamjallen committed Sep 1, 2023
1 parent 1f5b938 commit ea5b7ec
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 395 deletions.
9 changes: 9 additions & 0 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* @property int $id
Expand Down Expand Up @@ -95,4 +96,12 @@ class Project extends Model
'authenticatesubmissions',
'viewsubprojectslink',
];

/**
* @return HasMany<SubProject>
*/
public function subprojects(): HasMany
{
return $this->hasMany(SubProject::class, 'projectid', 'id');
}
}
75 changes: 75 additions & 0 deletions app/Models/SubProject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Support\Carbon;

/**
* @property int $id
* @property string $name
* @property int $projectid
* @property int $groupid
* @property string $path
* @property int $position
* @property Carbon $starttime
* @property Carbon $endtime
*
* @mixin Builder<SubProject>
*/
class SubProject extends Model
{
protected $table = 'subproject';

public $timestamps = false;

protected $fillable = [
'name',
'projectid',
'groupid',
'path',
'position',
'starttime',
'endtime',
];

protected $casts = [
'id' => 'integer',
'projectid' => 'integer',
'groupid' => 'integer',
'position' => 'integer',
'starttime' => 'datetime',
'endtime' => 'datetime',
];

/**
* @return BelongsTo<Project, self>
*/
public function project(): BelongsTo
{
return $this->belongsTo(Project::class, 'id', 'projectid');
}

/**
* Return the subprojects which depended upon this subproject on the specified date.
* If no date is provided, the current date is used.
*
* @return BelongsToMany<self>
*/
public function children(?Carbon $date = null): BelongsToMany
{
if ($date === null) {
$date = Carbon::now()->setTimezone('UTC');
}

return $this->belongsToMany(SubProject::class, 'subproject2subproject', 'subprojectid', 'dependsonid')
->wherePivot('starttime', '<=', Carbon::now()->setTimezone('UTC'))
->where(function ($query) use ($date) {
$query->where('subproject2subproject.endtime', '>', $date)
->orWhere('subproject2subproject.endtime', '=', Carbon::create(1980));
});
}
}
Loading

0 comments on commit ea5b7ec

Please sign in to comment.