-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcart-route.php
269 lines (196 loc) · 7.35 KB
/
cart-route.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/**
* @license MIT
* boutique-en-ligne (maxaboom)
* Copyright (c) 2023 Abraham Ukachi, Axel Vair, Morgane Marechal, Catherine Tranchand. The Maxaboom Project Contributors.
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @project boutique-en-ligne
* @name Cart - Route
* @file cart-route.php
* @author: Abraham Ukachi <[email protected]>
* @contributors: Axel Vair <[email protected]>, Morgane Marechal <[email protected]>, Catherine Tranchand <[email protected]>
* @version: 0.0.1
*
* Usage:
* 1+|> //
* -|>
*/
/*
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* MOTTO: We'll always do more 😜!!!
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
*/
// declare the `routes` namespace
namespace Maxaboom\Routes;
// use maxaboom's `CartController` class
use Maxaboom\Controllers\CartController;
// ####################
// ## GET - ROUTES ##
// ####################
/**
* Route used to display the cart page
*
* @example `/cart` will display the cart page
*
* @route /cart - the route to display the cart page
*/
$router->map('GET', '/cart', function() {
// create an instance of the CartController as `$cartController`
$cartController = new CartController();
// call the `showPage()` method to display the cart page
$cartController->showPage();
});
$router->map('GET', '/cart/[increase|reduce:action]/[i:product_id]', function($action, $product_id) {
$cartController = new CartController();
if($action=='increase'){
$response = $cartController->increaseQuantity($product_id);
echo json_encode($response);
}
if($action=='reduce'){
$response = $cartController->reduceQuantity($user_id, $product_id);
echo json_encode($response);
}
});
/**
* Route used to get the total number of products in the cart
*
* @example `/cart/total` will add the product with id 1 to the cart
*
* @param int $product_id - the id of the product to add to the cart
*/
$router->map('GET', '/cart/total', function() {
// create an instance of the CartController
$cartController = new CartController();
// call the addToCart method and store the response
$response = $cartController->getCartProductsTotal();
// return the response as JSON
echo json_encode($response);
});
$router->map( 'GET', '/cart/test', function() {
$cartController = new CartController();
$infoCart = $cartController->infoCart(11);
require_once __DIR__ . '/../models/test/cart.php';
});
/**
* Route used to get a range of products from the cart
*
* @example (1) - `/cart/products/0/10` will get the first 10 products from the cart
* @example (2) - `/cart/products/10/10` will get the next 10 products from the cart
*
* @route /cart/products/[i:start]/[i:limit] - the route to get a range of products from the cart
*
* @method GET
*
* @param int $start - the start index of the range
* @param int $limit - the limit of the range
*/
$router->map('GET', '/cart/products/[i:start]/[i:limit]', function($start, $limit) {
// instantiate the CartController
$cartController = new CartController();
// Get the products from the cart
$products = $cartController->getProducts($start, $limit);
// return the products as JSON
echo json_encode($products);
});
/*
$router->map('PATCH', '/cart/[increase|reduce:action]/[i:user_id]/[i:product_id]', function($action, $user_id, $product_id) {
$cartController = new CartController();
if($action=='increase'){
$response = $cartController->increaseQuantity($user_id, $product_id);
echo json_encode($response);
}
if($action=='reduce'){
$response = $cartController->reduceQuantity($user_id, $product_id);
echo json_encode($response);
}
//echo $response;
});
*/
// #######################
// ## DELETE - ROUTES ##
// #######################
/**
* Route used to remove a product from the cart
*
* @example: `/cart/1` will remove the product with id 1 from the cart
*
* @route /cart/[i:product_id] - the route to remove a product from the cart
* @param int $product_id - the id of the product to remove from the cart
*/
$router->map('DELETE', '/cart/[i:product_id]', function($product_id) {
// create an instance of the CartController
$cartController = new CartController();
// call the `removeFromCart()` method and store the response
$response = $cartController->removeFromCart($product_id);
// return the response as JSON
echo json_encode($response);
});
// ########################
// #### PUT - ROUTES ####
// ########################
/**
* Route used to add a product to the cart
*
* @example `/cart/1` will add the product with id 1 to the cart
*
* @route /cart/[i:product_id] - the route to add a product to the cart
* @param int $product_id - the id of the product to add to the cart
*/
$router->map('PUT', '/cart/[i:product_id]', function($product_id) {
// create an instance of the CartController
$cartController = new CartController();
// call the addToCart method and store the response
$response = $cartController->addToCart($product_id);
// return the response as JSON
echo json_encode($response);
});
// ##########################
// #### PATCH - ROUTES ####
// ##########################
/**
* Route used to update the quantity of a product in the cart
*
* @example `/cart/increase/2` will increase the quantity of the product with id 2 in the cart
*
* @route /cart/[increase|decrease:action]/[i:product_id] -
*
* @param string $action - the action to perform on the product quantity (increase|decrease)
* @param int $product_id - the id of the product to update the quantity of
*/
$router->map('PATCH', '/cart/[increase|decrease:action]/[i:product_id]', function($action, $product_id) {
// create an instance of the CartController
$cartController = new CartController();
// If the action is `increase`...
if($action == 'increase') {
// call the `increaseProductQuantity()` method and store the response
$response = $cartController->increaseProductQuantity($product_id);
// echo the response as JSON
echo json_encode($response);
}
// If the action is `reduce`...
if($action == 'decrease') {
// ... call the `decreaseProductQuantity()` method and store the response
$response = $cartController->decreaseProductQuantity($product_id);
// echo the response as JSON
echo json_encode($response);
}
});