diff --git a/app/Http/Controllers/EntryController.php b/app/Http/Controllers/EntryController.php
index 8d536da..5ed88ea 100644
--- a/app/Http/Controllers/EntryController.php
+++ b/app/Http/Controllers/EntryController.php
@@ -3,8 +3,14 @@
namespace App\Http\Controllers;
use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
class EntryController extends Controller
{
- //
+ // TODO authorization policies
+ public function index()
+ {
+ $entries = Auth::user()->entries()->with(['student','audition'])->get();
+ return view('entries.index',['entries' => $entries]);
+ }
}
diff --git a/app/Http/Controllers/StudentController.php b/app/Http/Controllers/StudentController.php
index 2ff5850..c7d05dd 100644
--- a/app/Http/Controllers/StudentController.php
+++ b/app/Http/Controllers/StudentController.php
@@ -19,7 +19,7 @@ class StudentController extends Controller
*/
public function index()
{
- $students = Auth::user()->students;
+ $students = Auth::user()->students()->with('entries')->get();
return view('students.index',['students' => $students]);
}
diff --git a/app/Models/School.php b/app/Models/School.php
index c1e9941..ab5ee2d 100644
--- a/app/Models/School.php
+++ b/app/Models/School.php
@@ -5,6 +5,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
+use Illuminate\Database\Eloquent\Relations\HasManyThrough;
class School extends Model
{
@@ -36,4 +37,10 @@ class School extends Model
{
return $this->hasMany(Student::class);
}
+
+ public function entries(): HasManyThrough
+ {
+ return $this->hasManyThrough(Entry::class,Student::class);
+ }
+
}
diff --git a/app/Models/Student.php b/app/Models/Student.php
index 794148b..c4b7089 100644
--- a/app/Models/Student.php
+++ b/app/Models/Student.php
@@ -5,6 +5,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
+use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
@@ -22,6 +23,11 @@ class Student extends Model
return $this->hasManyThrough(User::class, School::class);
}
+ public function entries(): HasMany
+ {
+ return $this->hasMany(Entry::class);
+ }
+
public function full_name(Bool $last_name_first = false): String
{
if ($last_name_first) return $this->last_name . ', ' . $this->first_name;
diff --git a/app/Models/User.php b/app/Models/User.php
index 1a078f7..cb76543 100644
--- a/app/Models/User.php
+++ b/app/Models/User.php
@@ -78,6 +78,17 @@ class User extends Authenticatable implements MustVerifyEmail
->orderBy('first_name');
}
+ public function entries(): HasManyThrough
+ {
+ return $this->hasManyThrough(
+ Entry::class,
+ Student::class,
+ 'school_id',
+ 'student_id',
+ 'school_id',
+ 'id'
+ );
+ }
/**
* Return an array of schools using the users email domain
diff --git a/app/Policies/EntryPolicy.php b/app/Policies/EntryPolicy.php
index 4e4b6d8..abd0e6e 100644
--- a/app/Policies/EntryPolicy.php
+++ b/app/Policies/EntryPolicy.php
@@ -5,6 +5,7 @@ namespace App\Policies;
use App\Models\Entry;
use App\Models\User;
use Illuminate\Auth\Access\Response;
+use function is_null;
class EntryPolicy
{
@@ -21,7 +22,8 @@ class EntryPolicy
*/
public function view(User $user, Entry $entry): bool
{
- //
+ if($user->is_admin) return true;
+ return $user->school_id == $entry->student()->school_id;
}
/**
@@ -29,7 +31,8 @@ class EntryPolicy
*/
public function create(User $user): bool
{
- //
+ if($user->is_admin) return true;
+ return ! is_null($user->school_id);
}
/**
@@ -37,7 +40,8 @@ class EntryPolicy
*/
public function update(User $user, Entry $entry): bool
{
- //
+ if($user->is_admin) return true;
+ return $user->school_id == $entry->student()->school_id;
}
/**
@@ -45,7 +49,8 @@ class EntryPolicy
*/
public function delete(User $user, Entry $entry): bool
{
- //
+ if($user->is_admin) return true;
+ return $user->school_id == $entry->student()->school_id;
}
/**
diff --git a/database/factories/EntryFactory.php b/database/factories/EntryFactory.php
index 3e99948..ad03155 100644
--- a/database/factories/EntryFactory.php
+++ b/database/factories/EntryFactory.php
@@ -17,7 +17,8 @@ class EntryFactory extends Factory
public function definition(): array
{
return [
- //
+ 'student_id' => 3,
+ 'audition_id' =>3
];
}
}
diff --git a/database/migrations/2024_05_31_031713_create_entries_table.php b/database/migrations/2024_05_31_031713_create_entries_table.php
index 0155ed9..fa01851 100644
--- a/database/migrations/2024_05_31_031713_create_entries_table.php
+++ b/database/migrations/2024_05_31_031713_create_entries_table.php
@@ -17,6 +17,7 @@ return new class extends Migration
$table->id();
$table->foreignIdFor(Student::class)->constrained()->restrictOnDelete()->cascadeOnUpdate();
$table->foreignIdFor(Audition::class)->constrained()->restrictOnDelete()->cascadeOnUpdate();
+ $table->unique(['student_id','audition_id']);
$table->timestamps();
});
}
diff --git a/database/seeders/EntrySeeder.php b/database/seeders/EntrySeeder.php
index ab64b8a..d6a1964 100644
--- a/database/seeders/EntrySeeder.php
+++ b/database/seeders/EntrySeeder.php
@@ -2,8 +2,13 @@
namespace Database\Seeders;
+use App\Models\Audition;
+use App\Models\Entry;
+use App\Models\Student;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
+use function rand;
+use function random_int;
class EntrySeeder extends Seeder
{
@@ -12,6 +17,47 @@ class EntrySeeder extends Seeder
*/
public function run(): void
{
- //
+ $students = Student::all();
+ $hs_auditions = Audition::where('maximum_grade', '=', '12');
+ $freshman_auditions = Audition::where('maximum_grade', '>', '8');
+ $jh_auditions = Audition::where('maximum_grade', '=', '9');
+ $seventh_auditions = Audition::where('maximum_grade', '=', '7');
+
+
+ foreach ($students as $student) {
+ if($student->grade > 9) $audition = Audition::where('maximum_grade','=','12')->inRandomOrder()->first();
+ if($student->grade == 9) $audition = Audition::where('maximum_grade','>','8')->inRandomOrder()->first();
+ if($student->grade == 8) $audition = Audition::where('maximum_grade','=','9')->inRandomOrder()->first();
+ if($student->grade == 7) $audition = Audition::where('maximum_grade','=','7')->inRandomOrder()->first();
+
+ Entry::factory()->create([
+ 'student_id' => $student->id,
+ 'audition_id' => $audition->id
+ ]);
+
+ if (random_int(1,100) > 80) {
+ if($student->grade > 9) $audition2 = Audition::where('maximum_grade','=','12')->where('id','!=',$audition->id)->inRandomOrder()->first();
+ if($student->grade == 9) $audition2 = Audition::where('maximum_grade','>','8')->where('id','!=',$audition->id)->inRandomOrder()->first();
+ if($student->grade == 8) $audition2 = Audition::where('maximum_grade','=','9')->where('id','!=',$audition->id)->inRandomOrder()->first();
+ if($student->grade == 7) $audition2 = Audition::where('maximum_grade','=','7')->where('id','!=',$audition->id)->inRandomOrder()->first();
+
+ Entry::factory()->create([
+ 'student_id' => $student->id,
+ 'audition_id' => $audition2->id
+ ]);
+ }
+
+ if (random_int(1,100) > 80) {
+ if($student->grade > 9) $audition3 = Audition::where('maximum_grade','=','12')->where('id','!=',$audition->id)->where('id','!=',$audition2->id)->inRandomOrder()->first();
+ if($student->grade == 9) $audition3 = Audition::where('maximum_grade','>','8')->where('id','!=',$audition->id)->where('id','!=',$audition2->id)->inRandomOrder()->first();
+ if($student->grade == 8) $audition3 = Audition::where('maximum_grade','=','9')->where('id','!=',$audition->id)->where('id','!=',$audition2->id)->inRandomOrder()->first();
+ if($student->grade == 7) $audition3 = Audition::where('maximum_grade','=','7')->where('id','!=',$audition->id)->where('id','!=',$audition2->id)->inRandomOrder()->first();
+
+ Entry::factory()->create([
+ 'student_id' => $student->id,
+ 'audition_id' => $audition3->id
+ ]);
+ }
+ }
}
}
diff --git a/resources/views/entries/index.blade.php b/resources/views/entries/index.blade.php
new file mode 100644
index 0000000..fba5a3d
--- /dev/null
+++ b/resources/views/entries/index.blade.php
@@ -0,0 +1,64 @@
+@php use Illuminate\Support\Facades\Auth; @endphp
+@push('scripts')
+ {{-- Code from https://codepen.io/ryangjchandler/pen/WNQQKeR--}}
+
+@endpush
+
+
+
+
+
+ @endforeach
+