-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
113 lines (99 loc) · 2.76 KB
/
app.js
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
const express = require("express");
const app = express();
// Set a port to run the server
const PORT = 3000;
// Initialize a project repository
const projects = [{
id: "1",
title: "Projeto Green Forest",
tasks: []
},{
id: "2",
title: "Projeto San Paolo",
tasks: []
},{
id: "3",
title: "Projeto Paris",
tasks: []
}];
// Set the middleware that handles the json files
app.use(express.json());
// Middleware that checks if a project exists
let checkProjectExists = (req, res, next) => {
const {id} = req.params;
let project = projects.find(project => project.id == id);
if(!project){
return res.status(400).json({
error: "Project not found"
})
}
return next();
}
// Middleware that checks if an ID already exists
let checkIdExists = (req,res,next) => {
const {id} = req.body;
let project = projects.find(project => project.id == id);
if(project){
return res.status(400).json({
error: "ID already exists"
})
}
return next();
}
// Middleware which is called by all requests (aka Global Middleware) and prints (console.log) a request counter
let logRequests = (req,res,next) => {
console.count("Number of requests");
next();
}
// Loads a global middleware
app.use(logRequests);
// List all projects
app.get("/projects", (req, res) => {
return res.json(projects);
});
// Get a project filtered by your id
app.get("/projects/:id", checkProjectExists, (req, res) => {
const {id} = req.params; // route params
let project = projects.filter((project)=>project.id===id);
return res.json(project);
});
// Create a new project
app.post("/projects", checkIdExists, (req,res) => {
const {id, title} = req.body;
let newProject = {
id,
title,
tasks: []
};
projects.push(newProject);
res.json(projects);
});
// Update a project based on its ID
app.put("/projects/:id", checkProjectExists, (req,res) => {
const {id} = req.params;
const {title} = req.body;
let project = projects.forEach((project) => {
if (project.id === id){
project.title = title;
}
});
res.json(projects);
});
// Delete a project based on its ID
app.delete("/projects/:id", checkProjectExists, (req, res) => {
const {id} = req.params;
//let index = projects.map(project => project.id).indexOf(id);
let projectIndex = projects.findIndex(project => project.id == id);
projects.splice(projectIndex, 1);
res.json(projects);
});
// Create a new task in an existing project
app.post("/projects/:id/tasks", checkProjectExists, (req, res) => {
const {title} = req.body;
const {id} = req.params;
let project = projects.find(project => project.id == id);
project.tasks.push(title);
res.json(projects);
});
// Start the express server on port 3000 (seted)
app.listen(PORT, ()=>console.log(`Server listening on port ${PORT}.`));