This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
158 lines (119 loc) · 4.94 KB
/
index.php
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
<?php
require "./vendor/autoload.php";
use App\Controllers\HomeController;
use App\Controllers\AuthController;
use App\Controllers\OrderController;
use App\Controllers\PrintController;
$router = new AltoRouter();
// map homepage
$router->map('GET', '/', function () {
$homeController = new HomeController;
$homeController->home();
});
// map homepage
$router->map('GET', '/print/*', function () {
$homeController = new HomeController;
$hashedDocumentId = filter_var($_GET['doc'], FILTER_SANITIZE_STRING);
$orderId = filter_var($_GET['orderId'], FILTER_VALIDATE_INT);
$ownerId = filter_var($_GET['ownerId'], FILTER_VALIDATE_INT);
if ( md5($orderId . 'yPf.FJY~r)[') !== $hashedDocumentId || !$orderId || !$ownerId) return $homeController->jsonResponse(401, 'Not permitted.');
$printController = new PrintController;
$printController->getOrderPDF($orderId, $ownerId);
});
// map user details page
$router->map('GET', '/user/[i:id]/', function ($id) {
require __DIR__ . '/views/user-details.php';
});
// map login
$router->map('POST', '/af-api/login', function () {
$authController = new AuthController;
$authController->login();
});
// map getOrders with count ..?count=0 (all)
$router->map('GET', '/af-api/[i:ownerId]/get-orders/*', function ($ownerId) {
$authController = new AuthController;
$homeController = new HomeController;
if (!$authController->hasUserPermission()) return $homeController->jsonResponse(401, 'Not permitted.');
$orderController = new OrderController;
$count = filter_var($_GET['count'], FILTER_VALIDATE_INT);
if ($count >= 0) {
$orderList = $orderController->getOrders($ownerId, (int) $count);
if ($orderList) {
$homeController->jsonResponse(200, null, $orderList);
} else {
$homeController->jsonResponse(404, 'Not Found.');
}
} else {
$homeController->jsonResponse(400, 'Bad request.');
}
});
// map get order total price
$router->map('GET', '/af-api/[i:ownerId]/get-order-total-price/*', function ($ownerId) {
$authController = new AuthController;
$homeController = new HomeController;
if (!$authController->hasUserPermission($ownerId)) return $homeController->jsonResponse(401, 'Not permitted.');
$orderController = new OrderController;
$orderId = filter_var($_GET['orderId'], FILTER_VALIDATE_INT);
if ($orderId) $totalPrice = $orderController->getOrderTotalPrice($orderId, $ownerId);
if (is_null($totalPrice)) {
$homeController->jsonResponse(404, 'Order not found.');
} else {
$homeController->jsonResponse(200, null, ['totalPrice' => $totalPrice]);
}
});
// map createNewOrder
$router->map('POST', '/af-api/create-new-order', function () {
$authController = new AuthController;
$homeController = new HomeController;
if (!$authController->hasUserPermission()) return $homeController->jsonResponse(401, 'Not permitted.');
$orderController = new OrderController;
if ($orderController->createNewOrder()) {
$homeController->jsonResponse(201, 'New order was created.');
} else {
$homeController->jsonResponse(400, 'Mistake.');
}
});
$router->map('POST', '/af-api/update-order', function () {
$authController = new AuthController;
$homeController = new HomeController;
if (!$authController->hasUserPermission()) return $homeController->jsonResponse(401, 'Not permitted.');
$orderController = new OrderController;
if ($orderController->updateOrder()) {
$homeController->jsonResponse(200, 'Order was updated.');
} else {
$homeController->jsonResponse(400, 'Mistake.');
}
});
$router->map('POST', '/af-api/insert-new-order-item', function () {
$authController = new AuthController;
$homeController = new HomeController;
if (!$authController->hasUserPermission()) return $homeController->jsonResponse(401, 'Not permitted.');
$orderController = new OrderController;
if ($orderController->insertNewOrderItem()) {
$homeController->jsonResponse(201, 'New item was created.');
} else {
$homeController->jsonResponse(401, 'Mistake.');
}
});
$router->map('POST', '/af-api/update-order-item', function () {
$authController = new AuthController;
$homeController = new HomeController;
if (!$authController->hasUserPermission()) return $homeController->jsonResponse(401, 'Not permitted.');
$orderController = new OrderController;
if ($orderController->updateOrderItem()) {
$homeController->jsonResponse(200, 'Order item was updated.');
} else {
$homeController->jsonResponse(400, 'Mistake.');
}
});
// match current request url
$match = $router->match();
// call closure or throw 404 status
//var_dump($match);
if (is_array($match) && is_callable($match['target'])) {
call_user_func_array($match['target'], $match['params']);
} else {
// no route was matched
header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
echo '1';
}