Commit 2fdd1578 authored by Vadim's avatar Vadim

конец занятия №82

parent 9996b974
const express = require('express');
const Category = require('./models/Category');
const router = express.Router();
const createRouter = () => {
router.get('/', async (req, res) => {
try {
const categories = await Category.find();
res.send(categories);
} catch (e) {
res.sendStatus(500);
}
});
router.post('/', async (req, res) => {
const category = new Category(req.body);
try {
await category.save();
res.send(category)
} catch (e) {
res.status(400).send(e);
}
});
return router
};
module.exports = createRouter;
\ No newline at end of file
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const CategorySchema = new Schema({
title: {
type: String,
required: true,
unique: true
},
description: String
});
const Category = mongoose.model('Category', CategorySchema);
module.exports = Category;
\ No newline at end of file
const mongoose = require('mongoose');
const idvalidator = require('mongoose-id-validator');
const Schema = mongoose.Schema;
......@@ -12,11 +13,19 @@ const ProductSchema = new Schema({
required: true
},
description: String,
image: String
image: String,
category: {
type: Schema.Types.ObjectId,
ref: 'Category',
required: true
}
}, {
versionKey: false
});
ProductSchema.plugin(idvalidator, {
message : 'Bad ID value for {PATH}'
});
const Product = mongoose.model('Product', ProductSchema);
module.exports = Product;
......@@ -18,8 +18,12 @@ const Product = require('./models/Products');
const createRouter = () => {
router.get('/', async (req, res) => {
let filter = {};
if(req.query.category){
filter.category = req.query.category;
}
try {
const products = await Product.find();
const products = await Product.find(filter).populate('category', '_id title');
res.send(products);
} catch (e) {
res.status(500).send(e);
......@@ -42,7 +46,6 @@ const createRouter = () => {
if(req.file) {
product.image = req.file.filename;
}
try {
await product.save();
res.send(product);
......
......@@ -357,6 +357,11 @@
"integrity": "sha512-y4coMcylgSCdVinjiDBuR8PCC2bLjyGTwEmPb9NHR/QaNU6EUOXcTY/s6VjGMD6ENSEaeQYHCY0GNGS5jfMwPw==",
"dev": true
},
"clone": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/clone/-/clone-1.0.4.tgz",
"integrity": "sha1-2jCcwmPfFZlMaIypAheco8fNfH4="
},
"clone-response": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/clone-response/-/clone-response-1.0.2.tgz",
......@@ -1073,6 +1078,15 @@
}
}
},
"mongoose-id-validator": {
"version": "0.6.0",
"resolved": "https://registry.npmjs.org/mongoose-id-validator/-/mongoose-id-validator-0.6.0.tgz",
"integrity": "sha512-y3b3/PkmaiMKSbKB8tsEEGUjgCgKQGpD2Ood7jaVEob3V2HWgnmNKCgiSQUpEtQuDU0lUnLJQ5JE9PH1Bytziw==",
"requires": {
"clone": "^1.0.2",
"traverse": "^0.6.6"
}
},
"mongoose-legacy-pluralize": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz",
......@@ -1670,6 +1684,11 @@
"nopt": "~1.0.10"
}
},
"traverse": {
"version": "0.6.6",
"resolved": "https://registry.npmjs.org/traverse/-/traverse-0.6.6.tgz",
"integrity": "sha1-y99WD9e5r2MlAv7UD5GMFX6pcTc="
},
"type-fest": {
"version": "0.8.1",
"resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz",
......
......@@ -14,6 +14,7 @@
"express": "^4.17.1",
"mongodb": "^3.6.3",
"mongoose": "^5.11.8",
"mongoose-id-validator": "^0.6.0",
"multer": "^1.4.2",
"mysql": "^2.18.1",
"nanoid": "^3.1.18"
......
const express = require('express');
const products = require('./app/products');
const cors = require('cors');
const config = require('./app/config');
const mongoose = require('mongoose');
const products = require('./app/products');
const categories = require('./app/categories');
const app = express();
const port = 8000;
......@@ -21,6 +22,7 @@ const run = async () => {
await mongoose.connect("mongodb://localhost/shop", {useNewUrlParser: true});
app.use('/products', products());
app.use('/categories', categories());
app.listen(port, () => {
console.log(`Server started on port ${port}`)
});
......
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