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);
}
}); });
......
...@@ -10,48 +10,65 @@ mongoose.connect(config.db.url + '/' + config.db.name); ...@@ -10,48 +10,65 @@ mongoose.connect(config.db.url + '/' + config.db.name);
const db = mongoose.connection; const db = mongoose.connection;
db.once('open', async () => { db.once('open', async () => {
try { try {
await db.dropCollection('users'); await db.dropCollection('users');
await db.dropCollection('categories'); await db.dropCollection('categories');
await db.dropCollection('products'); await db.dropCollection('products');
} 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',
phone: '+949205234052', phone: '+949205234052',
token: '' token: ''
}, { }, {
username: 'user2', username: 'user2',
password: 'user2', password: 'user2',
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({
author: user2._id, author: user2._id,
category: category1._id, category: category1._id,
title: 'some product', title: 'some product',
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