0
I have a REST API application in Nodejs already organized in MVC, but I still don’t understand how to use the services and repositories in practice. Please help me to separate the layers?
//MODEL
const db = require('../db');
exports.getProducts = function() {
try {
const query = 'SELECT * FROM products';
return db.execute(query);
} catch(error){
return error;
}
};
//CONTROLLER
const ProductsModel = require('../models/Products');
exports.getProducts = async (req, res, next) => {
try {
const result = await ProductsModel.getProducts();
const response = {
length: result.length,
products: result.map(prod => {
return {
productId: prod.productId,
name: prod.name,
price: prod.price,
request: {
type: 'GET',
description: 'Retorna os detalhes de um produto específico',
url: process.env.URL_API + 'products/' + prod.productId
}
}
})
}
return res.status(200).send(response);
} catch (error) {
return res.status(500).send({ error: error });
}
};
//ROUTE
const ProductsController = require('../controllers/product-controller');
router.get('/', ProductsController.getProducts);