Last active
February 9, 2020 02:42
-
-
Save TiagoSSGaspar/c379160d856b8e2b891692f7b5776b8e to your computer and use it in GitHub Desktop.
Crud usando nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require("express"); | |
| const server = express(); | |
| server.use(express.json()); | |
| let numberOfRequests = 0; | |
| const projects = []; | |
| function checkProjectExists(req, res, next) { | |
| const {id} = req.params; | |
| const project = projects.find(project => project.id === id); | |
| return (!project) ? res.status(400).json({error: 'Project not found'}) : next(); | |
| } | |
| function checkIdEmpty(req,res,next) { | |
| const {id} = req.body; | |
| return (id === "") ? res.status(400).json({erro:'Id cannot be null or undefined'}):next(); | |
| } | |
| function logRequests(req, res, next) { | |
| numberOfRequests++; | |
| console.log(`Number of requests: ${numberOfRequests}`); | |
| return next(); | |
| } | |
| server.use(logRequests); | |
| server.post('/projects',checkIdEmpty,(req, res) => { | |
| const {id, title} = req.body; | |
| projects.push({id, title, tasks: []}); | |
| return res.json(projects) | |
| }); | |
| server.post('/projects/:id/tasks', checkProjectExists, (req, res) => { | |
| const {id} = req.params; | |
| const {title} = req.body; | |
| projects.map((idProject) => { | |
| if (id === idProject.id) { | |
| idProject.tasks.push(title) | |
| } | |
| }); | |
| return res.json(projects) | |
| }); | |
| server.put('/projects/:id', checkProjectExists, (req, res) => { | |
| const {id} = req.params; | |
| const {title} = req.body; | |
| projects.map((idProject) => { | |
| if (id === idProject.id) { | |
| idProject.title = title | |
| } | |
| }); | |
| return res.json(projects) | |
| }); | |
| server.get('/projects', (req, res) => { | |
| return res.json(projects) | |
| }); | |
| server.get('/projects/:id', (req, res) => { | |
| const {id} = req.params; | |
| const projectIndex = projects.findIndex(project => project.id === id); | |
| return res.json(projects[projectIndex]) | |
| }); | |
| server.delete('/projects/:id', checkProjectExists, (req, res) => { | |
| const {id} = req.params; | |
| const projectIndex = projects.findIndex(project => project.id === id); | |
| projects.splice(projectIndex, 1); | |
| return res.send(); | |
| }); | |
| server.listen(3000); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "desafio-01-bootcamp", | |
| "version": "1.0.0", | |
| "main": "index.js", | |
| "license": "MIT", | |
| "scripts": { | |
| "dev": "nodemon index.js" | |
| }, | |
| "dependencies": { | |
| "express": "^4.17.1" | |
| }, | |
| "devDependencies": { | |
| "nodemon": "^2.0.2" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment