Skip to content

Commit 8951fd0

Browse files
committed
test routinsert shoppingcart and tes 1
1 parent 6807a87 commit 8951fd0

File tree

7 files changed

+53
-3
lines changed

7 files changed

+53
-3
lines changed

app.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const app = express();
33
const morgan = require('morgan');
44
const bodyParser = require('body-parser')
55

6+
const ShoppingCart = require('./routes/shoppingcart')
67
const ProductRoute = require('./routes/products')
78
const CategoryRoute = require('./routes/category')
89
const OrderRoute = require('./routes/orders')
@@ -37,6 +38,7 @@ app.use('/category', CategoryRoute);
3738
app.use('/orders', OrderRoute);
3839
app.use('/images', ImagesRoute);
3940
app.use('/user', userRoute);
41+
app.use('/shoppingcart', ShoppingCart);
4042

4143
// Quando não encontra rota, entra aqui:
4244
app.use((req, res, next) => {

controllers/category-controller.js

+22
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,26 @@ exports.updateCategory = async (req, res, next) => {
5656
} catch (error) {
5757
return res.status(500).send({ error: error });
5858
}
59+
};
60+
61+
62+
// ---------------- Sistem --------------------------------\\
63+
exports.getCategorySistem = async (req, res, next ) => {
64+
try {
65+
const results = await mysql.execute('SELECT * FROM tbl_category;')
66+
if (results.length < 1) {
67+
return res.status(204).send({ message: 'No Category registerd' })
68+
}else{
69+
return res.status(200).send(results.map(cat => {
70+
return {
71+
cd_cat: cat.cd_cat,
72+
nm_cat: cat.nm_cat,
73+
img_cat: process.env.URL_API + cat.img_cat
74+
}
75+
}))
76+
}
77+
78+
} catch (error) {
79+
return res.status(500).send({error: error})
80+
}
5981
};
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const mysql = require('../mysql')
2+
3+
4+
exports.postCategory = async (req, res, next ) => {
5+
try {
6+
const query = 'INSERT INTO tbl_shoppingcart(email_user, cd_prod, qt_prod) VALUES (?,?,?)'
7+
const result = await mysql.execute(query, [ req.params.email_user, req.params.cd_prod, req.params.qt_prod ])
8+
return res.status(201).send({
9+
email_user: result.email_user,
10+
cd_prod: req.params.cd_prod,
11+
qt_prod: req.params.qt_prod
12+
});
13+
} catch (error) {
14+
return res.status(500).send({error: error})
15+
}
16+
};

database/cfc_script.sql

+1-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ select * from tbl_menu;
7474
DROP table tbl_menu;
7575

7676
create table tbl_shoppingcart(
77-
cd_cart int primary key auto_increment,
78-
email_user varchar(256) not null,
77+
email_user varchar(256) primary key not null,
7978
cd_prod int not null,
8079
qt_prod int not null,
8180
FOREIGN KEY(cd_prod) REFERENCES tbl_menu (cd_prod)

nodemon.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"env" : {
33
"MYSQL_USER" : "root",
4-
"MYSQL_PASSWORD" : "Kaua2004",
4+
"MYSQL_PASSWORD" : "kaua2004",
55
"MYSQL_DATABASE" : "db_cfc",
66
"MYSQL_HOST" : "localhost",
77
"MYSQL_PORT" : 3306,

routes/category.js

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const upload = multer({
3232
// Get all category
3333
router.get('/', CategoryController.getCategory)
3434

35+
router.get('/sistema', CategoryController.getCategorySistem)
36+
3537
// Insert new category
3638
router.post('/insert/:nm_cat', upload.single('img_cat'),
3739
CategoryController.postCategory)

routes/shoppingcart.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const express = require('express');
2+
const router = express.Router();
3+
const ShoppingCartController = require('../controllers/shoppingcart_controller');
4+
5+
router.get('/');
6+
7+
router.post('/insert/:email_user/:cd_prod/:qt_prod', ShoppingCartController.postCategory)
8+
9+
module.exports = router;

0 commit comments

Comments
 (0)