diff --git a/app/Course/Controllers/CourseController.php b/app/Course/Controllers/CourseController.php index c41bc381..c6a510df 100644 --- a/app/Course/Controllers/CourseController.php +++ b/app/Course/Controllers/CourseController.php @@ -3,6 +3,7 @@ namespace App\Course\Controllers; use App\Course\Models\CourseMember; +use App\Course\Requests\DestroyRequest; use App\Course\Requests\StoreRequest; use App\Course\Requests\UpdateRequest; use App\Http\Controllers\Controller; @@ -25,4 +26,11 @@ class CourseController extends Controller return redirect()->route('member.index'); } + + public function destroy(Member $member, CourseMember $course, DestroyRequest $request): RedirectResponse + { + $request->persist($member, $course); + + return redirect()->route('member.index'); + } } diff --git a/app/Course/Requests/DestroyRequest.php b/app/Course/Requests/DestroyRequest.php new file mode 100644 index 00000000..74613780 --- /dev/null +++ b/app/Course/Requests/DestroyRequest.php @@ -0,0 +1,45 @@ + + */ + public function rules() + { + return []; + } + + public function persist(Member $member, CourseMember $course): void + { + try { + auth()->user()->api()->deleteCourse($member->nami_id, $course->nami_id); + } catch(NamiException $e) { + throw ValidationException::withMessages(['id' => 'Unbekannter Fehler']); + } + + $course->delete(); + } +} diff --git a/packages/laravel-nami b/packages/laravel-nami index 64ce1491..d348aea5 160000 --- a/packages/laravel-nami +++ b/packages/laravel-nami @@ -1 +1 @@ -Subproject commit 64ce14910a9b43004ddb6561c64ea909bb119cee +Subproject commit d348aea5f6bba6cf3eb8e2050a738817f281b4c3 diff --git a/tests/Feature/Course/DeleteTest.php b/tests/Feature/Course/DeleteTest.php new file mode 100644 index 00000000..94049e76 --- /dev/null +++ b/tests/Feature/Course/DeleteTest.php @@ -0,0 +1,46 @@ +withoutExceptionHandling(); + $this->login()->init(); + app(CourseFake::class)->deleteSuccessful(123, 999); + $member = Member::factory()->defaults()->inNami(123)->has(CourseMember::factory()->inNami(999)->for(Course::factory()), 'courses')->createOne(); + + $response = $this->delete("/member/{$member->id}/course/{$member->courses->first()->id}"); + + $response->assertRedirect("/member"); + $this->assertDatabaseCount('course_members', 0); + app(CourseFake::class)->assertDeleted(123, 999); + } + + public function testItReceivesUnknownErrors(): void + { + $this->login()->init(); + app(CourseFake::class)->deleteFailed(123, 999); + $member = Member::factory()->defaults()->inNami(123)->has(CourseMember::factory()->inNami(999)->for(Course::factory()), 'courses')->createOne(); + + $response = $this->delete("/member/{$member->id}/course/{$member->courses->first()->id}"); + + $this->assertDatabaseCount('course_members', 1); + + $response->assertSessionHasErrors(['id' => 'Unbekannter Fehler']); + } + +}