-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
52 lines (43 loc) · 1.29 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
<?php
require 'vendor/autoload.php';
require 'src/Product.php';
$app = new \Slim\Slim(array(
'debug' => true,
'templates.path' => './templates',
));
$app->get('/', function () use ($app) {
$data = array();
$app->render('main.php', $data);
});
$app->post('/product', function () use ($app) {
$body = $app->request->getBody();
$result = Product::add(json_decode($body));
if ($result['success']) {
$app->response->setStatus(201);
} else {
$app->response->setStatus(500);
}
$app->response->headers->set('Content-Type', 'application/json');
$app->response->setBody(json_encode($result));
});
$app->get('/product', function() use ($app) {
$products = Product::getAll();
$app->response->headers->set('Content-Type', 'application/json');
$app->response->setBody(json_encode($products));
});
$app->put('/product/:id', function($id) use ($app) {
$body = $app->request->getBody();
if (Product::update($id, json_decode($body)) !== false) {
$app->response->setStatus(204);
} else {
$app->response->setStatus(500);
}
});
$app->delete('/product/:id', function($id) use ($app) {
if (Product::remove($id)) {
$app->response->setStatus(204);
} else {
$app->response->setStatus(500);
}
});
$app->run();