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(200).json(result);
|
||||
res.status(OK).json(result);
|
||||
});
|
||||
|
||||
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) => {
|
||||
const { id } = req.params;
|
||||
|
||||
const result = await sales.deleteIt(id); // Retorna apenas o log de delete
|
||||
|
||||
// DEBUG:
|
||||
console.log('CONTROLLER: retorno result:');
|
||||
console.log(result);
|
||||
const result = await sales.deleteIt(id); // Here is returned "deleted data product" OR "data Errors" from SERVICES.
|
||||
|
||||
if (result.code) {
|
||||
if (result.code) { // Check if SERVICES retuned some "data errors"
|
||||
return res.status(result.status).json({
|
||||
err: {
|
||||
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); // Se não houver erro, o SERVICE repassa insertedId da MODEL que é usado aqui.
|
||||
res.status(OK).json(result);
|
||||
}));
|
||||
|
||||
module.exports = router;
|
|
@ -8,11 +8,11 @@ const OPTIONS = {
|
|||
|
||||
// Para usar com o avaliador:
|
||||
|
||||
const MONGO_DB_URL = 'mongodb://mongodb:27017/StoreManager';
|
||||
// const MONGO_DB_URL = 'mongodb://mongodb:27017/StoreManager';
|
||||
|
||||
// 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
|
||||
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
const { ObjectId } = require('mongodb');
|
||||
const models = require('../models/products');
|
||||
const validators = require('../utils/validators');
|
||||
|
||||
const OK = 200;
|
||||
const UNPROCESSABLE_ENTITY = 422;
|
||||
const CODE = 'invalid_data';
|
||||
const STATUS_UNPROCESSABLE_ENTITY = 422;
|
||||
const CODE_INVALID_DATA = 'invalid_data';
|
||||
const MSG_WRONG_ID = 'Wrong id format';
|
||||
|
||||
const create = async (name, quantity) => {
|
||||
const validQtd = validators.quantity(quantity);
|
||||
|
@ -36,19 +36,19 @@ const getAll = async () => {
|
|||
};
|
||||
|
||||
const getById = async (id) => {
|
||||
const notFoundMsg = 'Wrong id format';
|
||||
// Validating id ...
|
||||
|
||||
const validateId = validators.validProductId(id);
|
||||
if (validateId && validateId.code) return validateId;
|
||||
|
||||
if (!ObjectId.isValid(id)) { // Get error if invalid id format
|
||||
return { status: UNPROCESSABLE_ENTITY, code: CODE, message: notFoundMsg };
|
||||
}
|
||||
// Searching ...
|
||||
|
||||
const product = await models.getById(id);
|
||||
|
||||
// Get the SAME error if invalid id is not found:
|
||||
if (!product) {
|
||||
return { status: UNPROCESSABLE_ENTITY,
|
||||
code: CODE,
|
||||
message: notFoundMsg };
|
||||
return { status: STATUS_UNPROCESSABLE_ENTITY,
|
||||
code: CODE_INVALID_DATA,
|
||||
message: MSG_WRONG_ID };
|
||||
}
|
||||
|
||||
return product;
|
||||
|
@ -70,14 +70,14 @@ const update = async (id, name, quantity) => {
|
|||
const updateLog = await models.update(id, name, quantity);
|
||||
|
||||
if (updateLog.modifiedCount === 0) {
|
||||
return { status: UNPROCESSABLE_ENTITY,
|
||||
code: CODE,
|
||||
return { status: STATUS_UNPROCESSABLE_ENTITY,
|
||||
code: CODE_INVALID_DATA,
|
||||
message: notUpdated };
|
||||
}
|
||||
|
||||
// DEBUG:
|
||||
console.log('SERVICES: retorno updateLog:');
|
||||
console.log(updateLog);
|
||||
// // DEBUG:
|
||||
// console.log('SERVICES: retorno updateLog:');
|
||||
// console.log(updateLog);
|
||||
|
||||
return updateLog;
|
||||
};
|
||||
|
@ -86,23 +86,21 @@ const deleteIt = async (id) => {
|
|||
const notDeletedMsg = 'Wrong id format';
|
||||
const errorDeleteMsg = 'Ops!, Item not deleted';
|
||||
|
||||
// Searching
|
||||
|
||||
if (!ObjectId.isValid(id)) { // Get error if invalid id format
|
||||
return { status: UNPROCESSABLE_ENTITY, code: CODE, message: notDeletedMsg };
|
||||
}
|
||||
|
||||
// Validating id ...
|
||||
|
||||
const validateId = validators.validProductId(id);
|
||||
if (validateId && validateId.code) return validateId;
|
||||
|
||||
// Searching ...
|
||||
|
||||
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:
|
||||
|
||||
if (!product) {
|
||||
return { status: UNPROCESSABLE_ENTITY,
|
||||
code: CODE,
|
||||
message: notDeletedMsg };
|
||||
return { status: STATUS_UNPROCESSABLE_ENTITY,
|
||||
code: CODE_INVALID_DATA,
|
||||
message: notDeletedMsg };
|
||||
}
|
||||
|
||||
// Deleting
|
||||
|
@ -110,15 +108,11 @@ const deleteIt = async (id) => {
|
|||
const deleteLog = await models.deleteIt(id);
|
||||
|
||||
if (deleteLog.deletedCount === 0) {
|
||||
return { status: UNPROCESSABLE_ENTITY,
|
||||
code: CODE,
|
||||
return { status: STATUS_UNPROCESSABLE_ENTITY,
|
||||
code: CODE_INVALID_DATA,
|
||||
message: errorDeleteMsg };
|
||||
}
|
||||
|
||||
// // DEBUG:
|
||||
// console.log('SERVICES: retorno deleteLog:');
|
||||
// console.log(deleteLog);
|
||||
|
||||
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 db;
|
||||
|
||||
|
|
|
@ -1,13 +1,17 @@
|
|||
const { ObjectId } = require('mongodb');
|
||||
const models = require('../models/products');
|
||||
|
||||
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) {
|
||||
const lengthMdg = '"name" length must be at least 5 characters long';
|
||||
|
||||
if (field.length < 5) {
|
||||
return { status: UNPROCESSABLE_ENTITY, code: CODE, message: lengthMdg };
|
||||
return { status: UNPROCESSABLE_ENTITY, code: CODE_INVALID_DATA, message: lengthMdg };
|
||||
}
|
||||
|
||||
return {};
|
||||
|
@ -18,11 +22,11 @@ function quantity(qtd) {
|
|||
const typeMsg = '"quantity" must be a number';
|
||||
|
||||
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') {
|
||||
return { status: UNPROCESSABLE_ENTITY, code: CODE, message: typeMsg };
|
||||
return { status: UNPROCESSABLE_ENTITY, code: CODE_INVALID_DATA, message: typeMsg };
|
||||
}
|
||||
|
||||
return {};
|
||||
|
@ -32,14 +36,22 @@ const areadyExists = async (field, msg) => {
|
|||
const product = await models.getByName(field);
|
||||
|
||||
if (product.name) {
|
||||
return { status: UNPROCESSABLE_ENTITY, code: CODE, message: msg };
|
||||
return { status: UNPROCESSABLE_ENTITY, code: CODE_INVALID_DATA, message: msg };
|
||||
}
|
||||
|
||||
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 = {
|
||||
name,
|
||||
quantity,
|
||||
areadyExists,
|
||||
validProductId,
|
||||
};
|
Loading…
Reference in a new issue