forked from thegivehub/app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument-api.php
More file actions
163 lines (139 loc) · 5.02 KB
/
Copy pathdocument-api.php
File metadata and controls
163 lines (139 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
// document-api.php
require_once __DIR__ . '/lib/autoload.php';
require_once __DIR__ . '/lib/Document.php';
require_once __DIR__ . '/lib/Documents.php';
// Parse the request
$method = $_SERVER['REQUEST_METHOD'];
$action = isset($_GET['action']) ? $_GET['action'] : 'upload';
// Initialize document uploader
$uploader = new Document();
$documents = new Documents();
// Handle CORS
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
exit(0);
}
// Route the request based on action and method
switch ($action) {
case 'create':
if ($method === 'POST') {
// Handle document creation without file upload
$data = json_decode(file_get_contents('php://input'), true);
if (!$data) {
sendJson(400, ['error' => 'Invalid JSON data']);
}
// Use Documents class create method to ensure all required fields are set
$result = $documents->create(null, $data);
sendJson(
$result['success'] ? 200 : 400,
$result
);
} else {
sendJson(405, ['error' => 'Method not allowed']);
}
break;
case 'upload':
if ($method === 'POST') {
// Handle document upload
if (!isset($_FILES['document'])) {
sendJson(400, ['error' => 'No document file provided']);
}
$type = $_POST['type'] ?? '';
if (empty($type)) {
sendJson(400, ['error' => 'Document type is required']);
}
$description = $_POST['description'] ?? '';
$result = $uploader->uploadDocument($_FILES['document'], $type, $description);
sendJson(
$result['success'] ? 200 : 400,
$result
);
} else {
sendJson(405, ['error' => 'Method not allowed']);
}
break;
case 'list':
if ($method === 'GET') {
// Get user documents
$type = $_GET['type'] ?? null;
$userId = $_GET['userId'] ?? null;
$result = $uploader->getUserDocuments($userId, $type);
sendJson(
$result['success'] ? 200 : ($userId ? 404 : 401),
$result
);
} else {
sendJson(405, ['error' => 'Method not allowed']);
}
break;
case 'get':
if ($method === 'GET') {
// Get document details
$documentId = $_GET['id'] ?? null;
if (!$documentId) {
sendJson(400, ['error' => 'Document ID is required']);
}
$result = $uploader->getDocumentDetails($documentId);
sendJson(
$result['success'] ? 200 : ($result['error'] === 'Document not found' ? 404 : 403),
$result
);
} else {
sendJson(405, ['error' => 'Method not allowed']);
}
break;
case 'verify':
if ($method === 'PUT') {
// Verify a document
$data = json_decode(file_get_contents('php://input'), true);
$documentId = $_GET['id'] ?? $data['documentId'] ?? null;
$status = $data['status'] ?? null;
$notes = $data['notes'] ?? '';
if (!$documentId) {
sendJson(400, ['error' => 'Document ID is required']);
}
if (!$status || !in_array($status, ['approved', 'rejected'])) {
sendJson(400, ['error' => 'Valid status (approved or rejected) is required']);
}
$result = $uploader->verifyDocument($documentId, $status, $notes);
sendJson(
$result['success'] ? 200 : 403,
$result
);
} else {
sendJson(405, ['error' => 'Method not allowed']);
}
break;
case 'delete':
if ($method === 'DELETE') {
// Delete a document
$documentId = $_GET['id'] ?? null;
if (!$documentId) {
sendJson(400, ['error' => 'Document ID is required']);
}
$result = $uploader->deleteDocument($documentId);
sendJson(
$result['success'] ? 200 : 403,
$result
);
} else {
sendJson(405, ['error' => 'Method not allowed']);
}
break;
default:
sendJson(404, ['error' => 'Action not found']);
}
/**
* Send a JSON response
* @param int $statusCode HTTP status code
* @param array $data Response data
*/
function sendJson($statusCode, $data) {
http_response_code($statusCode);
header('Content-Type: application/json');
echo json_encode($data);
exit;
}