From 7edf1669851989954ed4fee386c7fe127f2f2f3a Mon Sep 17 00:00:00 2001 From: Matt Young Date: Fri, 31 May 2024 01:07:01 -0500 Subject: [PATCH] Entries listing page nearly complete --- app/Http/Controllers/EntryController.php | 8 ++- app/Http/Controllers/StudentController.php | 2 +- app/Models/School.php | 7 ++ app/Models/Student.php | 6 ++ app/Models/User.php | 11 ++++ app/Policies/EntryPolicy.php | 13 ++-- database/factories/EntryFactory.php | 3 +- ...2024_05_31_031713_create_entries_table.php | 1 + database/seeders/EntrySeeder.php | 48 +++++++++++++- resources/views/entries/index.blade.php | 64 +++++++++++++++++++ resources/views/students/index.blade.php | 2 + resources/views/test.blade.php | 49 ++------------ routes/web.php | 11 +++- 13 files changed, 174 insertions(+), 51 deletions(-) create mode 100644 resources/views/entries/index.blade.php 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 + + Entries + + + + Add Entry + + + + + + {{-- TODO make grade a dropdown --}} + Save + + + + + + + Entry Listing +
+ + + + Name + Grade + Audition + + Edit + + + + + @foreach($entries as $entry) + + {{ $entry->student->full_name(true) }} + {{ $entry->student->grade }} + {{ $entry->audition->name }} +{{-- --}} +{{-- Edit--}} +{{-- |--}} + +{{--
--}} +{{-- @csrf--}} +{{-- @method('DELETE')--}} +{{-- Delete--}} +{{-- --}} + +{{--
--}} + + @endforeach +
+
+
+
+
+
diff --git a/resources/views/students/index.blade.php b/resources/views/students/index.blade.php index 844ec90..ed32685 100644 --- a/resources/views/students/index.blade.php +++ b/resources/views/students/index.blade.php @@ -29,6 +29,7 @@ Name Grade + Entries Edit @@ -39,6 +40,7 @@ {{ $student->full_name(true) }} {{ $student->grade }} + {{ $student->entries->count() }} Edit | diff --git a/resources/views/test.blade.php b/resources/views/test.blade.php index 19b05bf..c3fe94f 100644 --- a/resources/views/test.blade.php +++ b/resources/views/test.blade.php @@ -1,49 +1,14 @@ @php use App\Models\School;use App\Models\SchoolEmailDomain;use App\Models\User;use Illuminate\Support\Facades\Auth; @endphp Test Page + @php + $entries = Auth::user()->entries()->with(['student','audition'])->get(); +// dd($entries->first()->student->full_name()) + @endphp - - - Users - A list of all the users in your account including their name, title, email and role. - Add User - - - - Name - Title - Email - Role - - Edit - - - - - - Lindsay Walton - Front-end Developer - lindsay.walton@example.com - Member - - Edit, Lindsay Walton - - - - - Lindsay Walton - Front-end Developer - lindsay.walton@example.com - Member - - Edit - - - - - - - + @foreach ($entries as $e) + {{ $e->student->full_name() }} is entered on {{ $e->audition->name }}
+ @endforeach
diff --git a/routes/web.php b/routes/web.php index 601b5e8..5663dc6 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,6 +1,7 @@ group(function () { }); // Entry Related Routes - +Route::middleware(['auth','verified'])->controller(EntryController::class)->group(function() { + Route::get('/entries','index'); + Route::get('/entries/create','create'); + Route::get('/entries/{entry}', 'show'); + Route::post('/entries', 'store'); + Route::get('/entries/{entry}/edit', 'edit'); + Route::patch('/entries/{entry}', 'update'); + Route::delete('/entries/{entry}', 'destroy'); +}); // User Related Routes Route::middleware(['auth','verified'])->controller(UserController::class)->group(function() {