Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions .history/server/controllers/productController_20210618082938.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import asyncHandler from 'express-async-handler';
import Product from '../models/Product.js';

// Get All Products
export const getProducts = asyncHandler(async (req, res) => {
const pageSize = 8;
const page = Number(req.query.pageNumber) || 1;

const keyword = req.query.keyword
? {
name: {
$regex: req.query.keyword,
$options: 'i',
},
}
: {};

const count = await Product.countDocuments({ ...keyword });
const products = await Product.find({ ...keyword })
.sort([['_id', 'desc']])
.limit(pageSize)
.skip(pageSize * (page - 1));

res.json({ products, page, pages: Math.ceil(count / pageSize) });
});

// Get Product By ID
export const getProductById = asyncHandler(async (req, res) => {
const product = await Product.findById(req.params.id);
res.json(product);
});

// Create Product
export const createProduct = asyncHandler(async (req, res) => {
const {
name,
price,
description,
image,
brand,
category,
countInStock,
} = req.body;

const product = new Product({
user: req.user._id,
name,
price,
image,
brand,
category,
countInStock,
description,
});

const createdProduct = await product.save();

res.status(201).json(createdProduct);
});

// Update product
export const updateProduct = asyncHandler(async (req, res) => {
const {
name,
price,
description,
image,
brand,
category,
countInStock,
} = req.body;

const product = await Product.findById(req.params.id);

if (product) {
product.name = name;
product.price = price;
product.description = description;
product.image = image;
product.brand = brand;
product.category = category;
product.countInStock = countInStock;

const updatedProduct = await product.save();

res.json(updatedProduct);
} else {
res.status(404);
throw new Error('Product not found');
}
});

// Create product review
export const createProductReview = asyncHandler(async (req, res) => {
const { rating, comment } = req.body;

const product = await Product.findById(req.params.id);

if (product) {
const alreadyReviewed = product.reviews.find(
r => r.user.toString() === req.user._id.toString()
);

if (alreadyReviewed) {
res.status(400);
throw new Error('Product already reviewed');
}
console.log(req.user);

const review = {
name: `${req.user.firstName} ${req.user.lastName && req.user.lastName}`,
avatar: req.user.avatar,
comment,
rating: Number(rating),
user: req.user._id,
};

product.reviews.push(review);

product.numReviews = product.reviews.length;
product.rating =
product.reviews.reduce((acc, item) => item.rating + acc, 0) /
product.reviews.length;

await product.save();
res.status(201).json({ message: 'Review added' });
} else {
res.status(404);
throw new Error('Product not found');
}
});

// Delete Product
export const deleteProduct = asyncHandler(async (req, res) => {
const product = await Product.findById(req.params.id);
if (product) {
await product.remove();
res.json({ message: 'Product Deleted Successfully' });
} else {
res.status(400);
throw new Error('Product not found');
}
});
144 changes: 144 additions & 0 deletions .history/server/controllers/productController_20210618084019.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import asyncHandler from 'express-async-handler';
import Product from '../models/Product.js';
import {validationProduct} from '../utils/validationsData';

// Get All Products
export const getProducts = asyncHandler(async (req, res) => {
const pageSize = 8;
const page = Number(req.query.pageNumber) || 1;

const keyword = req.query.keyword
? {
name: {
$regex: req.query.keyword,
$options: 'i',
},
}
: {};

const count = await Product.countDocuments({ ...keyword });
const products = await Product.find({ ...keyword })
.sort([['_id', 'desc']])
.limit(pageSize)
.skip(pageSize * (page - 1));

res.json({ products, page, pages: Math.ceil(count / pageSize) });
});

// Get Product By ID
export const getProductById = asyncHandler(async (req, res) => {
const product = await Product.findById(req.params.id);
res.json(product);
});

// Create Product
export const createProduct = asyncHandler(async (req, res) => {
const {
name,
price,
description,
image,
brand,
category,
countInStock,
} = req.body;

const product = new Product({
user: req.user._id,
name,
price,
image,
brand,
category,
countInStock,
description,
});

const createdProduct = await product.save();

res.status(201).json(createdProduct);
});

// Update product
export const updateProduct = asyncHandler(async (req, res) => {
const {
name,
price,
description,
image,
brand,
category,
countInStock,
} = req.body;

const product = await Product.findById(req.params.id);

if (product) {
product.name = name;
product.price = price;
product.description = description;
product.image = image;
product.brand = brand;
product.category = category;
product.countInStock = countInStock;

const updatedProduct = await product.save();

res.json(updatedProduct);
} else {
res.status(404);
throw new Error('Product not found');
}
});

// Create product review
export const createProductReview = asyncHandler(async (req, res) => {
const { rating, comment } = req.body;

const product = await Product.findById(req.params.id);

if (product) {
const alreadyReviewed = product.reviews.find(
r => r.user.toString() === req.user._id.toString()
);

if (alreadyReviewed) {
res.status(400);
throw new Error('Product already reviewed');
}
console.log(req.user);

const review = {
name: `${req.user.firstName} ${req.user.lastName && req.user.lastName}`,
avatar: req.user.avatar,
comment,
rating: Number(rating),
user: req.user._id,
};

product.reviews.push(review);

product.numReviews = product.reviews.length;
product.rating =
product.reviews.reduce((acc, item) => item.rating + acc, 0) /
product.reviews.length;

await product.save();
res.status(201).json({ message: 'Review added' });
} else {
res.status(404);
throw new Error('Product not found');
}
});

// Delete Product
export const deleteProduct = asyncHandler(async (req, res) => {
const product = await Product.findById(req.params.id);
if (product) {
await product.remove();
res.json({ message: 'Product Deleted Successfully' });
} else {
res.status(400);
throw new Error('Product not found');
}
});
Loading