mirror of
https://github.com/sergeyyarkov/digital-store.git
synced 2026-10-11 10:19:15 +03:00
main route refactor
This commit is contained in:
parent
21359dd458
commit
b74d2363b2
@ -1,17 +1,6 @@
|
|||||||
const ObjectID = require('mongodb').ObjectID;
|
const ObjectID = require('mongodb').ObjectID;
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
function indexRoute(req, res, dbo) {
|
|
||||||
dbo.collection('categories').find().toArray((err, result) => {
|
|
||||||
const categories = result.map(category => category.title[0].toUpperCase() + category.title.slice(1));
|
|
||||||
res.render('main/index', {
|
|
||||||
pageName: 'index',
|
|
||||||
title: 'Digital-Store | Главная',
|
|
||||||
categories
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// API
|
// API
|
||||||
function getItemsRoute(req, res, dbo) {
|
function getItemsRoute(req, res, dbo) {
|
||||||
if (req.query.token === 'yYNfW8ynVO18L1TW5qIkILM1WtWgrVZz') {
|
if (req.query.token === 'yYNfW8ynVO18L1TW5qIkILM1WtWgrVZz') {
|
||||||
@ -104,13 +93,84 @@ function getItemsRoute(req, res, dbo) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function onePageItemRoute(req, res, dbo) {
|
module.exports = function (server, db) {
|
||||||
|
server.get('/', (req, res) => {
|
||||||
try {
|
try {
|
||||||
dbo.collection('items').find({_id: ObjectID(req.params.id)}).toArray((err, result) => {
|
db.collection('categories').find().toArray((err, result) => {
|
||||||
const item = result[0];
|
const categories = result.map(category => category.title[0].toUpperCase() + category.title.slice(1));
|
||||||
|
res.render('main/index', {
|
||||||
|
pageName: 'index',
|
||||||
|
title: 'Digital-Store | Главная',
|
||||||
|
categories
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.render('main/404');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
server.get('/how-to-buy', (req, res) => {
|
||||||
try {
|
try {
|
||||||
dbo.collection('categories').find({title: item.category}).toArray((err, result) => {
|
res.render('main/how-to-buy', {
|
||||||
const category = result[0];
|
pageName: 'how-to-buy',
|
||||||
|
title: 'Digital-Store | Как купить товар'
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.send('main/404');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
server.get('/contacts', (req, res) => {
|
||||||
|
try {
|
||||||
|
res.render('main/contacts', {
|
||||||
|
pageName: 'contacts',
|
||||||
|
title: 'Digital-Store | Контакты'
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.send('main/404');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
server.get('/comments', (req, res) => {
|
||||||
|
try {
|
||||||
|
res.render('main/comments', {
|
||||||
|
pageName: 'comments',
|
||||||
|
title: 'Digital-Store | Отзывы'
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.render('main/404');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
server.get('/my-orders', (req, res) => {
|
||||||
|
try {
|
||||||
|
res.render('main/my-orders', {
|
||||||
|
pageName: 'my-orders',
|
||||||
|
title: 'Digital-Store | Мои покупки'
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
res.render('main/404');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
server.get('/product/:id', async (req, res) => {
|
||||||
|
const product = new Promise((resolve, reject) => {
|
||||||
|
db.collection('items').find({
|
||||||
|
_id: ObjectID(req.params.id)
|
||||||
|
}).toArray()
|
||||||
|
.then((items) => {
|
||||||
|
const item = items.find(item => item);
|
||||||
|
resolve(item);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
product.then((item) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
db.collection('categories').find({title: item.category}).toArray()
|
||||||
|
.then((categories) => {
|
||||||
|
const category = categories.find(category => category);
|
||||||
|
resolve({item,category});
|
||||||
|
});
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.then(((data) => {
|
||||||
|
const item = data.item,
|
||||||
|
category = data.category;
|
||||||
res.render('main/product', {
|
res.render('main/product', {
|
||||||
title: item.title,
|
title: item.title,
|
||||||
image: category.img,
|
image: category.img,
|
||||||
@ -122,51 +182,11 @@ function onePageItemRoute(req, res, dbo) {
|
|||||||
id: item._id,
|
id: item._id,
|
||||||
type: category.type,
|
type: category.type,
|
||||||
format: category.format
|
format: category.format
|
||||||
})
|
|
||||||
});
|
});
|
||||||
} catch {
|
}))
|
||||||
|
.catch((error) => {
|
||||||
res.render('main/404');
|
res.render('main/404');
|
||||||
}
|
|
||||||
})
|
|
||||||
} catch {
|
|
||||||
res.render('main/404');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function howToBuyRoute(req, res) {
|
|
||||||
res.render('main/how-to-buy', {
|
|
||||||
pageName: 'how-to-buy',
|
|
||||||
title: 'Digital-Store | Как купить товар'
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
function contactsRoute(req, res) {
|
|
||||||
res.render('main/contacts', {
|
|
||||||
pageName: 'contacts',
|
|
||||||
title: 'Digital-Store | Контакты'
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
function commentsRoute(req, res) {
|
|
||||||
res.render('main/comments', {
|
|
||||||
pageName: 'comments',
|
|
||||||
title: 'Digital-Store | Отзывы'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function myOrdersRoute(req, res) {
|
|
||||||
res.render('main/my-orders', {
|
|
||||||
pageName: 'my-orders',
|
|
||||||
title: 'Digital-Store | Мои покупки'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = function (server, db) {
|
|
||||||
server.get('/', (req, res) => indexRoute(req, res, db));
|
|
||||||
server.get('/api', (req, res) => getItemsRoute(req, res, db));
|
server.get('/api', (req, res) => getItemsRoute(req, res, db));
|
||||||
server.get('/how-to-buy', (req, res) => howToBuyRoute(req, res, db));
|
|
||||||
server.get('/contacts', (req, res) => contactsRoute(req, res, db));
|
|
||||||
server.get('/comments', (req, res) => commentsRoute(req, res, db));
|
|
||||||
server.get('/my-orders', (req, res) => myOrdersRoute(req, res, db));
|
|
||||||
server.get('/product/:id', (req, res) => onePageItemRoute(req, res, db));
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user