-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.php
64 lines (47 loc) · 1.89 KB
/
routes.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
<?php
use Slim\Http\Request;
use Slim\Http\Response;
// Routes
$app->post('/carro', function (Request $request, Response $response) {
// Exemplo: {"MODELO":"Fusca","MARCA":"Volkswagen"}
$json = json_encode($request->getParsedBody());
$carro = json_decode($json, true);
$table = $this->db->table('carro');
if($table->insert($carro) == 1) {
return $response->withJson('Cadastrado com sucesso', 201);
} else {
return $response->withJson('Erro no cadastro', 400, JSON_UNESCAPED_UNICODE);
}
});
$app->put('/carro/{id}', function ($request, $response, $args) {
// exemplo de JSON: {"MODELO":"New","MARCA":"marca gol"}
$id = $args['id'];
$json = json_encode($request->getParsedBody());
$carro = json_decode($json, true);
$table = $this->db->table('carro');
if($table->where('ID', $id)->update($carro) == 1) {
return $response->withJson('Atualizado com sucesso', 201);
} else {
return $response->withJson('Erro na atualização', 400, JSON_UNESCAPED_UNICODE);
}
});
$app->delete('/carro/{id}', function ($request, $response, $args) {
$id = $args['id'];
$table = $this->db->table('carro');
if($table->where('ID', $id)->delete() == 1) {
return $response->withJson('Deletado com sucesso', 201);
} else {
return $response->withJson('Erro na remoção', 400, JSON_UNESCAPED_UNICODE);
}
});
$app->get('/carro', function ($request, $response, $args) {
$table = $this->db->table('carro');
$carros = $table->get();
return $response->withJson($carros, 201, JSON_UNESCAPED_UNICODE);
});
$app->get('/carro/{id}', function ($request, $response, $args) {
$id = $args['id'];
$table = $this->db->table('carro');
$carro = $table->where('id', $id)->get();
return $response->withJson($carro, 201, JSON_UNESCAPED_UNICODE);
});