Skip to content

Commit

Permalink
Correção nas rotas de criação de coletores e adição de filtros de nom…
Browse files Browse the repository at this point in the history
…e na listagem de coletores (#28)
  • Loading branch information
RyanRamos01 authored Apr 11, 2024
1 parent 88197f5 commit c684768
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
23 changes: 21 additions & 2 deletions src/controllers/coletor-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@ import BadRequestException from '../errors/bad-request-exception';
import models from '../models';
import codigos from '../resources/codigos-http';

const { Coletor } = models;
const { Coletor, Sequelize: { Op } } = models;

export const cadastraColetor = async (req, res, next) => {
try {
if (req.body.numero) {
const validaNumero = await Coletor.findOne({
where: { numero: req.body.numero },
});

if (validaNumero) return res.status(400).json({ mensagem: 'Número do coletor já está em uso.' });
}

const coletor = await Coletor.create(req.body);

res.status(codigos.CADASTRO_RETORNO).json(coletor);
} catch (error) {
next(error);
}

return null;
};

export const encontraColetor = async (req, res, next) => {
Expand All @@ -34,11 +45,19 @@ export const encontraColetor = async (req, res, next) => {

export const listaColetores = async (req, res, next) => {
try {
const { id, nome } = req.query;
const { limite, pagina } = req.paginacao;
const offset = (pagina - 1) * limite;

const where = { ativo: true };
if (id) {
where.id = id;
} else if (nome) {
where.nome = { [Op.like]: `%${nome}%` };
}

const result = await Coletor.findAndCountAll({
where: { ativo: true },
where,
order: [['id', 'ASC']],
limit: limite,
offset,
Expand Down
5 changes: 2 additions & 3 deletions src/controllers/identificador-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ export const cadastraIdentificador = async (req, res, next) => {
};

export const encontradaIdentificador = async (req, res, next) => {
const { id } = req.params;

try {
const { id } = req.params;
const identificador = await Identificador.findOne({
where: { id },
});
Expand Down Expand Up @@ -48,7 +47,7 @@ export const listaIdentificadores = async (req, res, next) => {

const result = await Identificador.findAndCountAll({
where,
attributes: ['id', 'nome'],
order: [['id', 'ASC']],
limit: limite,
offset,
});
Expand Down
2 changes: 1 addition & 1 deletion src/validators/coletor-cadastro.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export default {
numero: {
in: 'body',
isInt: true,
isEmpty: false,
optional: true,
},
};

0 comments on commit c684768

Please sign in to comment.