-
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.
feat: CRUD coletores e identificadores ok!
- Loading branch information
1 parent
85093c5
commit fd8280d
Showing
12 changed files
with
1,928 additions
and
1,706 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
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,79 @@ | ||
import BadRequestException from '../errors/bad-request-exception'; | ||
import models from '../models'; | ||
import codigos from '../resources/codigos-http'; | ||
|
||
const { Coletor } = models; | ||
|
||
export const cadastraColetor = async (req, res, next) => { | ||
try { | ||
const coletor = await Coletor.create(req.body); | ||
res.status(codigos.CADASTRO_RETORNO).json(coletor); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
export const listaColetorPorId = async (req, res, next) => { | ||
try { | ||
const { id } = req.params; | ||
const coletor = await Coletor.findOne({ | ||
where: { id, ativo: true }, | ||
}); | ||
|
||
if (!coletor) { | ||
return res.status(404).json({ mensagem: 'Coletor não encontrado.' }); | ||
} | ||
|
||
res.status(200).json(coletor); | ||
} catch (error) { | ||
next(error); | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export const listaColetores = async (req, res, next) => { | ||
try { | ||
const coletores = await Coletor.findAll({ | ||
where: { ativo: true }, | ||
order: [['nome', 'ASC']], | ||
}); | ||
res.status(200).json(coletores); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
export const atualizaColetor = async (req, res, next) => { | ||
try { | ||
const { id } = req.params; | ||
const [updated] = await Coletor.update(req.body, { | ||
where: { id }, | ||
}); | ||
if (updated) { | ||
const updatedColetor = await Coletor.findByPk(id); | ||
res.status(200).json(updatedColetor); | ||
} else { | ||
throw new BadRequestException(404, 'Coletor não encontrado'); | ||
} | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
export const desativaColetor = async (req, res, next) => { | ||
try { | ||
const { id } = req.params; | ||
const [updated] = await Coletor.update({ ativo: false }, { | ||
where: { id }, | ||
}); | ||
|
||
if (updated) { | ||
res.status(codigos.DESATIVAR).send(); | ||
} else { | ||
throw new BadRequestException(404, 'Coletor não encontrado'); | ||
} | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; |
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,90 @@ | ||
import BadRequestException from '../errors/bad-request-exception'; | ||
import models from '../models'; | ||
import codigos from '../resources/codigos-http'; | ||
|
||
const { Identificador } = models; | ||
const { TomboIdentificador } = models; | ||
|
||
export const cadastraIdentificador = async (req, res, next) => { | ||
try { | ||
const identificador = await Identificador.create(req.body); | ||
res.status(codigos.CADASTRO_RETORNO).json(identificador); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
export const listaIdentificadorPorId = async (req, res, next) => { | ||
try { | ||
const { id } = req.params; | ||
const identificador = await Identificador.findByPk(id); | ||
|
||
if (!identificador) { | ||
return res.status(404).json({ mensagem: 'Identificador não encontrado.' }); | ||
} | ||
|
||
res.status(200).json(identificador); | ||
} catch (error) { | ||
next(error); | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
export const listaIdentificadores = async (req, res, next) => { | ||
try { | ||
const identificadores = await Identificador.findAll({ | ||
order: [['nome', 'ASC']], | ||
}); | ||
res.status(200).json(identificadores); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
export const listaIdentificadoresPorTombo = async (req, res, next) => { | ||
try { | ||
const { tomboHcf } = req.params; | ||
|
||
const tombosIdentificadores = await TomboIdentificador.findAll({ | ||
where: { tombo_hcf: tomboHcf }, | ||
attributes: ['identificador_id'], | ||
order: [['ordem', 'ASC']], | ||
}); | ||
|
||
if (!tombosIdentificadores || tombosIdentificadores.length === 0) { | ||
return res.status(404).json({ mensagem: 'Nenhum identificador encontrado para este tombo.' }); | ||
} | ||
|
||
const identificadorIds = tombosIdentificadores.map(ti => ti.identificador_id); | ||
|
||
// const identificadores = await Identificador.findAll({ | ||
// where: { id: identificadorIds }, | ||
// attributes: ['nome'], | ||
// }); | ||
|
||
// const nomesIdentificadores = identificadores.map(identificador => identificador.nome); | ||
|
||
res.status(200).json(identificadorIds); | ||
} catch (error) { | ||
next(error); | ||
} | ||
return null; | ||
}; | ||
|
||
export const atualizaIdentificador = async (req, res, next) => { | ||
try { | ||
const { id } = req.params; | ||
const [updated] = await Identificador.update(req.body, { | ||
where: { id }, | ||
}); | ||
if (updated) { | ||
const updatedIdentificador = await Identificador.findByPk(id); | ||
res.status(200).json(updatedIdentificador); | ||
} else { | ||
throw new BadRequestException(404, 'Identificador não encontrado'); | ||
} | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; |
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,38 @@ | ||
import * as coletoresController from '../controllers/coletor-controller'; | ||
import tokensMiddleware from '../middlewares/tokens-middleware'; | ||
import validacoesMiddleware from '../middlewares/validacoes-middleware'; | ||
import atualizarColetorEsquema from '../validators/coletor-atualiza'; | ||
import cadastrarColetorEsquema from '../validators/coletor-cadastro'; | ||
import desativarColetorEsquema from '../validators/coletor-desativa'; | ||
import listarColetoresEsquema from '../validators/coletor-listagem'; | ||
|
||
export default app => { | ||
app.route('/coletores').post([ | ||
tokensMiddleware(['CURADOR']), | ||
validacoesMiddleware(cadastrarColetorEsquema), | ||
coletoresController.cadastraColetor, | ||
]); | ||
|
||
app.route('/coletores/:id').get([ | ||
tokensMiddleware(['CURADOR']), | ||
coletoresController.listaColetorPorId, | ||
]); | ||
|
||
app.route('/coletores').get([ | ||
tokensMiddleware(['CURADOR']), | ||
validacoesMiddleware(listarColetoresEsquema), | ||
coletoresController.listaColetores, | ||
]); | ||
|
||
app.route('/coletores/:id').put([ | ||
tokensMiddleware(['CURADOR']), | ||
validacoesMiddleware(atualizarColetorEsquema), | ||
coletoresController.atualizaColetor, | ||
]); | ||
|
||
app.route('/coletores/:id').delete([ | ||
tokensMiddleware(['CURADOR']), | ||
validacoesMiddleware(desativarColetorEsquema), | ||
coletoresController.desativaColetor, | ||
]); | ||
}; |
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,36 @@ | ||
import * as identificadoresController from '../controllers/identificador-controller'; | ||
import tokensMiddleware from '../middlewares/tokens-middleware'; | ||
import validacoesMiddleware from '../middlewares/validacoes-middleware'; | ||
import atualizarIdentificadorEsquema from '../validators/identificador-atualiza'; | ||
import cadastrarIdentificadorEsquema from '../validators/identificador-cadastro'; | ||
import listarIdentificadoresEsquema from '../validators/identificador-listagem'; | ||
|
||
export default app => { | ||
app.route('/identificadores').post([ | ||
tokensMiddleware(['CURADOR']), | ||
validacoesMiddleware(cadastrarIdentificadorEsquema), | ||
identificadoresController.cadastraIdentificador, | ||
]); | ||
|
||
app.route('/identificadores/:id').get([ | ||
tokensMiddleware(['CURADOR']), | ||
identificadoresController.listaIdentificadorPorId, | ||
]); | ||
|
||
app.route('/identificadores').get([ | ||
tokensMiddleware(['CURADOR']), | ||
validacoesMiddleware(listarIdentificadoresEsquema), | ||
identificadoresController.listaIdentificadores, | ||
]); | ||
|
||
app.route('/identificadores/:id').put([ | ||
tokensMiddleware(['CURADOR']), | ||
validacoesMiddleware(atualizarIdentificadorEsquema), | ||
identificadoresController.atualizaIdentificador, | ||
]); | ||
|
||
app.route('/tombos/:tomboHcf/identificadores').get([ | ||
tokensMiddleware(['CURADOR']), | ||
identificadoresController.listaIdentificadoresPorTombo, | ||
]); | ||
}; |
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,28 @@ | ||
export default { | ||
nome: { | ||
in: 'body', | ||
isString: true, | ||
isEmpty: false, | ||
isLength: { | ||
options: [{ min: 3 }], | ||
}, | ||
}, | ||
email: { | ||
in: 'body', | ||
isString: true, | ||
optional: true, | ||
isLength: { | ||
options: [{ min: 5 }], | ||
}, | ||
}, | ||
numero: { | ||
in: 'body', | ||
isInt: true, | ||
optional: true, | ||
}, | ||
id: { | ||
in: 'params', | ||
isInt: true, | ||
isEmpty: false, | ||
}, | ||
}; |
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,7 @@ | ||
export default { | ||
id: { | ||
in: 'params', | ||
isInt: true, | ||
isEmpty: false, | ||
}, | ||
}; |
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,20 @@ | ||
export default { | ||
coletor: { | ||
in: 'query', | ||
isString: true, | ||
optional: true, | ||
isLength: { | ||
options: [{ min: 1 }], | ||
}, | ||
}, | ||
// limite: { | ||
// in: 'query', | ||
// isInt: true, | ||
// optional: true, | ||
// }, | ||
// pagina: { | ||
// in: 'query', | ||
// isInt: true, | ||
// optional: true, | ||
// }, | ||
}; |
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,15 @@ | ||
export default { | ||
nome: { | ||
in: 'body', | ||
isString: true, | ||
isEmpty: false, | ||
isLength: { | ||
options: { min: 3 }, | ||
}, | ||
}, | ||
id: { | ||
in: 'params', | ||
isInt: true, | ||
isEmpty: false, | ||
}, | ||
}; |
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,10 @@ | ||
export default { | ||
nome: { | ||
in: 'body', // Verifica o campo no corpo da requisição | ||
isString: true, | ||
isEmpty: false, | ||
isLength: { | ||
options: { min: 3 }, // Define o comprimento mínimo do nome | ||
}, | ||
}, | ||
}; |
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,20 @@ | ||
export default { | ||
identificador: { | ||
in: 'query', | ||
isString: true, | ||
optional: true, | ||
isLength: { | ||
options: [{ min: 1 }], | ||
}, | ||
}, | ||
// limite: { | ||
// in: 'query', | ||
// isInt: true, | ||
// optional: true, | ||
// }, | ||
// pagina: { | ||
// in: 'query', | ||
// isInt: true, | ||
// optional: true, | ||
// }, | ||
}; |
Oops, something went wrong.