Add: Delete course

This commit is contained in:
philipp lang 2021-11-20 01:09:28 +01:00
parent 986a5103df
commit bcc5d7af4b
4 changed files with 100 additions and 1 deletions

View File

@ -3,6 +3,7 @@
namespace App\Course\Controllers; namespace App\Course\Controllers;
use App\Course\Models\CourseMember; use App\Course\Models\CourseMember;
use App\Course\Requests\DestroyRequest;
use App\Course\Requests\StoreRequest; use App\Course\Requests\StoreRequest;
use App\Course\Requests\UpdateRequest; use App\Course\Requests\UpdateRequest;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
@ -25,4 +26,11 @@ class CourseController extends Controller
return redirect()->route('member.index'); return redirect()->route('member.index');
} }
public function destroy(Member $member, CourseMember $course, DestroyRequest $request): RedirectResponse
{
$request->persist($member, $course);
return redirect()->route('member.index');
}
} }

View File

@ -0,0 +1,45 @@
<?php
namespace App\Course\Requests;
use App\Course\Models\Course;
use App\Course\Models\CourseMember;
use App\Member\Member;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Arr;
use Illuminate\Validation\ValidationException;
use Zoomyboy\LaravelNami\NamiException;
class DestroyRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, string>
*/
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();
}
}

@ -1 +1 @@
Subproject commit 64ce14910a9b43004ddb6561c64ea909bb119cee Subproject commit d348aea5f6bba6cf3eb8e2050a738817f281b4c3

View File

@ -0,0 +1,46 @@
<?php
namespace Tests\Feature\Course;
use App\Course\Models\Course;
use App\Course\Models\CourseMember;
use App\Member\Member;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Zoomyboy\LaravelNami\Backend\FakeBackend;
use Zoomyboy\LaravelNami\Fakes\CourseFake;
class DeleteTest extends TestCase
{
use RefreshDatabase;
public function testItDeletesACourse(): void
{
$this->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']);
}
}