Use route model binding

This commit is contained in:
Marcus Moore 2024-10-31 12:34:06 -07:00
parent ae24b73b32
commit 5f83cb6a14
No known key found for this signature in database
7 changed files with 16 additions and 49 deletions

View file

@ -27,17 +27,10 @@ class ReportTemplatesController extends Controller
return redirect()->route('report-templates.show', $report->id); return redirect()->route('report-templates.show', $report->id);
} }
public function show($reportId) public function show(ReportTemplate $reportTemplate)
{ {
$this->authorize('reports.view'); $this->authorize('reports.view');
$reportTemplate = ReportTemplate::find($reportId);
if (!$reportTemplate) {
return redirect()->route('reports/custom')
->with('error', trans('admin/reports/message.no_report_permission'));
}
$customfields = CustomField::get(); $customfields = CustomField::get();
$report_templates = ReportTemplate::orderBy('name')->get(); $report_templates = ReportTemplate::orderBy('name')->get();
@ -48,34 +41,20 @@ class ReportTemplatesController extends Controller
]); ]);
} }
public function edit($reportId) public function edit(ReportTemplate $reportTemplate)
{ {
$this->authorize('reports.view'); $this->authorize('reports.view');
$reportTemplate = ReportTemplate::find($reportId);
if (!$reportTemplate) {
return redirect()->route('reports/custom')
->with('error', trans('admin/reports/message.no_report_permission'));
}
return view('reports/custom', [ return view('reports/custom', [
'customfields' => CustomField::get(), 'customfields' => CustomField::get(),
'template' => $reportTemplate, 'template' => $reportTemplate,
]); ]);
} }
public function update(Request $request, $reportId): RedirectResponse public function update(Request $request, ReportTemplate $reportTemplate): RedirectResponse
{ {
$this->authorize('reports.view'); $this->authorize('reports.view');
$reportTemplate = ReportTemplate::find($reportId);
if (!$reportTemplate) {
return redirect()->route('reports/custom')
->with('error', trans('admin/reports/message.no_report_permission'));
}
$reportTemplate->options = $request->except(['_token', 'name']); $reportTemplate->options = $request->except(['_token', 'name']);
$reportTemplate->save(); $reportTemplate->save();
@ -84,17 +63,10 @@ class ReportTemplatesController extends Controller
return redirect()->route('report-templates.show', $reportTemplate->id); return redirect()->route('report-templates.show', $reportTemplate->id);
} }
public function destroy($reportId): RedirectResponse public function destroy(ReportTemplate $reportTemplate): RedirectResponse
{ {
$this->authorize('reports.view'); $this->authorize('reports.view');
$reportTemplate = ReportTemplate::find($reportId);
if (!$reportTemplate) {
return redirect()->route('reports/custom')
->with('error', trans('admin/reports/message.delete.no_delete_permission'));
}
$reportTemplate->delete(); $reportTemplate->delete();
return redirect()->route('reports/custom') return redirect()->route('reports/custom')

View file

@ -1,7 +1,6 @@
<?php <?php
return [ return [
'no_report_permission' => 'Template does not exist or you do not have permission to view it.',
'about_templates' => 'About Saved Templates', 'about_templates' => 'About Saved Templates',
'saving_templates_description' => 'Select your options, then enter the name of your template in the box above and click the \'Save Template\' button. Use the dropdown to select a previously saved template.', 'saving_templates_description' => 'Select your options, then enter the name of your template in the box above and click the \'Save Template\' button. Use the dropdown to select a previously saved template.',
'create' => [ 'create' => [

View file

@ -383,10 +383,10 @@ Route::group(['middleware' => ['auth']], function () {
Route::prefix('reports/templates')->name('report-templates')->group(function () { Route::prefix('reports/templates')->name('report-templates')->group(function () {
Route::post('/', [ReportTemplatesController::class, 'store'])->name('.store'); Route::post('/', [ReportTemplatesController::class, 'store'])->name('.store');
Route::get('/{reportId}', [ReportTemplatesController::class, 'show'])->name('.show'); Route::get('/{reportTemplate}', [ReportTemplatesController::class, 'show'])->name('.show');
Route::get('/{reportId}/edit', [ReportTemplatesController::class, 'edit'])->name('.edit'); Route::get('/{reportTemplate}/edit', [ReportTemplatesController::class, 'edit'])->name('.edit');
Route::post('/{reportId}', [ReportTemplatesController::class, 'update'])->name('.update'); Route::post('/{reportTemplate}', [ReportTemplatesController::class, 'update'])->name('.update');
Route::delete('/{reportId}', [ReportTemplatesController::class, 'destroy'])->name('.destroy'); Route::delete('/{reportTemplate}', [ReportTemplatesController::class, 'destroy'])->name('.destroy');
}); });
Route::get( Route::get(

View file

@ -17,7 +17,7 @@ class DeleteReportTemplateTest extends TestCase implements TestsPermissionsRequi
$this->actingAs(User::factory()->create()) $this->actingAs(User::factory()->create())
->post($this->getRoute($reportTemplate)) ->post($this->getRoute($reportTemplate))
->assertForbidden(); ->assertNotFound();
$this->assertModelExists($reportTemplate); $this->assertModelExists($reportTemplate);
} }
@ -28,8 +28,7 @@ class DeleteReportTemplateTest extends TestCase implements TestsPermissionsRequi
$this->actingAs(User::factory()->canViewReports()->create()) $this->actingAs(User::factory()->canViewReports()->create())
->delete($this->getRoute($reportTemplate)) ->delete($this->getRoute($reportTemplate))
->assertSessionHas('error') ->assertNotFound();
->assertRedirect(route('reports/custom'));
$this->assertModelExists($reportTemplate); $this->assertModelExists($reportTemplate);
} }

View file

@ -15,7 +15,7 @@ class EditReportTemplateTest extends TestCase implements TestsPermissionsRequire
{ {
$this->actingAs(User::factory()->create()) $this->actingAs(User::factory()->create())
->get($this->getRoute(ReportTemplate::factory()->create())) ->get($this->getRoute(ReportTemplate::factory()->create()))
->assertForbidden(); ->assertNotFound();
} }
public function testCannotLoadEditPageForAnotherUsersReportTemplate() public function testCannotLoadEditPageForAnotherUsersReportTemplate()
@ -25,8 +25,7 @@ class EditReportTemplateTest extends TestCase implements TestsPermissionsRequire
$this->actingAs($user) $this->actingAs($user)
->get($this->getRoute($reportTemplate)) ->get($this->getRoute($reportTemplate))
->assertSessionHas('error') ->assertNotFound();
->assertRedirect(route('reports/custom'));
} }
public function testCanLoadEditReportTemplatePage() public function testCanLoadEditReportTemplatePage()

View file

@ -15,7 +15,7 @@ class ShowReportTemplateTest extends TestCase implements TestsPermissionsRequire
{ {
$this->actingAs(User::factory()->create()) $this->actingAs(User::factory()->create())
->get($this->getRoute(ReportTemplate::factory()->create())) ->get($this->getRoute(ReportTemplate::factory()->create()))
->assertForbidden(); ->assertNotFound();
} }
public function testCanLoadASavedReportTemplate() public function testCanLoadASavedReportTemplate()
@ -38,8 +38,7 @@ class ShowReportTemplateTest extends TestCase implements TestsPermissionsRequire
$this->actingAs(User::factory()->canViewReports()->create()) $this->actingAs(User::factory()->canViewReports()->create())
->get($this->getRoute($reportTemplate)) ->get($this->getRoute($reportTemplate))
->assertSessionHas('error') ->assertNotFound();
->assertRedirect(route('reports/custom'));
} }
private function getRoute(ReportTemplate $reportTemplate): string private function getRoute(ReportTemplate $reportTemplate): string

View file

@ -15,15 +15,14 @@ class UpdateReportTemplateTest extends TestCase implements TestsPermissionsRequi
{ {
$this->actingAs(User::factory()->create()) $this->actingAs(User::factory()->create())
->post($this->getRoute(ReportTemplate::factory()->create())) ->post($this->getRoute(ReportTemplate::factory()->create()))
->assertForbidden(); ->assertNotFound();
} }
public function testCannotUpdateAnotherUsersReportTemplate() public function testCannotUpdateAnotherUsersReportTemplate()
{ {
$this->actingAs(User::factory()->canViewReports()->create()) $this->actingAs(User::factory()->canViewReports()->create())
->post($this->getRoute(ReportTemplate::factory()->create())) ->post($this->getRoute(ReportTemplate::factory()->create()))
->assertSessionHas('error') ->assertNotFound();
->assertRedirect(route('reports/custom'));
} }
public function testCanUpdateAReportTemplate() public function testCanUpdateAReportTemplate()