Skip to content

Commit

Permalink
make the cart queries
Browse files Browse the repository at this point in the history
  • Loading branch information
mohammedagha27 committed Sep 26, 2022
1 parent d950ba2 commit e43ed60
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
.env.development.local
.env.test.local
.env.production.local

.vscode
npm-debug.log*
yarn-debug.log*
yarn-error.log*
17 changes: 17 additions & 0 deletions src/controllers/cart/addCart.js
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;
11 changes: 11 additions & 0 deletions src/controllers/cart/deleteCart.js
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;
12 changes: 12 additions & 0 deletions src/controllers/cart/getCart.js
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;
6 changes: 6 additions & 0 deletions src/controllers/cart/index.js
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 };
3 changes: 2 additions & 1 deletion src/controllers/index.js
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 };
121 changes: 80 additions & 41 deletions src/database/config/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
13 changes: 13 additions & 0 deletions src/database/queries/cart/addCart.js
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;
11 changes: 11 additions & 0 deletions src/database/queries/cart/deleteCart.js
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;
11 changes: 11 additions & 0 deletions src/database/queries/cart/getCart.js
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;
5 changes: 5 additions & 0 deletions src/database/queries/cart/index.js
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 };
9 changes: 8 additions & 1 deletion src/database/queries/index.js
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,
};
8 changes: 8 additions & 0 deletions src/router/cart.js
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;
2 changes: 2 additions & 0 deletions src/router/index.js
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;

0 comments on commit e43ed60

Please sign in to comment.