Inventory manager with the following functions:
- Add a piece of clothing (name, size, description and price)
- View inventory;
- Update product price;
- Delete product from database.
In this project was used the following tools:
- JavaScript
- React
- NodeJs
- mySQL
Along with the API's and Packages:
After download the code, run on terminal (in both folders):
npm install
Open backend folder and run:
npm install mysql express cors
on frontend folder, run:
npm start
on backend folder, run:
node index.js
- Insert function, will receive the data from fronend end send it to the backend.
//post requet, vai inserrir os dados no database
app.post('/create', (req, res) => { // rota ; font end -> back end
//variáveis do front end
/*
const name = req.body.name;
const num = req.body.num;
const desc = req.body.desc;
const price = req.body.price;
*/
const { name, num, desc, price } = req.body;
database.query('INSERT INTO produtos (`name`, `num`, `desc`, `price`) VALUES (?,?,?,?)', //insert
[name, num, desc, price], (err, result) => { //verify
if (err) {
console.log(err);
} else {
res.send("inserido!");
}
}
);
});
- Display function, will show on the screen the registeres product.
app.get('/estoque', (req, res) => {
database.query('SELECT * FROM produtos', (err, result) =>{
if (err) {
console.log(err);
} else {
res.send(result);
}
});
});
- Update function, will update the price on database.
app.put('/update', (req, res) => {
const id = req.body.id;
const price = req.body.price;
database.query("UPDATE SET produtos price = ? WHERE id = ?", [price, id], (err, result) => {
if (err) {
console.log(err);
} else {
res.send(result);
}
});
});
- Delete function.
app.delete('/delete/:id', (req, res) => { //ID será acessível
const id = req.params.id;
database.query("DELETE FROM produtos WHERE id = ?", id, (err, result) => { // ? => recebe id
if (err) {
console.log(err);
} else {
res.send(result);
}
});
});
NOTICE after add a product, for a better usage of update & delete function, refresh page are require (F5).
Átila de Freitas