adrema/app/Course/Requests/StoreRequest.php

56 lines
1.5 KiB
PHP
Raw Normal View History

2021-11-19 22:58:27 +01:00
<?php
namespace App\Course\Requests;
use App\Course\Models\Course;
use App\Member\Member;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Arr;
2021-11-19 23:14:18 +01:00
use Illuminate\Validation\ValidationException;
use Zoomyboy\LaravelNami\NamiException;
2021-11-19 22:58:27 +01:00
class StoreRequest 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 [
'organizer' => 'required|max:255',
'event_name' => 'required|max:255',
'completed_at' => 'required|date',
'course_id' => 'required|exists:courses,id',
];
}
public function persist(Member $member): void
{
2021-11-19 23:14:18 +01:00
$course = Course::where('id', $this->input('course_id'))->firstOrFail();
2021-11-19 22:58:27 +01:00
$payload = array_merge(
2021-11-20 00:48:42 +01:00
$this->only(['event_name', 'completed_at', 'organizer']),
2021-11-19 22:58:27 +01:00
['course_id' => $course->nami_id],
);
2021-11-19 23:14:18 +01:00
try {
$namiId = auth()->user()->api()->createCourse($member->nami_id, $payload);
} catch(NamiException $e) {
throw ValidationException::withMessages(['id' => 'Unbekannter Fehler']);
}
2021-11-19 22:58:27 +01:00
2021-11-20 00:48:42 +01:00
$member->courses()->create($this->safe()->collect()->put('nami_id', $namiId)->toArray());
2021-11-19 22:58:27 +01:00
}
}