Cleanup on entries page

This commit is contained in:
Matt Young 2024-05-31 23:37:38 -05:00
parent e71919f35f
commit 220dbbcd52
6 changed files with 37 additions and 28 deletions

View File

@ -4,14 +4,18 @@ namespace App\Http\Controllers;
use App\Models\Audition;
use App\Models\Entry;
use App\Models\School;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use function abort;
use function sendMessage;
class EntryController extends Controller
{
// TODO authorization policies
public function index()
{
$entries = Auth::user()->entries()->with(['student','audition'])->get();
$auditions = Audition::all();
$students = Auth::user()->students;
@ -21,6 +25,7 @@ class EntryController extends Controller
public function store(Request $request)
{
if ($request->user()->cannot('create', Entry::class)) abort(403);
// TODO write custom rule to verify the combination of student and audition is unique
$request->validate([
'student_id' => ['required', 'exists:students,id'],
@ -34,4 +39,11 @@ class EntryController extends Controller
return redirect('/entries');
}
public function destroy(Request $request, Entry $entry)
{
$entry->delete();
sendMessage('The ' . $entry->audition->name . 'entry for ' . $entry->student->full_name(). 'has been deleted.','success');
return redirect('/entries');
}
}

View File

@ -40,7 +40,7 @@ class StudentPolicy
*/
public function update(User $user, Student $student): bool
{
if (Entry::where('student_id','=',$student->id)->exists()) return false; // Don't allow deletion of a student with entries
if($user->is_admin) return true;
return $user->school_id == $student->school_id;
}
@ -50,6 +50,7 @@ class StudentPolicy
*/
public function delete(User $user, Student $student): bool
{
if (Entry::where('student_id','=',$student->id)->exists()) return false; // Don't allow deletion of a student with entries
return $user->school_id == $student->school_id;
}

View File

@ -14,10 +14,11 @@
</div>
<div class="hidden md:block">
<div class="ml-10 flex items-baseline space-x-4">
<!-- Current: "bg-indigo-700 text-white", Default: "text-white hover:bg-indigo-500 hover:bg-opacity-75" -->
<!-- Current: "bg-indigo-700 text-white",
Default: "text-white hover:bg-indigo-500 hover:bg-opacity-75" -->
<x-layout.nav-link href="/dashboard" :active="request()->is('dashboard')">Dashboard</x-layout.nav-link>
<x-layout.nav-link href="/students" :active="request()->is('students')">Students</x-layout.nav-link>
<x-layout.nav-link href="/entries" :actve="request()->is('entries')">Entries</x-layout.nav-link>
<x-layout.nav-link href="/entries" :active="request()->is('entries')">Entries</x-layout.nav-link>
{{-- <a href="/dashboard" class="bg-indigo-700 text-white rounded-md px-3 py-2 text-sm font-medium" aria-current="page">Dashboard</a>--}}
{{-- <a href="/students" class="text-white hover:bg-indigo-500 hover:bg-opacity-75 rounded-md px-3 py-2 text-sm font-medium">Students</a>--}}

View File

@ -37,6 +37,7 @@
<x-layout.page-section>
<x-slot:section_name>Entry Listing</x-slot:section_name>
<x-slot:section_description>You have {{ $entries->count() }} entries</x-slot:section_description>
<div class="px-4">
<x-table.table>
<thead>
@ -55,19 +56,16 @@
<x-table.td first>{{ $entry->student->full_name(true) }}</x-table.td>
<x-table.td>{{ $entry->student->grade }}</x-table.td>
<x-table.td>{{ $entry->audition->name }}</x-table.td>
{{-- <x-table.td for_button>--}}
{{-- <x-table.button href="/students/{{ $student->id }}/edit">Edit</x-table.button>--}}
{{-- |--}}
<x-table.td for_button>
<form method="POST" action="/entries/{{ $entry->id }}" class="inline">
@csrf
@method('DELETE')
<x-table.button
onclick="return confirm('Please confirm you would like to delete the {{ $entry->audition->name }} entry for {{ $entry->student->full_name() }}');"
>Delete</x-table.button>
</form>
{{-- <form method="POST" action="/students/{{ $student->id }}" class="inline">--}}
{{-- @csrf--}}
{{-- @method('DELETE')--}}
{{-- <x-table.button--}}
{{-- onclick="return confirm('Please confirm you would like to delete the student {{ $student->full_name() }}');"--}}
{{-- >Delete</x-table.button>--}}
{{-- </form>--}}
{{-- </x-table.td>--}}
</x-table.td>
</tr>
@endforeach
</x-table.body>

View File

@ -42,9 +42,7 @@
<x-table.td>{{ $student->grade }}</x-table.td>
<x-table.td>{{ $student->entries->count() }}</x-table.td>
<x-table.td for_button>
<x-table.button href="/students/{{ $student->id }}/edit">Edit</x-table.button>
|
@can('delete', $student)
<form method="POST" action="/students/{{ $student->id }}" class="inline">
@csrf
@method('DELETE')
@ -52,7 +50,9 @@
onclick="return confirm('Please confirm you would like to delete the student {{ $student->full_name() }}');"
>Delete</x-table.button>
</form>
|
@endcan
<x-table.button href="/students/{{ $student->id }}/edit">Edit</x-table.button>
</x-table.td>
</tr>
@endforeach

View File

@ -24,10 +24,7 @@ Route::middleware(['auth','verified'])->group(function () {
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');
});