-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
105 lines (89 loc) · 3.19 KB
/
Copy pathapp.js
File metadata and controls
105 lines (89 loc) · 3.19 KB
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
// 1. Importar módulos
var express = require("express");
var bodyParser = require("body-parser");
// 2. Importar o módulo da nossa aplicação
var ListaDAO = require("./listaDAO");
// 3. Iniciar aplicação Express
var app = express();
// 4. Configurar método para ler a propriedade `body` da requisições
app.use(bodyParser.json());
// - Criar nova lista
app.post("/lista", function(request, response) {
// Pega as informações provenientes da requisição
var nome = request.body.nome;
// Cria a nova lista
var result = ListaDAO.criarLista(nome);
response.status(200);
response.json(result);
response.end();
});
// - Renomear lista
app.put("/lista/:id_lista", function(request, response) {
// Pega as informações provenientes da requisição
var novoNome = request.body.nome;
var idDaLista = request.params.id_lista;
// Renomeia a lista
var result = ListaDAO.renomearLista(idDaLista, novoNome);
response.status(200);
response.json(result);
response.end();
});
// - Apagar lista
app.delete("/lista/:id_lista", function(request, response) {
// Pega as informações provenientes da requisição
var idDaLista = request.params.id_lista;
// Apaga a lista com o identificador correspondente
var result = ListaDAO.apagarLista(idDaLista);
response.status(200);
response.json(result);
response.end();
});
// - Listar todas as listas
app.get("/lista", function(request, response) {
response.status(200);
response.json(ListaDAO.getListas());
response.end();
});
// - Criar nova tarefa em uma lista
app.post("/lista/:id_lista", function(request, response) {
// Pega as informações provenientes da requisição
var idDaLista = request.params.id_lista;
var descricaoDaTarefa = request.body.descricao;
// Adiciona a nova tarefa na lista de tarefas
var result = ListaDAO.novaTarefa(descricaoDaTarefa, idDaLista);
response.status(200);
response.json(result);
response.end();
});
// - Alternar tarefa para completa ou não completa
app.put("/lista/:id_lista/tarefa/:id_tarefa", function(request, response) {
// Pega as informações provenientes da requisição
var idDaLista = request.params.id_lista;
var idDaTarefa = request.params.id_tarefa;
// Alterna o estado da tarefa
var result = ListaDAO.toggleTarefa(idDaLista, idDaTarefa);
response.status(200);
response.json(result);
response.end();
});
// - Apagar tarefa em uma lista
app.delete("/lista/:id_lista/tarefa/:id_tarefa", function(request, response) {
// Pega as informações provenientes da requisição
var idDaLista = request.params.id_lista;
var idDaTarefa = request.params.id_tarefa;
// Apaga a tarefa da lista de tarefas
var result = ListaDAO.apagarTarefa(idDaLista, idDaTarefa);
response.status(200);
response.json(result);
response.end();
});
// - Listar tarefas de uma lista
app.get("/lista/:id_lista/tarefa", function(request, response) {
// Pegar o identificador da lista na rota
var idDaLista = request.params.id_lista;
response.status(200);
response.json(ListaDAO.getTarefas(idDaLista));
response.end();
});
// 5. Associar nossa API com a porta 8080
app.listen(8080);