-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5e2ccc
commit ca0b289
Showing
24 changed files
with
1,629 additions
and
121 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
config | ||
config | ||
.env |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "pwa-node", | ||
"request": "launch", | ||
"name": "Run Nodemon", | ||
"runtimeExecutable": "nodemon", | ||
"restart": true, | ||
"skipFiles": [ | ||
"<node_internals>/**" | ||
], | ||
"program": "${workspaceFolder}/api/agendamentos/Agendamento.js" | ||
} | ||
] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,110 @@ | ||
const FormatoInvalido = require('./erros/Formatoinvalido'); | ||
const FormatoInvalido = require('./errors/FormatoInvalido'); | ||
const jsonxml = require('jsontoxml') | ||
|
||
class Serializar { | ||
json (dados){ | ||
json(dados) { | ||
return JSON.stringify(dados) | ||
} | ||
transformar(dados){ | ||
if(this.contentType !== 'application/json'){ | ||
throw new FormatoInvalido(this.contentType); | ||
|
||
}; | ||
|
||
xml(dados){ | ||
if(Array.isArray(dados)){ | ||
dados = dados.map((item)=>{ | ||
return{ | ||
[this.tag]: item | ||
} | ||
}) | ||
this.tag=this.tagList; | ||
} | ||
return this.json(this.filtrar(dados)); | ||
return jsonxml({ | ||
[this.tag]:dados | ||
}) | ||
} | ||
filtrarCampos (dados){ | ||
|
||
transformar(dados) { | ||
dados = this.filtrar(dados) | ||
if(this.contentType ==='application/json'){ | ||
|
||
return this.json( | ||
dados | ||
) | ||
} | ||
|
||
if(this.contentType ==='application/xml'){ | ||
|
||
return this.xml( | ||
dados | ||
) | ||
} | ||
|
||
throw new FormatoInvalido(this.contentType) | ||
|
||
|
||
}; | ||
|
||
filtrarCampos(dados) { | ||
const camposFiltrados = {}; | ||
this.camposPermitidos.forEach(campo => { | ||
if(dados.hasOwnProperty(campo)){ | ||
camposFiltrados[campo]= dados[campo]; | ||
this.camposPermitidos.forEach((campo) => { | ||
if(dados.hasOwnProperty(campo)) { | ||
camposFiltrados[campo] = dados[campo]; | ||
} | ||
|
||
}); | ||
|
||
return camposFiltrados; | ||
} | ||
filtrar(dados){ | ||
let dadosFiltrados = this.filtrarCampos(dados); | ||
}; | ||
|
||
if(Array.isArray(dados)){ | ||
dadosFiltrados = dados.map((dados)=>{ | ||
return this.filtrarCampos(dados); | ||
filtrar(dados) { | ||
let dadosFiltrados = this.filtrarCampos(dados); | ||
|
||
}) | ||
if(Array.isArray(dados)) { | ||
dadosFiltrados = dados.map((dado) => { | ||
return this.filtrarCampos(dado); | ||
}); | ||
} | ||
|
||
return dadosFiltrados; | ||
} | ||
} | ||
|
||
class SerializarAgendamento extends Serializar{ | ||
constructor(contentType, camposPersonalizados){ | ||
class SerializarAgendamento extends Serializar { | ||
constructor(contentType, camposPersonalizados) { | ||
super() | ||
this.contentType = contentType; | ||
this.camposPermitidos = ['id', 'nome_cliente', 'data_agendamento'].concat(camposPersonalizados || []) | ||
this.camposPermitidos = [ | ||
'id', 'nome_cliente', 'data_agendamento' | ||
].concat(camposPersonalizados || []); | ||
this.tag = 'Agendamento' | ||
this.tag = 'Agendamentos'; | ||
} | ||
} | ||
|
||
class SerializarErro extends Serializar { | ||
constructor(contentType, camposPersonalizados) { | ||
super(); | ||
this.contentType = contentType; | ||
this.camposPermitidos = [ | ||
'id', 'mensagem' | ||
].concat(camposPersonalizados || []); | ||
this.tag = 'Error'; | ||
this.tagList = 'Erros' | ||
} | ||
} | ||
module.exports ={ | ||
class SerializarUsuario extends Serializar{ | ||
constructor(contentType, camposPersonalizados){ | ||
super(); | ||
this.contentType = contentType; | ||
this.camposPermitidos =[ | ||
'id', 'email', 'senha' | ||
].concat (camposPersonalizados|| []); | ||
this.tag = 'Usuario'; | ||
this.tagList = 'Usuarios' | ||
} | ||
} | ||
|
||
|
||
module.exports = { | ||
Serializar: Serializar, | ||
SerializarAgendamento: SerializarAgendamento, | ||
FormatosValidos : ['application/json'] | ||
SerializarError: SerializarErro, | ||
SerializarUsuario, | ||
FormatosValidos: ['application/json', 'application/xml'] | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,58 @@ | ||
const express = require('express'); | ||
const router = require('../routes/agendamentos'); | ||
const FormatoInvalido = require ('../erros/Formatoinvalido'); | ||
const routersAgendamento = require('../routes/agendamentos'); | ||
const routersUsuarios = require('../routes/usuarios') | ||
const routersLogin = require('../routes/login') | ||
const FormatoInvalido = require('../errors/FormatoInvalido'); | ||
const FormatosValidos = require('../Serializar').FormatosValidos; | ||
const NaoEncontrado = require('../errors/NaoEncontrado'); | ||
const CampoInvalido = require('../errors/CampoInvalido'); | ||
const SerializarError = require('../Serializar').SerializarError; | ||
const DadosNaoInformados = require('../errors/DadosNaoInformados'); | ||
const passport = require('../usuarios/autenticacao'); | ||
|
||
module.exports =()=>{ | ||
module.exports = () => { | ||
const app = express() | ||
app.use((req, resp, next) =>{ | ||
|
||
app.use((req, resp, next) => { | ||
let formatoSolicitado = req.header('Accept'); | ||
if(formatoSolicitado === '*/*'){ | ||
formatoSolicitado = 'application/json' | ||
} | ||
if(FormatosValidos.indexOf(formatoSolicitado) === -1){ | ||
if(FormatosValidos.indexOf(formatoSolicitado) === -1) { | ||
resp.status(406); | ||
return resp.send(); | ||
} | ||
|
||
resp.setHeader('Content-Type', formatoSolicitado); | ||
next(); | ||
}) | ||
}); | ||
app.use(express.json()) | ||
app.use('/api', router) | ||
app.use('/api', routersAgendamento) | ||
app.use('/api', routersUsuarios) | ||
app.use('/api', routersLogin) | ||
|
||
app.use((error, req, resp, next) => { | ||
let status = 500; | ||
if(error instanceof CampoInvalido || error instanceof DadosNaoInformados) { | ||
status = 400 | ||
} | ||
if(error instanceof NaoEncontrado) { | ||
status = 404 | ||
} | ||
if(error instanceof FormatoInvalido) { | ||
status = 406 | ||
} | ||
serializarError = new SerializarError( | ||
resp.getHeader('Content-Type') | ||
) | ||
|
||
resp.status(status).send( | ||
serializarError.transformar({ | ||
id: error.idError, | ||
mensagem: error.message | ||
}) | ||
); | ||
}) | ||
|
||
return app | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
const ModeloTabelaAgendamento = require('../agendamentos/modelTabelaAgendamento'); | ||
const ModeloTabelaUsuario = require('../usuarios/modelTabelaUsuarios') | ||
|
||
ModeloTabelaAgendamento.sync() | ||
.then(()=> { | ||
console.log('Tabela criada com sucesso') | ||
}) | ||
.catch(() => { | ||
console.log('Erro, tabela não criada') | ||
.catch((error) => { | ||
console.log('Erro, tabela não criada',error) | ||
}); | ||
ModeloTabelaUsuario.sync() | ||
.then(()=> { | ||
console.log('Tabela criada com sucesso') | ||
}) | ||
.catch((error) => { | ||
console.log('Erro, tabela não criada', error) | ||
}); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class LoginInvalido extends Error{ | ||
constructor(){ | ||
super('email ou senha inválidos!') | ||
this.name = 'LoginInvalido' | ||
this.idError = 7; | ||
} | ||
} | ||
|
||
module.exports= LoginInvalido; |
This file contains 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
This file contains 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
Oops, something went wrong.