mirror of
https://github.com/sergeyyarkov/digital-store.git
synced 2026-10-11 18:29:11 +03:00
main route refactor
This commit is contained in:
parent
21359dd458
commit
b74d2363b2
@ -1,17 +1,6 @@
|
||||
const ObjectID = require('mongodb').ObjectID;
|
||||
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
|
||||
function getItemsRoute(req, res, dbo) {
|
||||
if (req.query.token === 'yYNfW8ynVO18L1TW5qIkILM1WtWgrVZz') {
|
||||
@ -104,69 +93,100 @@ function getItemsRoute(req, res, dbo) {
|
||||
}
|
||||
}
|
||||
|
||||
function onePageItemRoute(req, res, dbo) {
|
||||
try {
|
||||
dbo.collection('items').find({_id: ObjectID(req.params.id)}).toArray((err, result) => {
|
||||
const item = result[0];
|
||||
try {
|
||||
dbo.collection('categories').find({title: item.category}).toArray((err, result) => {
|
||||
const category = result[0];
|
||||
res.render('main/product', {
|
||||
title: item.title,
|
||||
image: category.img,
|
||||
count: item.count,
|
||||
category: item.category[0].toUpperCase() + item.category.slice(1),
|
||||
price: item.price,
|
||||
description: item.description,
|
||||
date: item.date,
|
||||
id: item._id,
|
||||
type: category.type,
|
||||
format: category.format
|
||||
})
|
||||
});
|
||||
} catch {
|
||||
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('/', (req, res) => {
|
||||
try {
|
||||
db.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
|
||||
});
|
||||
});
|
||||
} catch (error) {
|
||||
res.render('main/404');
|
||||
}
|
||||
});
|
||||
server.get('/how-to-buy', (req, res) => {
|
||||
try {
|
||||
res.render('main/how-to-buy', {
|
||||
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', {
|
||||
title: item.title,
|
||||
image: category.img,
|
||||
count: item.count,
|
||||
category: item.category[0].toUpperCase() + item.category.slice(1),
|
||||
price: item.price,
|
||||
description: item.description,
|
||||
date: item.date,
|
||||
id: item._id,
|
||||
type: category.type,
|
||||
format: category.format
|
||||
});
|
||||
}))
|
||||
.catch((error) => {
|
||||
res.render('main/404');
|
||||
});
|
||||
});
|
||||
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