Commit 9996b974 authored by Vadim's avatar Vadim

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

parent 4481e41e
...@@ -12,6 +12,9 @@ module.exports = { ...@@ -12,6 +12,9 @@ module.exports = {
console.error(e); console.error(e);
data = [] data = []
} }
},
getItem(id){
}, },
getItems() { getItems() {
return data; return data;
......
const MongoClient = require('mongodb').MongoClient;
let db = null;
let client = null;
const connect = async () => {
client = await MongoClient.connect("mongodb://localhost");
db = client.db('shop')
};
const disconnect = async () => {
await client.disconnect();
};
module.exports = {
connect,
disconnect,
getDb: () => db
};
\ No newline at end of file
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ProductSchema = new Schema({
title: {
type: String,
required: true
},
price: {
type: Number,
required: true
},
description: String,
image: String
}, {
versionKey: false
});
const Product = mongoose.model('Product', ProductSchema);
module.exports = Product;
...@@ -4,6 +4,7 @@ const path = require('path'); ...@@ -4,6 +4,7 @@ const path = require('path');
const router = express.Router(); const router = express.Router();
const {nanoid} = require('nanoid'); const {nanoid} = require('nanoid');
const config = require('./config'); const config = require('./config');
const Product = require('./models/Products');
const storage = multer.diskStorage({ const storage = multer.diskStorage({
destination: (req, file, cb) => { destination: (req, file, cb) => {
...@@ -15,25 +16,39 @@ const config = require('./config'); ...@@ -15,25 +16,39 @@ const config = require('./config');
}); });
const upload = multer({storage}); const upload = multer({storage});
const createRouter = (db) => { const createRouter = () => {
router.get('/', async (req, res) => { router.get('/', async (req, res) => {
try {
const products = await db.getItems('products'); const products = await Product.find();
res.send(products); res.send(products);
} catch (e) {
res.status(500).send(e);
}
}); });
router.get('/:id', async (req, res) => { router.get('/:id', async (req, res) => {
const product = await db.getItem('products', req.params.id); try {
res.send(product[0]); const result = await Product.findById(req.params.id);
res.send(result);
}catch (e) {
res.sendStatus(404);
}
}); });
router.post('/', upload.single('image'), async (req, res) => { router.post('/', upload.single('image'), async (req, res) => {
const product = req.body; const product = new Product(req.body);
if(req.file) { if(req.file) {
product.image = req.file.filename; product.image = req.file.filename;
} }
const newProduct = await db.addItem('products', product);
res.send(newProduct); try {
await product.save();
res.send(product);
} catch (e) {
res.status(500).send(e);
}
}); });
return router; return router;
......
This diff is collapsed.
...@@ -12,6 +12,8 @@ ...@@ -12,6 +12,8 @@
"dependencies": { "dependencies": {
"cors": "^2.8.5", "cors": "^2.8.5",
"express": "^4.17.1", "express": "^4.17.1",
"mongodb": "^3.6.3",
"mongoose": "^5.11.8",
"multer": "^1.4.2", "multer": "^1.4.2",
"mysql": "^2.18.1", "mysql": "^2.18.1",
"nanoid": "^3.1.18" "nanoid": "^3.1.18"
......
const express = require('express'); const express = require('express');
const products = require('./app/products'); const products = require('./app/products');
const cors = require('cors'); const cors = require('cors');
const mysql = require('mysql');
const config = require('./app/config'); const config = require('./app/config');
const db = require('./app/db/mysql'); const mongoose = require('mongoose');
const app = express(); const app = express();
const port = 8000; const port = 8000;
const connection = mysql.createConnection(config.db);
const corsOptions = { const corsOptions = {
origin: 'http://localhost:3000', origin: 'http://localhost:3000',
...@@ -20,16 +17,16 @@ app.use(express.json()); ...@@ -20,16 +17,16 @@ app.use(express.json());
app.use(express.static('public')); app.use(express.static('public'));
connection.connect(err => { const run = async () => {
if(err){ await mongoose.connect("mongodb://localhost/shop", {useNewUrlParser: true});
console.error(err);
throw err;
}
app.use('/products', products(db(connection)));
app.use('/products', products());
app.listen(port, () => { app.listen(port, () => {
console.log(`Server started on port ${port}`) console.log(`Server started on port ${port}`)
}); });
console.log('mysql connected!'); console.log('mongo connected!');
}); };
run().catch(console.log);
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