-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
d950ba2
commit e43ed60
Showing
14 changed files
with
187 additions
and
44 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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* eslint-disable no-unused-vars */ | ||
const { addCartQuery } = require('../../database/queries'); | ||
|
||
const addCart = (req, res) => { | ||
console.log(req.body); | ||
const { userId, productId, quantity } = req.body; | ||
const q = quantity || 1; | ||
addCartQuery({ userId, productId, q }) | ||
.then((data) => res.json(data)) | ||
.catch((err) => { | ||
console.log(err); | ||
res | ||
.status(500) | ||
.json({ mag: 'couldnt add product to cart!, Please Try again' }); | ||
}); | ||
}; | ||
module.exports = addCart; |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
const { deleteCartQuery } = require('../../database/queries'); | ||
|
||
/* eslint-disable no-unused-vars */ | ||
const deleteCart = (req, res) => { | ||
const userId = 1; | ||
const { productId } = req.params; | ||
deleteCartQuery({ userId, productId }).then((data) => | ||
res.json({ msg: 'cart deleted' }) | ||
); | ||
}; | ||
module.exports = deleteCart; |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* eslint-disable no-unused-vars */ | ||
const { getCartQuery } = require('../../database/queries'); | ||
|
||
const getCart = (req, res) => { | ||
const { userId } = req.params; | ||
getCartQuery(userId) | ||
.then((data) => res.send(data)) | ||
.catch((err) => | ||
res.status(500).json({ mag: 'couldnt get your cart,Please Try again' }) | ||
); | ||
}; | ||
module.exports = getCart; |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/* eslint-disable no-unused-vars */ | ||
const getCart = require('./getCart'); | ||
const addCart = require('./addCart'); | ||
const deleteCart = require('./deleteCart'); | ||
|
||
module.exports = { getCart, addCart, deleteCart }; |
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,3 +1,4 @@ | ||
const { signup } = require('./auth'); | ||
const { getCart, addCart, deleteCart, updateCart } = require('./cart'); | ||
|
||
module.exports = { signup }; | ||
module.exports = { getCart, addCart, deleteCart, updateCart, signup }; |
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 |
---|---|---|
|
@@ -2,46 +2,85 @@ BEGIN; | |
|
||
drop table if exists users, products, categories, cart cascade; | ||
|
||
CREATE TABLE users ( | ||
id SERIAL PRIMARY KEY, | ||
username VARCHAR(200) UNIQUE NOT NULL, | ||
email VARCHAR(200) UNIQUE NOT NULL, | ||
password TEXT NOT NULL, | ||
avatar TEXT | ||
); | ||
CREATE TABLE categories ( | ||
id SERIAL PRIMARY KEY, | ||
name VARCHAR(200) | ||
); | ||
CREATE TABLE products ( | ||
id SERIAL PRIMARY KEY, | ||
name VARCHAR(200), | ||
image TEXT, | ||
price decimal, | ||
categoryId INT, | ||
CONSTRAINT FKcategoryId FOREIGN KEY (categoryId) REFERENCES categories(id) ON DELETE CASCADE | ||
); | ||
CREATE TABLE cart ( | ||
userId INT NOT NULL, | ||
productId INT NOT NULL, | ||
quantity INT DEFAULT 1, | ||
timeAdded TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
CONSTRAINT FKuserId FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE, | ||
CONSTRAINT FKproductId FOREIGN KEY (productId) REFERENCES users(id) ON DELETE CASCADE | ||
); | ||
|
||
INSERT INTO users (email, username, password, avatar) VALUES | ||
('[email protected]', 'Katty', '$2a$10$Q4WJ8gPjyvinnEawSsguju/DK7xUSYPQigBoRDGuY6bKT2IWjjsB2', 'https://images.pexels.com/photos/1276553/pexels-photo-1276553.jpeg?auto=compress&cs=tinysrgb&w=600'), | ||
('[email protected]','Example', '$2a$10$Q4WJ8gPjyvinnEawSsguju/DK7xUSYPQigBoRDGuY6bKT2IWjjsB2', 'https://images.pexels.com/photos/3755755/pexels-photo-3755755.jpeg?auto=compress&cs=tinysrgb&w=600'); | ||
|
||
INSERT INTO categories (name) VALUES ('Smart watches'), ('Wall watches'); | ||
|
||
INSERT INTO products (name, image, price, categoryId) VALUES | ||
('Wyze watch', 'https://cdn.shopify.com/s/files/1/0580/0450/4738/products/47cblue3.png?v=1651261060&width=1500', 50, 1), | ||
('Apple watch', 'https://msdrnews.com/wp-content/uploads/2022/08/Apple-Watch.jpg', 200, 1), | ||
('Wall watch', 'https://i.pinimg.com/originals/ef/67/06/ef67067010009c6c604047735ab3dbb4.jpg', 20, 2); | ||
|
||
|
||
INSERT INTO cart (userId, productId, quantity) VALUES (1, 1, 1), (1, 2, 3), (2, 1, 2); | ||
CREATE TABLE | ||
users ( | ||
id SERIAL PRIMARY KEY, | ||
username VARCHAR(200) UNIQUE NOT NULL, | ||
email VARCHAR(200) UNIQUE NOT NULL, | ||
password TEXT NOT NULL, | ||
avatar TEXT | ||
); | ||
|
||
CREATE TABLE | ||
categories ( | ||
id SERIAL PRIMARY KEY, | ||
name VARCHAR(200) | ||
); | ||
|
||
CREATE TABLE | ||
products ( | ||
id SERIAL PRIMARY KEY, | ||
name VARCHAR(200), | ||
image TEXT, | ||
price decimal, | ||
categoryId INT, | ||
CONSTRAINT FKcategoryId FOREIGN KEY (categoryId) REFERENCES categories(id) ON DELETE CASCADE | ||
); | ||
|
||
CREATE TABLE | ||
cart ( | ||
userId INT NOT NULL, | ||
productId INT NOT NULL, | ||
quantity INT DEFAULT 1, | ||
timeAdded TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
CONSTRAINT FKuserId FOREIGN KEY (userId) REFERENCES users(id) ON DELETE CASCADE, | ||
CONSTRAINT FKproductId FOREIGN KEY (productId) REFERENCES users(id) ON DELETE CASCADE, | ||
PRIMARY KEY (userId, productId) | ||
); | ||
|
||
INSERT INTO | ||
users ( | ||
email, | ||
username, | ||
password, | ||
avatar | ||
) | ||
VALUES ( | ||
'[email protected]', | ||
'Katty', | ||
'$2a$10$Q4WJ8gPjyvinnEawSsguju/DK7xUSYPQigBoRDGuY6bKT2IWjjsB2', | ||
'https://images.pexels.com/photos/1276553/pexels-photo-1276553.jpeg?auto=compress&cs=tinysrgb&w=600' | ||
), ( | ||
'[email protected]', | ||
'Example', | ||
'$2a$10$Q4WJ8gPjyvinnEawSsguju/DK7xUSYPQigBoRDGuY6bKT2IWjjsB2', | ||
'https://images.pexels.com/photos/3755755/pexels-photo-3755755.jpeg?auto=compress&cs=tinysrgb&w=600' | ||
); | ||
|
||
INSERT INTO categories (name) | ||
VALUES ('Smart watches'), ('Wall watches'); | ||
|
||
INSERT INTO | ||
products (name, image, price, categoryId) | ||
VALUES ( | ||
'Wyze watch', | ||
'https://cdn.shopify.com/s/files/1/0580/0450/4738/products/47cblue3.png?v=1651261060&width=1500', | ||
50, | ||
1 | ||
), ( | ||
'Apple watch', | ||
'https://msdrnews.com/wp-content/uploads/2022/08/Apple-Watch.jpg', | ||
200, | ||
1 | ||
), ( | ||
'Wall watch', | ||
'https://i.pinimg.com/originals/ef/67/06/ef67067010009c6c604047735ab3dbb4.jpg', | ||
20, | ||
2 | ||
); | ||
|
||
INSERT INTO | ||
cart (userId, productId, quantity) | ||
VALUES (1, 1, 1), (2, 1, 2); | ||
|
||
COMMIT; |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* eslint-disable arrow-body-style */ | ||
const connection = require('../../config/connection'); | ||
|
||
const addCart = ({ userId, productId, q }) => { | ||
return connection.query( | ||
`INSERT INTO cart (userid, productid, quantity) | ||
VALUES ($1, $2, $3) | ||
ON CONFLICT (userid,productid) DO UPDATE SET quantity = $3`, | ||
[userId, productId, q] | ||
); | ||
}; | ||
|
||
module.exports = addCart; |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* eslint-disable arrow-body-style */ | ||
const connection = require('../../config/connection'); | ||
|
||
const deleteCart = ({ userId, productId }) => { | ||
return connection.query( | ||
'delete from cart c where c.userid=$1 and c.productid=$2 ', | ||
[userId, productId] | ||
); | ||
}; | ||
|
||
module.exports = deleteCart; |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* eslint-disable arrow-body-style */ | ||
const connection = require('../../config/connection'); | ||
|
||
const getCart = (userId) => { | ||
return connection.query( | ||
'select * from cart c inner join users u on c.userId = u.id where u.id = $1 ', | ||
[userId] | ||
); | ||
}; | ||
|
||
module.exports = getCart; |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const getCartQuery = require('./getCart'); | ||
const addCartQuery = require('./addCart'); | ||
const deleteCartQuery = require('./deleteCart'); | ||
|
||
module.exports = { getCartQuery, addCartQuery, deleteCartQuery }; |
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,3 +1,10 @@ | ||
const { addUserQuery, checkExistanceQuery } = require('./users'); | ||
const { getCartQuery, addCartQuery, deleteCartQuery } = require('./cart'); | ||
|
||
module.exports = { addUserQuery, checkExistanceQuery }; | ||
module.exports = { | ||
addUserQuery, | ||
checkExistanceQuery, | ||
getCartQuery, | ||
addCartQuery, | ||
deleteCartQuery, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const router = require('express').Router(); | ||
const { getCart, addCart, deleteCart } = require('../controllers'); | ||
|
||
router.post('/cart', addCart); | ||
router.get('/cart/:userId', getCart); | ||
router.delete('/cart/:productId', deleteCart); | ||
|
||
module.exports = router; |
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,6 +1,8 @@ | ||
const router = require('express').Router(); | ||
const AuthRoutes = require('./auth'); | ||
const CartRoutes = require('./cart'); | ||
|
||
router.use(AuthRoutes); | ||
router.use(CartRoutes); | ||
|
||
module.exports = router; |