Skip to content

Commit

Permalink
feat: CRUD coletores e identificadores ok!
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanRamos01 committed Mar 14, 2024
1 parent 85093c5 commit fd8280d
Show file tree
Hide file tree
Showing 12 changed files with 1,928 additions and 1,706 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"editor.detectIndentation": false,
"editor.semanticHighlighting.enabled": false,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"source.fixAll.eslint": "explicit"
},
"editor.rulers": [
120
],
Expand Down
79 changes: 79 additions & 0 deletions src/controllers/coletor-controller.js
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);
}
};
90 changes: 90 additions & 0 deletions src/controllers/identificador-controller.js
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);
}
};
38 changes: 38 additions & 0 deletions src/routes/coletor.js
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,
]);
};
36 changes: 36 additions & 0 deletions src/routes/identificador.js
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,
]);
};
28 changes: 28 additions & 0 deletions src/validators/coletor-atualiza.js
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,
},
};
7 changes: 7 additions & 0 deletions src/validators/coletor-desativa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
id: {
in: 'params',
isInt: true,
isEmpty: false,
},
};
20 changes: 20 additions & 0 deletions src/validators/coletor-listagem.js
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,
// },
};
15 changes: 15 additions & 0 deletions src/validators/identificador-atualiza.js
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,
},
};
10 changes: 10 additions & 0 deletions src/validators/identificador-cadastro.js
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
},
},
};
20 changes: 20 additions & 0 deletions src/validators/identificador-listagem.js
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,
// },
};
Loading

0 comments on commit fd8280d

Please sign in to comment.