Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/UitsHabib/shop-on
Browse files Browse the repository at this point in the history
  • Loading branch information
HranikBs23 committed Feb 27, 2022
2 parents 5d4e437 + a44a8f1 commit fcd6d4c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 136 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.

Empty file removed src/modules/cart/cart.schema.js
Empty file.
52 changes: 26 additions & 26 deletions src/modules/customer/customer.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function login(req, res) {
try {
const { email, password } = req.body;

const customer = await Customer.findOne({ where: { email }});
const customer = await Customer.findOne({ where: { email } });

if (!customer || !customer.password || !customer.validPassword(password)) return res.status(400).send("Invalid email or password!");

Expand Down Expand Up @@ -52,9 +52,9 @@ const registerCustomer = async (req, res) => {
}
};

async function getSignedInCustomerProfile (req, res) {
async function getSignedInCustomerProfile(req, res) {
try {
const customer = await Customer.findOne({ where: { id: req.user.id }});
const customer = await Customer.findOne({ where: { id: req.user.id } });

if (!customer) return res.status(404).send("Customer not found!");

Expand All @@ -65,17 +65,17 @@ async function getSignedInCustomerProfile (req, res) {
}
}

async function updateSignedInCustomerProfile (req, res) {
async function updateSignedInCustomerProfile(req, res) {
try {
const { first_name, last_name, username, email, phone } = req.body;

const customer = await Customer.findOne({ where: { id: req.user.id }});
const customer = await Customer.findOne({ where: { id: req.user.id } });

if (!customer) return res.status(404).send("Customer not found!");

await customer.update({ first_name, last_name, username, email, phone });

if(req.file?.path) {
if (req.file?.path) {
const file_url = await cloudinary.uploader.upload(req.file.path);
await customer.update({ avatar_url: file_url.secure_url });
}
Expand All @@ -87,63 +87,63 @@ async function updateSignedInCustomerProfile (req, res) {
}
}

async function getOrders (req, res) {
async function getOrders(req, res) {
try {
const orders = await Order.findAll({
where: { customer_id: req.user.id },
include: [{
model: OrderProduct,
as: "order_products",
attributes: ["id"],
attributes: ["id"],
include: [{
model: Product,
as: "product",
attributes: ["id", "name", "price"],
include: [{
model: Shop,
as: 'shop'
}]
}]
}]
}]
});

res.status(200).send(orders);

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

async function getOrder (req, res) {
async function getOrder(req, res) {
try {
const order = await Order.findOne({
where: { id: req.params.id },
include: [{
model: OrderProduct,
as: "order_products",
attributes: ["id"],
attributes: ["id"],
include: [{
model: Product,
as: "product",
attributes: ["id", "name", "price"],
include: [{
model: Shop,
as: 'shop'
}]
}]
}]
}]
});

res.status(200).send(order);

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

async function createOrder (req, res) {
async function createOrder(req, res) {
try {
const carts = await Cart.findAll({
where: {
Expand Down Expand Up @@ -207,12 +207,12 @@ async function createOrder (req, res) {
}
}

async function addToCart (req, res) {
async function addToCart(req, res) {
try {
const { product_id, quantity } = req.body;

const [cart, created] = await Cart.findOrCreate({
where: {
where: {
customer_id: req.user.id,
product_id
},
Expand All @@ -223,7 +223,7 @@ async function addToCart (req, res) {
},
});

if(cart) await cart.update({ quantity });
if (cart) await cart.update({ quantity });

res.status(201).send((cart || created));
}
Expand All @@ -233,11 +233,11 @@ async function addToCart (req, res) {
}
}

async function getCart (req, res) {
async function getCart(req, res) {
try {
const cart = await Cart.findAll({
where: {
customer_id: req.user.id
const cart = await Cart.findAll({
where: {
customer_id: req.user.id
},
include: [{
model: Product,
Expand All @@ -246,7 +246,7 @@ async function getCart (req, res) {
include: [{
model: Shop,
as: 'shop'
}]
}]
}]
});

Expand Down
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 fcd6d4c

Please sign in to comment.