Skip to content

Commit cafaba4

Browse files
committed
Add attendance and HR controllers/models + minor fixes
1 parent 606de9e commit cafaba4

26 files changed

+948
-46
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
use App\Core\Controller;
6+
use App\Core\Response;
7+
use App\Models\Attendance;
8+
9+
class AttendanceController extends Controller
10+
{
11+
public function index()
12+
{
13+
$perPage = $this->request->get('per_page', 10);
14+
$page = $this->request->get('page', 1);
15+
$attendanceRecords = Attendance::paginate($perPage, $page);
16+
return Response::json($attendanceRecords);
17+
}
18+
19+
public function show($id)
20+
{
21+
$attendance = Attendance::find($id);
22+
if (!$attendance) {
23+
return Response::json(['error' => 'Attendance record not found'], 404);
24+
}
25+
return Response::json($attendance);
26+
}
27+
28+
public function store()
29+
{
30+
$data = $this->request->json();
31+
$attendance = new Attendance();
32+
$attendance->employee_id = $data['employee_id'] ?? null;
33+
$attendance->date = $data['date'] ?? null;
34+
$attendance->check_in_time = $data['check_in_time'] ?? null;
35+
$attendance->check_out_time = $data['check_out_time'] ?? null;
36+
$attendance->status = $data['status'] ?? 'present';
37+
38+
if ($attendance->save()) {
39+
return Response::json(['message' => 'Attendance record created successfully', 'attendance' => $attendance], 201);
40+
}
41+
return Response::json(['error' => 'Failed to create attendance record'], 500);
42+
}
43+
44+
public function update($id)
45+
{
46+
$attendance = Attendance::find($id);
47+
if (!$attendance) {
48+
return Response::json(['error' => 'Attendance record not found'], 404);
49+
}
50+
51+
$data = $this->request->json();
52+
$attendance->employee_id = $data['employee_id'] ?? $attendance->employee_id;
53+
$attendance->date = $data['date'] ?? $attendance->date;
54+
$attendance->check_in_time = $data['check_in_time'] ?? $attendance->check_in_time;
55+
$attendance->check_out_time = $data['check_out_time'] ?? $attendance->check_out_time;
56+
$attendance->status = $data['status'] ?? $attendance->status;
57+
58+
if ($attendance->save()) {
59+
return Response::json(['message' => 'Attendance record updated successfully', 'attendance' => $attendance]);
60+
}
61+
return Response::json(['error' => 'Failed to update attendance record'], 500);
62+
}
63+
64+
public function destroy($id)
65+
{
66+
$attendance = Attendance::find($id);
67+
if (!$attendance) {
68+
return Response::json(['error' => 'Attendance record not found'], 404);
69+
}
70+
71+
if ($attendance->delete()) {
72+
return Response::json(['message' => 'Attendance record deleted successfully']);
73+
}
74+
return Response::json(['error' => 'Failed to delete attendance record'], 500);
75+
}
76+
}

app/controllers/AuthController.php

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
use App\Core\Controller;
66
use App\Core\Response;
7+
use App\Core\Request;
8+
use App\Core\Application;
79
use App\Services\AuthService;
10+
use App\Helpers\Validator;
11+
use Exception;
812

913
class AuthController extends Controller
1014
{
@@ -15,43 +19,67 @@ public function __construct()
1519
$this->authService = new AuthService();
1620
}
1721

