127 lines
4.0 KiB
PHP
Executable File
127 lines
4.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Models\Student;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class RFIDController extends BaseApiController
|
|
{
|
|
protected User $user;
|
|
protected Student $student;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->user = model(User::class);
|
|
$this->student = model(Student::class);
|
|
}
|
|
|
|
public function process()
|
|
{
|
|
$data = $this->payloadData();
|
|
if (empty($data)) {
|
|
return $this->error('Invalid request data', Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
$rules = [
|
|
'rfid' => 'required',
|
|
];
|
|
|
|
$errors = $this->validateRequest($data, $rules);
|
|
if (!empty($errors)) {
|
|
return $this->respondValidationError($errors);
|
|
}
|
|
|
|
try {
|
|
$rfidTag = $data['rfid'];
|
|
|
|
$user = $this->user->where('rfid_tag', $rfidTag)->first();
|
|
|
|
$student = null;
|
|
if (!$user) {
|
|
$student = $this->student->where('rfid_tag', $rfidTag)->first();
|
|
}
|
|
|
|
if (!$user && !$student) {
|
|
return $this->error('RFID tag not recognized', Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
$logData = [
|
|
'user_id' => $user['id'] ?? null,
|
|
'student_id' => $student['id'] ?? null,
|
|
'card_id' => $rfidTag,
|
|
'scan_time' => Carbon::now('UTC')->toDateTimeString(),
|
|
];
|
|
|
|
DB::table('scan_log')->insert($logData);
|
|
|
|
$result = [
|
|
'rfid_tag' => $rfidTag,
|
|
'user' => $user ? [
|
|
'id' => $user['id'],
|
|
'name' => trim(($user['firstname'] ?? '') . ' ' . ($user['lastname'] ?? '')),
|
|
] : null,
|
|
'student' => $student ? [
|
|
'id' => $student['id'],
|
|
'name' => trim(($student['firstname'] ?? '') . ' ' . ($student['lastname'] ?? '')),
|
|
] : null,
|
|
];
|
|
|
|
return $this->success($result, 'RFID processed successfully');
|
|
} catch (\Throwable $e) {
|
|
log_message('error', 'RFID process error: ' . $e->getMessage());
|
|
return $this->respondError('Failed to process RFID', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
}
|
|
|
|
public function logs()
|
|
{
|
|
$page = max(1, (int) ($this->request->getGet('page') ?? 1));
|
|
$perPage = min(100, max(1, (int) ($this->request->getGet('per_page') ?? 20)));
|
|
|
|
try {
|
|
$builder = DB::table('scan_log as sl')
|
|
->select([
|
|
'u.firstname as user_firstname',
|
|
'u.lastname as user_lastname',
|
|
's.firstname as student_firstname',
|
|
's.lastname as student_lastname',
|
|
'sl.card_id',
|
|
'sl.scan_time',
|
|
])
|
|
->leftJoin('users as u', 'u.id', '=', 'sl.user_id')
|
|
->leftJoin('students as s', 's.rfid_tag', '=', 'sl.card_id');
|
|
|
|
$offset = ($page - 1) * $perPage;
|
|
|
|
$total = (clone $builder)->count();
|
|
|
|
$logs = (clone $builder)
|
|
->orderBy('sl.scan_time', 'DESC')
|
|
->offset($offset)
|
|
->limit($perPage)
|
|
->get()
|
|
->toArray();
|
|
|
|
$result = [
|
|
'data' => $logs,
|
|
'pagination' => [
|
|
'current_page' => $page,
|
|
'per_page' => $perPage,
|
|
'total' => $total,
|
|
'total_pages' => (int) ceil($total / $perPage),
|
|
],
|
|
];
|
|
|
|
return $this->success($result, 'RFID scan logs retrieved successfully');
|
|
} catch (\Throwable $e) {
|
|
log_message('error', 'RFID logs error: ' . $e->getMessage());
|
|
return $this->respondError('Failed to retrieve RFID logs', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
}
|
|
}
|