-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
89 lines (64 loc) · 2.41 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
<?php
require __DIR__ . '/vendor/autoload.php';
use Psr\Http\Message\RequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Factory\AppFactory;
require_once 'Fruit.php';
require_once 'FruitDAO.php';
$app = AppFactory::create();
$app->addBodyParsingMiddleware();
$app->addRoutingMiddleware();
$app->addErrorMiddleware(true, true, true);
$app->get('/', function (Request $request, Response $response, array $args) {
$response->getBody()->write('Olá mundo');
return $response;
});
$app->get('/alunos', function (Request $request, Response $response, array $args) {
$alunos = [
'1' => 'Ravel',
'2' => 'Daniel Rocha Galvão',
'3' => 'João, já entregou o exercício?'
];
$response->getBody()->write(json_encode($alunos));
return $response->withHeader("Content-type", "application/json");
});
$app->get("/alunos/{id}", function (Request $request, Response $response, array $args){
$alunos = [
'1' => 'Ravel',
'2' => 'Daniel Rocha Galvão',
'3' => 'João, já entregou o exercício?'
];
$idAluno = $args['id'];
$aluno = [$idAluno => $alunos[$idAluno]];
$response->getBody()->write(json_encode($aluno));
return $response->withHeader('Content-type','application/json');
});
$app->get('/fruits', function (Request $request, Response $response, array $args) {
$fruitsDAO = new FruitDAO();
$fruits = $fruitsDAO->read();
$response->getBody()->write(json_encode($fruits));
return $response->withHeader('Content-type', 'application/json');
});
$app->post('/fruits', function (Request $request, Response $response, array $args) {
$data = $request->getParsedBody();
$fruit = new Fruit($data['nome'],$data['quantidade']);
$fruitDAO = new FruitDAO();
$fruitDAO->create($fruit);
return $response->withStatus(201);
});
$app->put('/fruits/{id}', function (Request $request, Response $response, array $args) {
$id = $args['id'];
$data = $request->getParsedBody();
$fruit = new Fruit($data['nome'],$data['quantidade']);
$fruit->setId($id);
$fruitDAO = new FruitDAO();
$fruitDAO->update($fruit);
return $response->withStatus(200);
});
$app->delete('/fruits/{id}', function (Request $request, Response $response, array $args) {
$id = $args['id'];
$fruitDAO = new FruitDAO();
$fruitDAO->delete($id);
return $response->withStatus(200);
});
$app->run();