diff --git a/Md03_Dev_Back_end/Bloco21/21.4/README.md b/Md03_Dev_Back_end/Bloco21/21.4/README.md new file mode 100644 index 00000000..e69de29b diff --git a/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/.eslintignore b/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/.eslintignore new file mode 100644 index 00000000..8d9740ad --- /dev/null +++ b/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/.eslintignore @@ -0,0 +1,3 @@ +// .eslintignore + +node_modules \ No newline at end of file diff --git a/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/.eslintrc.json b/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/.eslintrc.json new file mode 100644 index 00000000..9cf09375 --- /dev/null +++ b/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/.eslintrc.json @@ -0,0 +1,10 @@ +// .eslintrc.json +{ + "env": { + "es2020": true + }, + "extends": "trybe-backend", + "rules": { + "sonarjs/no-duplicate-string": ["error", 5] + } +} \ No newline at end of file diff --git a/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/app.js b/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/app.js new file mode 100644 index 00000000..2bdaadca --- /dev/null +++ b/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/app.js @@ -0,0 +1,16 @@ +// src/app.js +const express = require('express'); +const route = require('./routes/rotas'); + +const app = express(); + +app.use(express.json()); +app.use(route); + + + +// GET / HTTP/1.1 +// Host: developer.mozilla.org +// Accept: text/html + +module.exports = app; \ No newline at end of file diff --git a/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/database/teams.js b/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/database/teams.js new file mode 100644 index 00000000..7c65bf23 --- /dev/null +++ b/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/database/teams.js @@ -0,0 +1,9 @@ +const teams = [ + + { id: 1, nome: 'São Paulo Futebol Clube', sigla: 'SPF' }, + + { id: 2, nome: 'Sociedade Esportiva Palmeiras', sigla: 'PAL' }, + +]; + +module.exports = teams; \ No newline at end of file diff --git a/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/index.js b/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/index.js new file mode 100644 index 00000000..e69de29b diff --git a/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/middlewares/validateTeam.js b/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/middlewares/validateTeam.js new file mode 100644 index 00000000..a612f3d3 --- /dev/null +++ b/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/middlewares/validateTeam.js @@ -0,0 +1,34 @@ +const teams = require("../database/teams"); +const NOT_FOUND = 404; + +const validateTeam = (req, res, next) => { + const requiredProperties = ['nome', 'sigla']; + + if (requiredProperties.every((property) => property in req.body)) { + next(); // Chama o próximo middleware + } else { + res.sendStatus(400); // Ou já responde avisando que deu errado + } +}; + +const validateId = (req, res, next) => { + const id = Number(req.params.id); + + if (Number.isNaN(id)) res.status(400).send({ message: 'id não numerico' }); + + return next(); +} + +const existingId = (req, res, next) => { + const idParam = Number(req.params.id); + + if (teams.some(({ id }) => id === idParam)) next(); + + return res.status(NOT_FOUND).send({ message: 'id não encontrado' }); +} + +module.exports = { + validateTeam, + validateId, + existingId +}; \ No newline at end of file diff --git a/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/routes/rotas.js b/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/routes/rotas.js new file mode 100644 index 00000000..aac72130 --- /dev/null +++ b/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/routes/rotas.js @@ -0,0 +1,54 @@ +const express = require('express'); +const teams = require('../database/teams'); +const { validateId, existingId, validateTeam } = require('../middlewares/validateTeam'); + +const OK = 200; +// const INTERNAL_SERVER_ERROR = 500; + +const route = express.Router(); + +let nextId = 3; + +route.get('/', (_req, res) => res.status(OK).json({ message: 'Olá Mundo!' })); + +route.get('/teams', (req, res) => res.status(OK).json({ teams })); + +route.get('/teams/:id', validateId, existingId, (req, res) => { + const { id } = req.params; + const teamEsp = teams.find((team) => team.id === Number(id)); + + res.status(OK).json({ teamEsp }); +}); + +// Arranja os middlewares para chamar validateTeam primeiro +route.post('/teams', validateTeam, (req, res) => { + const team = { id: nextId, ...req.body }; + + teams.push(team); + nextId += 1; + res.status(201).json(team); +}); + +route.put('/teams/:id', validateTeam, validateId, existingId, (req, res) => { + const id = Number(req.params.id); + const team = teams.find(t => t.id === id); + + const index = teams.indexOf(team); + const updated = { id, ...req.body }; + + teams.splice(index, 1, updated); + res.status(201).json(updated); +}); + + +route.delete('/teams/:id', validateId, existingId, (req, res) => { + const id = Number(req.params.id); + const team = teams.find(t => t.id === id); + const index = teams.indexOf(team); + + teams.splice(index, 1); + res.sendStatus(204); +}); + + +module.exports = route; \ No newline at end of file diff --git a/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/server.js b/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/server.js new file mode 100644 index 00000000..389396a0 --- /dev/null +++ b/Md03_Dev_Back_end/Bloco21/21.4/soccer-team-manager/src/server.js @@ -0,0 +1,4 @@ +// src/server.js +const app = require('./app'); + +app.listen(3002, () => console.log('server running on port 3002')); \ No newline at end of file