18-
public function login()
22+
public function login(Request $request): Response
1923
{
20-
$data = $this->getRequestData();
21-
22-
if (!isset($data['email']) || !isset($data['password'])) {
23-
return Response::json(['error' => 'Email and password required'], 400);
24+
$data = $request->json();
25+
26+
$validator = new Validator($data);
27+
$validator->validate([
28+
'email' => ['required' => true, 'email' => true],
29+
'password' => ['required' => true, 'min' => 6],
30+
]);
31+
32+
if (!$validator->isValid()) {
33+
return Response::json(['errors' => $validator->errors()], 422);
2434
}
2535

26-
$result = $this->authService->login($data['email'], $data['password']);
27-
28-
if ($result) {
29-
return Response::json($result);
36+
try {
37+
$result = $this->authService->login($data['email'], $data['password']);
38+
if ($result) {
39+
return Response::json($result);
40+
}
41+
return Response::json(['error' => 'Invalid credentials'], 401);
42+
} catch (Exception $e) {
43+
Application::logger()->error("Login error: " . $e->getMessage());
44+
return Response::json(['error' => 'An unexpected error occurred during login.'], 500);
3045
}
31-
32-
return Response::json(['error' => 'Invalid credentials'], 401);
3346
}
3447

35-
public function register()
48+
public function register(Request $request): Response
3649
{
37-
$data = $this->getRequestData();
38-
39-
if (!isset($data['name']) || !isset($data['email']) || !isset($data['password'])) {
40-
return Response::json(['error' => 'Name, email and password required'], 400);
50+
$data = $request->json();
51+
52+
$validator = new Validator($data);
53+
$validator->validate([
54+
'name' => ['required' => true, 'min' => 3],
55+
'email' => ['required' => true, 'email' => true],
56+
'password' => ['required' => true, 'min' => 6],
57+
]);
58+
59+
if (!$validator->isValid()) {
60+
return Response::json(['errors' => $validator->errors()], 422);
4161
}
4262

43-
$result = $this->authService->register($data);
44-
45-
if ($result) {
46-
return Response::json($result, 201);
63+
try {
64+
$result = $this->authService->register($data);
65+
if ($result) {
66+
return Response::json($result, 201);
67+
}
68+
return Response::json(['error' => 'Registration failed. Email may already be in use.'], 409);
69+
} catch (Exception $e) {
70+
Application::logger()->error("Registration error: " . $e->getMessage());
71+
return Response::json(['error' => 'An unexpected error occurred during registration.'], 500);
4772
}
48-
49-
return Response::json(['error' => 'Registration failed'], 400);
5073
}
5174

52-
public function logout()
75+
public function logout(Request $request)
5376
{
54-
$token = $this->getBearerToken();
77+
$authHeader = $request->getHeader('Authorization');
78+
$token = null;
79+
80+
if ($authHeader && strpos($authHeader, 'Bearer ') === 0) {
81+
$token = substr($authHeader, 7);
82+
}
5583

5684
if ($token) {
5785
$this->authService->logout($token);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
use App\Core\Controller;
6+
use App\Core\Response;
7+
use App\Models\Department;
8+
9+
class DepartmentController extends Controller
10+
{
11+
public function index()
12+
{
13+
$perPage = $this->request->get('per_page', 10);
14+
$page = $this->request->get('page', 1);
15+
$departments = Department::paginate($perPage, $page);
16+
return Response::json($departments);
17+
}
18+
19+
public function show($id)
20+
{
21+
$department = Department::find($id);
22+
if (!$department) {
23+
return Response::json(['error' => 'Department not found'], 404);
24+
}
25+
return Response::json($department);
26+
}
27+
28+
public function store()
29+
{
30+
$data = $this->request->json();
31+
$department = new Department();
32+
$department->name = $data['name'] ?? null;
33+
$department->description = $data['description'] ?? null;
34+
35+
if ($department->save()) {
36+
return Response::json(['message' => 'Department created successfully', 'department' => $department], 201);
37+
}
38+
return Response::json(['error' => 'Failed to create department'], 500);
39+
}
40+
41+
public function update($id)
42+
{
43+
$department = Department::find($id);
44+
if (!$department) {
45+
return Response::json(['error' => 'Department not found'], 404);
46+
}
47+
48+
$data = $this->request->json();
49+
$department->name = $data['name'] ?? $department->name;
50+
$department->description = $data['description'] ?? $department->description;
51+
52+
if ($department->save()) {
53+
return Response::json(['message' => 'Department updated successfully', 'department' => $department]);
54+
}
55+
return Response::json(['error' => 'Failed to update department'], 500);
56+
}
57+
58+
public function destroy($id)
59+
{
60+
$department = Department::find($id);
61+
if (!$department) {
62+
return Response::json(['error' => 'Department not found'], 404);
63+
}
64+
65+
if ($department->delete()) {
66+
return Response::json(['message' => 'Department deleted successfully']);
67+
}
68+
return Response::json(['error' => 'Failed to delete department'], 500);
69+
}
70+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
namespace App\Controllers;
4+
5+
use App\Core\Controller;
6+
use App\Core\Response;
7+
use App\Models\EmployeeProfile;
8+
9+
class EmployeeProfileController extends Controller
10+
{
11+
public function index()
12+
{
13+
$perPage = $this->request->get('per_page', 10);
14+
$page = $this->request->get('page', 1);
15+
$employeeProfiles = EmployeeProfile::paginate($perPage, $page);
16+
return Response::json($employeeProfiles);
17+
}
18+
19+
public function show($id)
20+
{
21+
$employeeProfile = EmployeeProfile::find($id);
22+
if (!$employeeProfile) {
23+
return Response::json(['error' => 'Employee Profile not found'], 404);
24+
}
25+
return Response::json($employeeProfile);
26+
}
27+
28+
public function store()
29+
{
30+
$data = $this->request->json();
31+
$employeeProfile = new EmployeeProfile();
32+
$employeeProfile->user_id = $data['user_id'] ?? null;
33+
$employeeProfile->department_id = $data['department_id'] ?? null;
34+
$employeeProfile->position = $data['position'] ?? null;
35+
$employeeProfile->hire_date = $data['hire_date'] ?? null;
36+
$employeeProfile->salary = $data['salary'] ?? null;
37+
38+
if ($employeeProfile->save()) {
39+
return Response::json(['message' => 'Employee Profile created successfully', 'employee_profile' => $employeeProfile], 201);
40+
}
41+
return Response::json(['error' => 'Failed to create employee profile'], 500);
42+
}
43+
44+
public function update($id)
45+
{
46+
$employeeProfile = EmployeeProfile::find($id);
47+
if (!$employeeProfile) {
48+
return Response::json(['error' => 'Employee Profile not found'], 404);
49+
}
50+
51+
$data = $this->request->json();
52+
$employeeProfile->user_id = $data['user_id'] ?? $employeeProfile->user_id;
53+
$employeeProfile->department_id = $data['department_id'] ?? $employeeProfile->department_id;
54+
$employeeProfile->position = $data['position'] ?? $employeeProfile->position;
55+
$employeeProfile->hire_date = $data['hire_date'] ?? $employeeProfile->hire_date;
56+
$employeeProfile->salary = $data['salary'] ?? $employeeProfile->salary;
57+
58+
if ($employeeProfile->save()) {
59+
return Response::json(['message' => 'Employee Profile updated successfully', 'employee_profile' => $employeeProfile]);
60+
}
61+
return Response::json(['error' => 'Failed to update employee profile'], 500);
62+
}
63+
64+
public function destroy($id)
65+
{
66+
$employeeProfile = EmployeeProfile::find($id);
67+
if (!$employeeProfile) {
68+
return Response::json(['error' => 'Employee Profile not found'], 404);
69+
}
70+
71+
if ($employeeProfile->delete()) {
72+
return Response::json(['message' => 'Employee Profile deleted successfully']);
73+
}
74+
return Response::json(['error' => 'Failed to delete employee profile'], 500);
75+
}
76+
}

app/controllers/FileController.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,30 @@ public function upload()
2222
if ($filepath) {
2323
return Response::json([
2424
'message' => 'File uploaded successfully',
25-
'filepath' => $filepath,
26-
'size' => FileHelper::getSize($filepath)
25+
'file' => FileHelper::getFileInfo($filepath)
2726
]);
2827
}
2928

3029
return Response::json(['error' => 'Upload failed'], 500);
3130
}
3231

32+
public function getFile($id)
33+
{
34+
$filepath = $_GET['filepath'] ?? null;
35+
36+
if (!$filepath) {
37+
return Response::json(['error' => 'Filepath required'], 400);
38+
}
39+
40+
$fileInfo = FileHelper::getFileInfo($filepath);
41+
42+
if ($fileInfo) {
43+
return Response::json($fileInfo);
44+
}
45+
46+
return Response::json(['error' => 'File not found'], 404);
47+
}
48+
3349
public function delete($id)
3450
{
3551
$filepath = $_POST['filepath'] ?? null;

0 commit comments

Comments
 (0)