-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
58 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,82 @@ | ||
// /controller/product.controller.js | ||
const dataBase = require('../db/bd'); | ||
document.addEventListener('DOMContentLoaded', () => { | ||
|
||
const createProduct = (request, response) => { | ||
const { sku, titulo, descripcion, precio } = request.body; | ||
console.log("/product.controller.js"); | ||
|
||
// Validación de los datos del producto | ||
if (!sku || !titulo || !descripcion || !precio) { | ||
return response.status(400).json({ message: "Todos los campos son obligatorios" }); | ||
} | ||
const SQL = 'INSERT INTO products (sku,titulo,descripcion,precio) VALUES (?,?,?,?)'; | ||
const dataBase = require('../db/bd'); | ||
|
||
dataBase.query(SQL, [sku, titulo, descripcion, precio], (error, result) => { | ||
const createProduct = (request, response) => { | ||
const { sku, titulo, descripcion, precio } = request.body; | ||
|
||
if (error) throw error; | ||
// Validación de los datos del producto | ||
if (!sku || !titulo || !descripcion || !precio) { | ||
return response.status(400).json({ message: "Todos los campos son obligatorios" }); | ||
} | ||
const SQL = 'INSERT INTO products (sku,titulo,descripcion,precio) VALUES (?,?,?,?)'; | ||
|
||
dataBase.query(SQL, [sku, titulo, descripcion, precio], (error, result) => { | ||
|
||
if (error) throw error; | ||
|
||
response.json({ | ||
message: "Producto creado con éxito", | ||
idProduct: result.insertId | ||
response.json({ | ||
message: "Producto creado con éxito", | ||
idProduct: result.insertId | ||
}); | ||
}); | ||
}); | ||
}; | ||
}; | ||
|
||
const getAll = (request, response) => { | ||
const getAll = (request, response) => { | ||
|
||
console.log("Ruta que muestra todos los productos"); | ||
const SQL = 'SELECT * FROM products'; | ||
console.log("Ruta que muestra todos los productos"); | ||
const SQL = 'SELECT * FROM products'; | ||
|
||
dataBase.query(SQL, (error, result) => { | ||
dataBase.query(SQL, (error, result) => { | ||
|
||
// if (error) throw error; | ||
if (error) { | ||
console.error('Error al ejecutar la consulta:', error); | ||
return response.status(500).json({ error: 'Error en el servidor' }); | ||
} | ||
// if (error) throw error; | ||
if (error) { | ||
console.error('Error al ejecutar la consulta:', error); | ||
return response.status(500).json({ error: 'Error en el servidor' }); | ||
} | ||
|
||
// if (result.length === 0) { | ||
// console.log('No hay usuarios en la lista'); | ||
// return response.status(404).json({ mensaje: 'No existe ningún usuario aún' }); | ||
// } | ||
// if (result.length === 0) { | ||
// console.log('No hay usuarios en la lista'); | ||
// return response.status(404).json({ mensaje: 'No existe ningún usuario aún' }); | ||
// } | ||
|
||
response.json(result) | ||
}); | ||
}; | ||
response.json(result) | ||
}); | ||
}; | ||
|
||
const deleteProduct = (request, response) => { | ||
const { id } = request.params; | ||
const SQL = 'DELETE FROM products WHERE id = ?'; | ||
const deleteProduct = (request, response) => { | ||
const { id } = request.params; | ||
const SQL = 'DELETE FROM products WHERE id = ?'; | ||
|
||
dataBase.query(SQL, [id], (error, result) => { | ||
if (error) { | ||
return response.status(500).json({ mensaje: 'Error NO EXISTE el producto', error }); | ||
} | ||
dataBase.query(SQL, [id], (error, result) => { | ||
if (error) { | ||
return response.status(500).json({ mensaje: 'Error NO EXISTE el producto', error }); | ||
} | ||
|
||
response.json({ | ||
message: "EL productos se eliminó correctamente." | ||
response.json({ | ||
message: "EL productos se eliminó correctamente." | ||
}); | ||
}); | ||
}); | ||
}; | ||
}; | ||
|
||
const uploadProduct = (request, response) => { | ||
const { id } = request.params; | ||
const { sku, titulo, descripcion, precio } = request.body; | ||
const uploadProduct = (request, response) => { | ||
const { id } = request.params; | ||
const { sku, titulo, descripcion, precio } = request.body; | ||
|
||
const SQL = 'UPDATE products SET sku = ?, titulo = ?, descripcion = ?, precio = ? WHERE id = ?'; | ||
dataBase.query(SQL, [sku, titulo, descripcion, precio, id], (error, result) => { | ||
if (error) throw error; | ||
const SQL = 'UPDATE products SET sku = ?, titulo = ?, descripcion = ?, precio = ? WHERE id = ?'; | ||
dataBase.query(SQL, [sku, titulo, descripcion, precio, id], (error, result) => { | ||
if (error) throw error; | ||
|
||
response.json({ | ||
message: "Producto editado exitosamente!" | ||
response.json({ | ||
message: "Producto editado exitosamente!" | ||
}); | ||
}); | ||
}); | ||
}; | ||
}; | ||
|
||
module.exports = { getAll, deleteProduct, uploadProduct, createProduct } | ||
module.exports = { getAll, deleteProduct, uploadProduct, createProduct } | ||
}) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
document.addEventListener('DOMContentLoaded', () => { | ||
console.log("Aqui vemos y agregamos productos >"); | ||
|
||
|