79 lines
2.6 KiB
PHP
79 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Assignment;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Resources\Assignment\AssignmentOverviewResource;
|
|
use App\Http\Resources\Assignment\AssignmentSectionResource;
|
|
use App\Services\Assignment\AssignmentService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class AssignmentApiController extends Controller
|
|
{
|
|
public function __construct(
|
|
protected AssignmentService $assignmentService
|
|
) {}
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$data = $this->assignmentService->getAssignmentsOverview(
|
|
schoolYear: $request->query('school_year'),
|
|
semester: $request->query('semester'),
|
|
);
|
|
|
|
return response()->json([
|
|
'message' => 'Assignments retrieved successfully.',
|
|
'data' => new AssignmentOverviewResource((object) $data),
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'student_id' => ['required', 'integer', 'exists:students,id'],
|
|
'class_section_id' => ['required', 'integer', 'exists:classSection,class_section_id'],
|
|
'semester' => ['required', 'string', 'max:50'],
|
|
'school_year' => ['required', 'string', 'max:50'],
|
|
'description' => ['nullable', 'string'],
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json([
|
|
'message' => 'Validation failed.',
|
|
'errors' => $validator->errors(),
|
|
], 422);
|
|
}
|
|
|
|
$userId = (int) ($request->user()?->id ?? 0);
|
|
if ($userId <= 0) {
|
|
return response()->json(['message' => 'Unauthorized.'], 401);
|
|
}
|
|
|
|
$assignment = $this->assignmentService->storeAssignment(
|
|
data: $validator->validated(),
|
|
updatedBy: $userId
|
|
);
|
|
|
|
return response()->json([
|
|
'message' => 'Assignment saved successfully.',
|
|
'data' => $assignment,
|
|
], 201);
|
|
}
|
|
|
|
public function classAssignmentData(): JsonResponse
|
|
{
|
|
$data = $this->assignmentService->getClassAssignmentData();
|
|
|
|
return response()->json([
|
|
'message' => 'Class assignment data retrieved successfully.',
|
|
'data' => [
|
|
'classSections' => AssignmentSectionResource::collection(collect($data['classSections'])),
|
|
'semester' => $data['semester'],
|
|
'school_year' => $data['school_year'],
|
|
],
|
|
]);
|
|
}
|
|
}
|