Remove alguns cometarios / renomeia vars em products SERVICES
This commit is contained in:
parent
3aeb23f6dd
commit
757631d86c
|
@ -24,9 +24,7 @@ router.get('/:id', async (req, res) => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// const { _id, name, quantity } = result;
|
res.status(OK).json(result);
|
||||||
|
|
||||||
res.status(200).json(result);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('/', rescue(async (req, res) => {
|
router.post('/', rescue(async (req, res) => {
|
||||||
|
@ -63,19 +61,15 @@ router.put('/:id', rescue(async (req, res) => {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
res.status(OK).json({ _id: id, itensSold: [...soldProducts] }); // Se não houver erro, o SERVICE repassa insertedId da MODEL que é usado aqui.
|
res.status(OK).json({ _id: id, itensSold: [...soldProducts] });
|
||||||
}));
|
}));
|
||||||
|
|
||||||
router.delete('/:id', rescue(async (req, res) => {
|
router.delete('/:id', rescue(async (req, res) => {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|
||||||
const result = await sales.deleteIt(id); // Retorna apenas o log de delete
|
const result = await sales.deleteIt(id); // Here is returned "deleted data product" OR "data Errors" from SERVICES.
|
||||||
|
|
||||||
// DEBUG:
|
if (result.code) { // Check if SERVICES retuned some "data errors"
|
||||||
console.log('CONTROLLER: retorno result:');
|
|
||||||
console.log(result);
|
|
||||||
|
|
||||||
if (result.code) {
|
|
||||||
return res.status(result.status).json({
|
return res.status(result.status).json({
|
||||||
err: {
|
err: {
|
||||||
code: result.code,
|
code: result.code,
|
||||||
|
@ -84,8 +78,7 @@ router.delete('/:id', rescue(async (req, res) => {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// res.status(OK).json({ _id: id, itensSold: [...result.soldProducts] }); // Se não houver erro, o SERVICE repassa insertedId da MODEL que é usado aqui.
|
res.status(OK).json(result);
|
||||||
res.status(OK).json(result); // Se não houver erro, o SERVICE repassa insertedId da MODEL que é usado aqui.
|
|
||||||
}));
|
}));
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
|
@ -8,11 +8,11 @@ const OPTIONS = {
|
||||||
|
|
||||||
// Para usar com o avaliador:
|
// Para usar com o avaliador:
|
||||||
|
|
||||||
const MONGO_DB_URL = 'mongodb://mongodb:27017/StoreManager';
|
// const MONGO_DB_URL = 'mongodb://mongodb:27017/StoreManager';
|
||||||
|
|
||||||
// Para uso local:
|
// Para uso local:
|
||||||
|
|
||||||
// const MONGO_DB_URL = 'mongodb://localhost:27017/StoreManager';
|
const MONGO_DB_URL = 'mongodb://localhost:27017/StoreManager';
|
||||||
|
|
||||||
// const DB_NAME = 'StoreManager'; // outra maneira
|
// const DB_NAME = 'StoreManager'; // outra maneira
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
const { ObjectId } = require('mongodb');
|
|
||||||
const models = require('../models/products');
|
const models = require('../models/products');
|
||||||
const validators = require('../utils/validators');
|
const validators = require('../utils/validators');
|
||||||
|
|
||||||
const OK = 200;
|
const OK = 200;
|
||||||
const UNPROCESSABLE_ENTITY = 422;
|
const STATUS_UNPROCESSABLE_ENTITY = 422;
|
||||||
const CODE = 'invalid_data';
|
const CODE_INVALID_DATA = 'invalid_data';
|
||||||
|
const MSG_WRONG_ID = 'Wrong id format';
|
||||||
|
|
||||||
const create = async (name, quantity) => {
|
const create = async (name, quantity) => {
|
||||||
const validQtd = validators.quantity(quantity);
|
const validQtd = validators.quantity(quantity);
|
||||||
|
@ -36,19 +36,19 @@ const getAll = async () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const getById = async (id) => {
|
const getById = async (id) => {
|
||||||
const notFoundMsg = 'Wrong id format';
|
// Validating id ...
|
||||||
|
|
||||||
if (!ObjectId.isValid(id)) { // Get error if invalid id format
|
const validateId = validators.validProductId(id);
|
||||||
return { status: UNPROCESSABLE_ENTITY, code: CODE, message: notFoundMsg };
|
if (validateId && validateId.code) return validateId;
|
||||||
}
|
|
||||||
|
// Searching ...
|
||||||
|
|
||||||
const product = await models.getById(id);
|
const product = await models.getById(id);
|
||||||
|
|
||||||
// Get the SAME error if invalid id is not found:
|
|
||||||
if (!product) {
|
if (!product) {
|
||||||
return { status: UNPROCESSABLE_ENTITY,
|
return { status: STATUS_UNPROCESSABLE_ENTITY,
|
||||||
code: CODE,
|
code: CODE_INVALID_DATA,
|
||||||
message: notFoundMsg };
|
message: MSG_WRONG_ID };
|
||||||
}
|
}
|
||||||
|
|
||||||
return product;
|
return product;
|
||||||
|
@ -70,14 +70,14 @@ const update = async (id, name, quantity) => {
|
||||||
const updateLog = await models.update(id, name, quantity);
|
const updateLog = await models.update(id, name, quantity);
|
||||||
|
|
||||||
if (updateLog.modifiedCount === 0) {
|
if (updateLog.modifiedCount === 0) {
|
||||||
return { status: UNPROCESSABLE_ENTITY,
|
return { status: STATUS_UNPROCESSABLE_ENTITY,
|
||||||
code: CODE,
|
code: CODE_INVALID_DATA,
|
||||||
message: notUpdated };
|
message: notUpdated };
|
||||||
}
|
}
|
||||||
|
|
||||||
// DEBUG:
|
// // DEBUG:
|
||||||
console.log('SERVICES: retorno updateLog:');
|
// console.log('SERVICES: retorno updateLog:');
|
||||||
console.log(updateLog);
|
// console.log(updateLog);
|
||||||
|
|
||||||
return updateLog;
|
return updateLog;
|
||||||
};
|
};
|
||||||
|
@ -86,23 +86,21 @@ const deleteIt = async (id) => {
|
||||||
const notDeletedMsg = 'Wrong id format';
|
const notDeletedMsg = 'Wrong id format';
|
||||||
const errorDeleteMsg = 'Ops!, Item not deleted';
|
const errorDeleteMsg = 'Ops!, Item not deleted';
|
||||||
|
|
||||||
// Searching
|
// Validating id ...
|
||||||
|
|
||||||
if (!ObjectId.isValid(id)) { // Get error if invalid id format
|
const validateId = validators.validProductId(id);
|
||||||
return { status: UNPROCESSABLE_ENTITY, code: CODE, message: notDeletedMsg };
|
if (validateId && validateId.code) return validateId;
|
||||||
}
|
|
||||||
|
// Searching ...
|
||||||
|
|
||||||
const product = await models.getById(id);
|
const product = await models.getById(id);
|
||||||
|
|
||||||
// // DEBUG:
|
|
||||||
// console.log('SERVICES: retorno product:');
|
|
||||||
// console.log(product);
|
|
||||||
|
|
||||||
// Get the SAME error if invalid id was not found:
|
// Get the SAME error if invalid id was not found:
|
||||||
|
|
||||||
if (!product) {
|
if (!product) {
|
||||||
return { status: UNPROCESSABLE_ENTITY,
|
return { status: STATUS_UNPROCESSABLE_ENTITY,
|
||||||
code: CODE,
|
code: CODE_INVALID_DATA,
|
||||||
message: notDeletedMsg };
|
message: notDeletedMsg };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deleting
|
// Deleting
|
||||||
|
@ -110,15 +108,11 @@ const deleteIt = async (id) => {
|
||||||
const deleteLog = await models.deleteIt(id);
|
const deleteLog = await models.deleteIt(id);
|
||||||
|
|
||||||
if (deleteLog.deletedCount === 0) {
|
if (deleteLog.deletedCount === 0) {
|
||||||
return { status: UNPROCESSABLE_ENTITY,
|
return { status: STATUS_UNPROCESSABLE_ENTITY,
|
||||||
code: CODE,
|
code: CODE_INVALID_DATA,
|
||||||
message: errorDeleteMsg };
|
message: errorDeleteMsg };
|
||||||
}
|
}
|
||||||
|
|
||||||
// // DEBUG:
|
|
||||||
// console.log('SERVICES: retorno deleteLog:');
|
|
||||||
// console.log(deleteLog);
|
|
||||||
|
|
||||||
return product;
|
return product;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -571,7 +571,7 @@ describe('7 - Crie um endpoint para atualizar uma venda', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe.only('8 - Crie um endpoint para deletar uma venda', () => {
|
describe('8 - Crie um endpoint para deletar uma venda', () => {
|
||||||
let connection;
|
let connection;
|
||||||
let db;
|
let db;
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,17 @@
|
||||||
|
const { ObjectId } = require('mongodb');
|
||||||
const models = require('../models/products');
|
const models = require('../models/products');
|
||||||
|
|
||||||
const UNPROCESSABLE_ENTITY = 422;
|
const UNPROCESSABLE_ENTITY = 422;
|
||||||
const CODE = 'invalid_data';
|
const CODE_INVALID_DATA = 'invalid_data';
|
||||||
|
const STATUS_UNPROCESSABLE_ENTITY = 422;
|
||||||
|
|
||||||
|
const MSG_WRONG_ID = 'Wrong id format';
|
||||||
|
|
||||||
function name(field) {
|
function name(field) {
|
||||||
const lengthMdg = '"name" length must be at least 5 characters long';
|
const lengthMdg = '"name" length must be at least 5 characters long';
|
||||||
|
|
||||||
if (field.length < 5) {
|
if (field.length < 5) {
|
||||||
return { status: UNPROCESSABLE_ENTITY, code: CODE, message: lengthMdg };
|
return { status: UNPROCESSABLE_ENTITY, code: CODE_INVALID_DATA, message: lengthMdg };
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
|
@ -18,11 +22,11 @@ function quantity(qtd) {
|
||||||
const typeMsg = '"quantity" must be a number';
|
const typeMsg = '"quantity" must be a number';
|
||||||
|
|
||||||
if (qtd < 1) {
|
if (qtd < 1) {
|
||||||
return { status: UNPROCESSABLE_ENTITY, code: CODE, message: sizeMsg };
|
return { status: UNPROCESSABLE_ENTITY, code: CODE_INVALID_DATA, message: sizeMsg };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof qtd !== 'number') {
|
if (typeof qtd !== 'number') {
|
||||||
return { status: UNPROCESSABLE_ENTITY, code: CODE, message: typeMsg };
|
return { status: UNPROCESSABLE_ENTITY, code: CODE_INVALID_DATA, message: typeMsg };
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
|
@ -32,14 +36,22 @@ const areadyExists = async (field, msg) => {
|
||||||
const product = await models.getByName(field);
|
const product = await models.getByName(field);
|
||||||
|
|
||||||
if (product.name) {
|
if (product.name) {
|
||||||
return { status: UNPROCESSABLE_ENTITY, code: CODE, message: msg };
|
return { status: UNPROCESSABLE_ENTITY, code: CODE_INVALID_DATA, message: msg };
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const validProductId = (id) => {
|
||||||
|
if (!ObjectId.isValid(id)) { // Get error if invalid id format
|
||||||
|
return {
|
||||||
|
status: STATUS_UNPROCESSABLE_ENTITY, code: CODE_INVALID_DATA, message: MSG_WRONG_ID };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name,
|
name,
|
||||||
quantity,
|
quantity,
|
||||||
areadyExists,
|
areadyExists,
|
||||||
|
validProductId,
|
||||||
};
|
};
|
Loading…
Reference in a new issue