HEX
Server: Apache/2.4.58 (Ubuntu)
System: Linux Bradford-Sitios 6.14.0-1017-azure #17~24.04.1-Ubuntu SMP Mon Dec 1 20:10:50 UTC 2025 x86_64
User: www-data (33)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/matriculas_api_dev/app/Http/Controllers/StudentController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\Student\StoreStudentRequest;
use App\Http\Requests\Student\UpdateStudentRequest;
use App\Models\StudentPeriod;
use App\Services\StudentService;
use Exception;

class StudentController extends Controller
{

    protected $studentService;
    public function __construct(StudentService $studentService)
    {
        $this->studentService = $studentService;
    }

    public function list(Request $request)
    {
        try {
            $parentId = $request->query('parentId');
            $data = $this->studentService->list($parentId);
            return genericResponse(['data' =>  $data, 'message' => 'ok', 'code' => 200], 200);
        } catch (Exception $e) {
            $errorCode = $e->getCode();
            return genericResponse(['error' => $e->getMessage()], $errorCode ? $errorCode : 500);
        }
    }

    public function store(StoreStudentRequest $request)
    {
        try {
            $data = $this->studentService->store($request);
            return genericResponse(['data' =>  $data, 'message' => 'Estudiante Creado Correctamente', 'code' => 201], 201);
        } catch (Exception $e) {
            $errorCode = $e->getCode();
            return genericResponse(['error' => $e->getMessage()], $errorCode ? $errorCode : 500);
        }
    }

    public function update($id, UpdateStudentRequest $request)
    {
        try {
            $data = $this->studentService->update($request, $id);
            return genericResponse(['data' =>  null, 'message' => null, 'code' => 204], 204);
        } catch (Exception $e) {
            $errorCode = $e->getCode();
            return genericResponse(['error' => $e->getMessage()], $errorCode ? $errorCode : 500);
        }
    }

    public function show($id)
    {
        try {
            $data = $this->studentService->show($id);
            return genericResponse(['data' =>  $data, 'message' => 'ok', 'code' => 200], 200);
        } catch (Exception $e) {
            $errorCode = $e->getCode();
            return genericResponse(['error' => $e->getMessage()], $errorCode ? $errorCode : 500);
        }
    }

    public function setSiblingOrderOverride($studentId, $periodId, Request $request)
    {
        try {
            $value = $request->input('sibling_order_override');
            $override = $value !== null && $value !== '' ? (int) $value : null;

            if ($override !== null && ($override < 1 || $override > 10)) {
                return genericResponse(['error' => 'El valor debe estar entre 1 y 10'], 422);
            }

            $updated = StudentPeriod::where('student_id', $studentId)
                ->where('period_id', $periodId)
                ->update(['sibling_order_override' => $override]);

            if (!$updated) {
                return genericResponse(['error' => 'No se encontrĂ³ el estudiante en el perĂ­odo indicado'], 404);
            }

            return genericResponse(['data' => null, 'message' => 'Override actualizado', 'code' => 200], 200);
        } catch (Exception $e) {
            return genericResponse(['error' => $e->getMessage()], 500);
        }
    }

    public function destroy($id)
    {
        try {
            $data = $this->studentService->delete($id);
            return genericResponse(['data' =>  null, 'message' => 'Estudiante Eliminado correctamente', 'code' => 200], 200);
        } catch (Exception $e) {
            $errorCode = $e->getCode();
            return genericResponse(['error' => $e->getMessage()], $errorCode ? $errorCode : 500);
        }
    }

}