add projet
This commit is contained in:
+107
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Models\Configuration;
|
||||
use App\Models\LateSlipLog;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class LateSlipLogsController extends BaseApiController
|
||||
{
|
||||
protected LateSlipLog $log;
|
||||
protected Configuration $config;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->log = model(LateSlipLog::class);
|
||||
$this->config = model(Configuration::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/v1/late-slip-logs
|
||||
* Get paginated late slip logs with optional filters
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$defaultYear = (string) ($this->config->getConfig('school_year') ?? '');
|
||||
$defaultSem = (string) ($this->config->getConfig('semester') ?? '');
|
||||
|
||||
$schoolYear = trim((string) ($this->request->getGet('school_year') ?? $defaultYear));
|
||||
$semester = trim((string) ($this->request->getGet('semester') ?? $defaultSem));
|
||||
$q = trim((string) ($this->request->getGet('q') ?? ''));
|
||||
$dateFrom = trim((string) ($this->request->getGet('date_from') ?? ''));
|
||||
$dateTo = trim((string) ($this->request->getGet('date_to') ?? ''));
|
||||
|
||||
$page = max(1, (int) ($this->request->getGet('page') ?? 1));
|
||||
$perPage = min(200, max(1, (int) ($this->request->getGet('per_page') ?? 20)));
|
||||
|
||||
// Normalize dates to Y-m-d for filtering
|
||||
$toDbDate = static function (?string $s): ?string {
|
||||
$s = trim((string) $s);
|
||||
if ($s === '') {
|
||||
return null;
|
||||
}
|
||||
$ts = strtotime($s);
|
||||
return $ts ? date('Y-m-d', $ts) : null;
|
||||
};
|
||||
|
||||
$df = $toDbDate($dateFrom);
|
||||
$dt = $toDbDate($dateTo);
|
||||
|
||||
try {
|
||||
$query = $this->log->newQuery();
|
||||
|
||||
if ($schoolYear !== '') {
|
||||
$query->where('school_year', $schoolYear);
|
||||
}
|
||||
|
||||
if ($semester !== '') {
|
||||
$query->where('semester', $semester);
|
||||
}
|
||||
|
||||
if ($q !== '') {
|
||||
$query->like('student_name', $q);
|
||||
}
|
||||
|
||||
if ($df) {
|
||||
$query->where('slip_date >=', $df);
|
||||
}
|
||||
|
||||
if ($dt) {
|
||||
$query->where('slip_date <=', $dt);
|
||||
}
|
||||
|
||||
$result = $this->paginate($query->orderBy('id', 'DESC'), $page, $perPage);
|
||||
|
||||
return $this->success([
|
||||
'logs' => $result['data'],
|
||||
'pagination' => $result['pagination'],
|
||||
'filters' => [
|
||||
'school_year' => $schoolYear,
|
||||
'semester' => $semester,
|
||||
'q' => $q,
|
||||
'date_from' => $dateFrom,
|
||||
'date_to' => $dateTo,
|
||||
],
|
||||
], 'Late slip logs retrieved successfully');
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'Late slip logs index error: ' . $e->getMessage());
|
||||
return $this->respondError('Failed to retrieve late slip logs', Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/v1/late-slip-logs/{id}
|
||||
* Get a single late slip log by ID
|
||||
*/
|
||||
public function show($id = null)
|
||||
{
|
||||
$log = $this->log->find($id);
|
||||
if (!$log) {
|
||||
return $this->error('Late slip log not found', Response::HTTP_NOT_FOUND);
|
||||
}
|
||||
|
||||
return $this->success($log, 'Late slip log retrieved successfully');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user