Commit d2e82b69 authored by zarina's avatar zarina 🌊

#4, реализовала возможность удаления товара

parent 7aca4dca
...@@ -33,24 +33,37 @@ const createRouter = () => { ...@@ -33,24 +33,37 @@ const createRouter = () => {
}); });
router.get("/:id", async (req, res) => { router.get("/:id", async (req, res) => {
const product = await Product.findById(req.params.id).populate("category"); const product = await Product.findById(req.params.id).populate('category author');
res.send(product); res.send(product);
}); });
router.post("/", [upload.single("image"), auth], async (req, res) => { router.post("/", [upload.single("image"), auth], async (req, res) => {
const product = new Product(req.body); const productData = req.body;
productData.author = req.user;
if (req.file) { if (req.file) {
product.image = req.file.filename; productData.image = req.file.filename;
} else {
productData.image = 'ic-dialog.png';
}
const product = new Product(productData);
try {
res.send(await product.save());
} catch (err) {
res.status(400).send(err);
} }
await product.save();
res.send(product);
}); });
router.delete("/:id", async (req, res) => { router.delete("/:id", auth, async (req, res) => {
try { const product = await Product.findById(req.params.id);
res.send(await Product.findByIdAndRemove(req.params.id)); if (req.user._id.toString() === product.author.toString()) {
} catch(e) {res.status(500).send(e)} await Product.findByIdAndRemove(req.params.id);
return res.send(await Product.find());
} else {
return res.sendStatus(403);
}
}); });
......
...@@ -17,7 +17,7 @@ db.once('open', async () => { ...@@ -17,7 +17,7 @@ db.once('open', async () => {
} catch (e) { } catch (e) {
console.log('Collections were not present, skipping drop...'); console.log('Collections were not present, skipping drop...');
} }
const[user1, user2] = await User.create({ const [user1, user2] = await User.create({
username: 'user', username: 'user',
password: 'user', password: 'user',
displayName: 'Some User', displayName: 'Some User',
...@@ -29,12 +29,15 @@ db.once('open', async () => { ...@@ -29,12 +29,15 @@ db.once('open', async () => {
displayName: 'Some User 2', displayName: 'Some User 2',
phone: '+324013248105', phone: '+324013248105',
token: '' token: ''
}); }
);
const [category1, category2] = await Category.create({ const [category1, category2, category3] = await Category.create({
title: "some category", title: "some category",
},{ }, {
title: "some category 2" title: "some category 2"
}, {
title: "some category 3"
}); });
await Product.create({ await Product.create({
...@@ -44,13 +47,27 @@ db.once('open', async () => { ...@@ -44,13 +47,27 @@ db.once('open', async () => {
description: 'some description for some product', description: 'some description for some product',
image: 'somePhoto.jpeg', image: 'somePhoto.jpeg',
price: 222 price: 222
},{ }, {
author: user1._id, author: user1._id,
category: category2._id, category: category2._id,
title: 'some product 2', title: 'some product 2',
description: 'some description for some product 2', description: 'some description for some product 2',
image: 'somePhoto1.jpeg', image: 'somePhoto1.jpeg',
price: 154 price: 154424
}, {
author: user1._id,
category: category2._id,
title: 'some product 3',
description: 'some description for some product 3',
image: 'somePhoto1.jpeg',
price: 154443
},{
author: user1._id,
category: category3._id,
title: 'some product 4',
description: 'some description for some product 4',
image: 'somePhoto1.jpeg',
price: 15453
}); });
db.close(); db.close();
......
* *
!.gitignore !.gitignore
!somePhoto1.jpeg
!somePhoto.jpeg
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment