Skip to content

Commit

Permalink
Remove cart controller and routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
fahimjason committed Feb 27, 2022
1 parent dc37275 commit a44a8f1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 142 deletions.
70 changes: 0 additions & 70 deletions src/modules/cart/cart.controller.js

This file was deleted.

18 changes: 0 additions & 18 deletions src/modules/cart/cart.routes.js

This file was deleted.

33 changes: 1 addition & 32 deletions src/modules/customer/customer.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,36 +258,6 @@ async function getCart(req, res) {
}
}

async function deleteFromCart(req, res) {
try {

const cart = await Cart.findOne({
where: {
customer_id: req.user.id
},
include: [
{
model: Product,
as: 'product'
}
]
});

await Cart.destroy({
where: {
customer_id: req.user.id
}
});
if (cart) await cart.update({ quantity });
res.status(201).send(('cart updated '));

} catch (err) {
console.error(err);
res.status(500).send("Internal server error!");
}

}

module.exports.login = login;
module.exports.logout = logout;
module.exports.registerCustomer = registerCustomer;
Expand All @@ -297,5 +267,4 @@ module.exports.getOrders = getOrders;
module.exports.getOrder = getOrder;
module.exports.createOrder = createOrder;
module.exports.addToCart = addToCart;
module.exports.getCart = getCart;
module.exports.deleteFromCart = deleteFromCart;
module.exports.getCart = getCart;
79 changes: 57 additions & 22 deletions src/modules/product/product.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,74 @@ const Shop = require(path.join(process.cwd(), "src/modules/shop/shop.model"));

async function getProducts(req, res) {
try {
const page = +req.query.page || 1;
const limit = +req.query.limit || 15;
const offset = (page - 1) * limit;
let { orderBy, orderType } = req.query;
orderType = orderType || 'asc';
let order = [['created_at', 'desc']];

if (orderBy) {
order.push([orderBy, orderType]);
const page = req.query.page ? req.query.page - 1 : 0;
if (page < 0) return res.status(400).send("page must be greater or equal 1");

const limit = req.query.limit ? +req.query.limit : 15;
const offset = page * limit;

const orderBy = req.query.orderBy ? req.query.orderBy : null;
const orderType = req.query.orderType === "asc" || req.query.orderType === "desc" ? req.query.orderType : "asc";

const order = [
["created_at", "DESC"],
["id", "DESC"]
];

const sortableColumns = [
"name",
"price",
"description",
"discount",
"stock_quantity",
"created_at"
];

if (orderBy && sortableColumns.includes(orderBy)) {
order.splice(0, 0, [orderBy, orderType]);
}

if (orderBy === "shop") {
order.splice(0, 0, [
{ model: Shop, as: "shop" },
"name",
orderType
]);

}

if (orderBy === "category") {
order.splice(0, 0, [
{ model: Category, as: "category" },
"name",
orderType
]);
}

// const filterOptions = { id: { [Op.ne]: req.user.id } };

const products = await Product.findAll({
offset,
limit,
order,
include: [
{
model: Shop,
as: 'shop'
}
],
offset,
limit,
order
]
});

const total = await Product.count();
const totalProducts = await Product.count();

const data = {
products,
meta: {
start: offset + 1,
end: Math.min(total, page * limit),
total,
page
metaData: {
page: page + 1,
limit: limit,
total: totalProducts,
start: limit * page + 1,
end: offset + limit > totalProducts ? totalProducts : offset + limit,
}
};

Expand All @@ -48,11 +84,9 @@ async function getProducts(req, res) {

async function getProduct(req, res) {
try {
const { id } = req.params;

const product = await Product.findOne({
where: {
id
id: req.params.id
},
include: [
{
Expand All @@ -61,6 +95,7 @@ async function getProduct(req, res) {
}
]
});

if (!product) return res.status(404).send("Product not found.");

res.status(200).send(product);
Expand Down

0 comments on commit a44a8f1

Please sign in to comment.