From bcc5d7af4b5eaf522a40f8f4d9b746c15a7d9a6b Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sat, 20 Nov 2021 01:09:28 +0100 Subject: [PATCH] Add: Delete course --- app/Course/Controllers/CourseController.php | 8 ++++ app/Course/Requests/DestroyRequest.php | 45 ++++++++++++++++++++ packages/laravel-nami | 2 +- tests/Feature/Course/DeleteTest.php | 46 +++++++++++++++++++++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 app/Course/Requests/DestroyRequest.php create mode 100644 tests/Feature/Course/DeleteTest.php 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']); + } + +}