adrema/app/Course/Requests/StoreRequest.php

60 lines
1.6 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;
2022-02-19 15:18:24 +01:00
use App\Setting\NamiSettings;
2021-11-19 22:58:27 +01:00
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Arr;
2021-11-19 23:14:18 +01:00
use Illuminate\Validation\ValidationException;
2022-02-19 15:18:24 +01:00
use Zoomyboy\LaravelNami\Nami;
2021-11-19 23:14:18 +01:00
use Zoomyboy\LaravelNami\NamiException;
2021-11-19 22:58:27 +01:00
class StoreRequest extends FormRequest
{
2022-02-19 15:18:24 +01:00
2021-11-19 22:58:27 +01:00
/**
* 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',
];
}
2022-02-19 15:18:24 +01:00
public function persist(Member $member, NamiSettings $settings): void
2021-11-19 22:58:27 +01:00
{
2021-11-19 23:14:18 +01:00
$course = Course::where('id', $this->input('course_id'))->firstOrFail();
2022-02-19 15:18:24 +01:00
$payload = collect($this->input())->only(['event_name', 'completed_at', 'organizer'])->merge([
'course_id' => $course->nami_id,
])->toArray();
2021-11-19 23:14:18 +01:00
try {
2022-02-19 15:18:24 +01:00
$namiId = Nami::login($settings->mglnr, $settings->password)->createCourse($member->nami_id, $payload);
2021-11-19 23:14:18 +01:00
} 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
}
2022-02-19 15:18:24 +01:00
2021-11-19 22:58:27 +01:00
}