Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix to fix update of the product #101

Merged
merged 1 commit into from
Jul 24, 2024
Merged
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
58 changes: 25 additions & 33 deletions src/controllers/productsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,57 +80,49 @@ export const createProduct = async (req: Request, res: Response) => {
};

export const updateProduct = async (req: Request, res: Response) => {
const transaction = await sequelize.transaction();
try {
const { productId } = req.params;

const data = {
name: req.body.name,
description: req.body.description,
colors: req.body.colors,
categoryName: req.body.categoryName,
};

// update images
if (req.files) {
const product = await Product.findByPk(productId);
const foundImages = product?.images;

// delete already existing images
if (foundImages instanceof Array) {
for (let i = 0; i < foundImages.length; i++) {
const strImg = foundImages[i].toString();
const imageId = extractImageId(strImg) as string;
await destroyImage(imageId);
product!.images = [];
}
}
// find product by id
const foundProduct = Product.findByPk(productId);

// update new images
const images: unknown = req.files;
const productImages = [];
if (images instanceof Array && images.length > 3) {
for (const image of images) {
const imageBuffer: Buffer = image.buffer;
const url = await uploadImage(imageBuffer);
productImages.push(url);
Product.update({ images: productImages }, { where: { id: productId } });
}
} else {
return res.status(400).json({
message: 'Product should have at least 4 images',
});
}
if (!foundProduct) {
return res.status(400).json({ message: 'Product not found' });
}

// update product
Product.update(data, { where: { id: productId } }).then(() => {
res.status(200).json({
const [affectedRows] = await Product.update(data, { where: { id: productId }, transaction });

// update sizes if provided
if (req.body.sizes) {
const sizes = req.body.sizes;
for (const sizeData of sizes) {
await Size.update({ ...sizeData }, { where: { productId }, transaction });
}
}

await transaction.commit();

if (affectedRows !== 0) {
return res.status(200).json({
ok: true,
message: 'Product updated successfully',
});
});
}
} catch (error) {
await transaction.rollback();
sendInternalErrorResponse(res, error);
}
};

// get size
export const getAllSizes = async (req: Request, res: Response) => {
try {
Expand Down Expand Up @@ -362,7 +354,7 @@ export const deleteProductById = async (req: Request, res: Response) => {
}

// deleting product related sizes
const sizes = await Size.findAll({ where: { id }, transaction });
await Size.findAll({ where: { id }, transaction });
await Size.destroy({ where: { id }, transaction });

// deleting the product itself
Expand Down
Loading