adrema/app/Course/Requests/StoreRequest.php

50 lines
1.3 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;
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',
];
}
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
2022-02-19 18:06:07 +01:00
$namiId = $settings->login()->createCourse($member->nami_id, $payload);
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
}
}