You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.8 KiB
91 lines
2.8 KiB
|
2 weeks ago
|
<?php
|
||
|
|
|
||
|
|
namespace App\Http\Controllers\Admin;
|
||
|
|
|
||
|
|
use App\Http\Controllers\Controller;
|
||
|
|
use App\Models\Course;
|
||
|
|
use App\Models\CourseSession;
|
||
|
|
use App\Support\ApiResponse;
|
||
|
|
use Illuminate\Http\JsonResponse;
|
||
|
|
use Illuminate\Http\Request;
|
||
|
|
|
||
|
|
class CourseSessionController extends Controller
|
||
|
|
{
|
||
|
|
use ApiResponse;
|
||
|
|
|
||
|
|
public function index(int $course): JsonResponse
|
||
|
|
{
|
||
|
|
Course::query()->findOrFail($course);
|
||
|
|
$items = CourseSession::query()
|
||
|
|
->where('course_id', $course)
|
||
|
|
->orderBy('sort')
|
||
|
|
->orderBy('id')
|
||
|
|
->get()
|
||
|
|
->map(fn (CourseSession $s) => $this->serialize($s));
|
||
|
|
|
||
|
|
return $this->ok(['items' => $items]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function store(Request $request, int $course): JsonResponse
|
||
|
|
{
|
||
|
|
Course::query()->findOrFail($course);
|
||
|
|
$data = $request->validate([
|
||
|
|
'title' => ['nullable', 'string', 'max:255'],
|
||
|
|
'starts_at' => ['nullable', 'date'],
|
||
|
|
'ends_at' => ['nullable', 'date', 'after_or_equal:starts_at'],
|
||
|
|
'venue' => ['nullable', 'string', 'max:255'],
|
||
|
|
'capacity' => ['nullable', 'integer', 'min:0'],
|
||
|
|
'sort' => ['nullable', 'integer'],
|
||
|
|
]);
|
||
|
|
|
||
|
|
$session = CourseSession::query()->create([
|
||
|
|
'course_id' => $course,
|
||
|
|
...$data,
|
||
|
|
]);
|
||
|
|
|
||
|
|
return $this->ok(['id' => $session->id], '已创建');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function update(Request $request, int $course, int $session): JsonResponse
|
||
|
|
{
|
||
|
|
$model = CourseSession::query()->where('course_id', $course)->findOrFail($session);
|
||
|
|
$data = $request->validate([
|
||
|
|
'title' => ['nullable', 'string', 'max:255'],
|
||
|
|
'starts_at' => ['nullable', 'date'],
|
||
|
|
'ends_at' => ['nullable', 'date'],
|
||
|
|
'venue' => ['nullable', 'string', 'max:255'],
|
||
|
|
'capacity' => ['nullable', 'integer', 'min:0'],
|
||
|
|
'sort' => ['nullable', 'integer'],
|
||
|
|
]);
|
||
|
|
$model->fill($data);
|
||
|
|
$model->save();
|
||
|
|
|
||
|
|
return $this->ok(null, '已保存');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function destroy(int $course, int $session): JsonResponse
|
||
|
|
{
|
||
|
|
$model = CourseSession::query()->where('course_id', $course)->findOrFail($session);
|
||
|
|
$model->delete();
|
||
|
|
|
||
|
|
return $this->ok(null, '已删除');
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @return array<string, mixed>
|
||
|
|
*/
|
||
|
|
protected function serialize(CourseSession $s): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'id' => $s->id,
|
||
|
|
'course_id' => $s->course_id,
|
||
|
|
'title' => $s->title,
|
||
|
|
'starts_at' => $s->starts_at?->toIso8601String(),
|
||
|
|
'ends_at' => $s->ends_at?->toIso8601String(),
|
||
|
|
'venue' => $s->venue,
|
||
|
|
'capacity' => $s->capacity !== null ? (int) $s->capacity : null,
|
||
|
|
'sort' => (int) $s->sort,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
}
|