Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env node
- // todo: trailing slash hard check
- const KNEX_CONNECTION = {
- client: 'pg',
- connection: {
- user: 'vek',
- password: 'vek',
- database: 'vek'
- }
- };
- const CATALOGS = [
- {name: 'Межкомнатные двери', url: '/catalog/room/'},
- {name: 'Металлические двери', url: '/catalog/metal/'},
- {name: 'Фурнитура', url: '/catalog/furniture/'}
- ];
- const CATALOG_ROOM_ID = 0;
- const CATALOG_METAL_ID = 1;
- const CATALOG_FURNITURE_ID = 2;
- const CATALOGS_MAP = {
- room: CATALOG_ROOM_ID,
- metal: CATALOG_METAL_ID,
- furniture: CATALOG_FURNITURE_ID
- };
- var koa = require('koa');
- var views = require('co-views');
- var parse = require('co-body');
- var knex = require('koa-knex');
- var inspect = require('util').inspect;
- var app = koa();
- var render = views(__dirname + '/tpl', {default: 'jade'});
- var dev = process.argv.indexOf('--production') == -1;
- if (dev) {
- app.use(require('koa-static')(__dirname + '/static'));
- }
- app.use(knex(KNEX_CONNECTION));
- app.use(function* catalog(next) {
- // todo: total parsing
- if (this.url.indexOf('/catalog/') != -1) {
- var parts = this.url.split('/').filter(val => { return val });
- var catalogPart = parts[1];
- var sectionPart = parts[2];
- var productPart = parts[3];
- var locals = {};
- locals.catalogId = CATALOGS_MAP[catalogPart];
- if (locals.catalogId === undefined) {
- return this.status = 404;
- }
- locals.catalogs = CATALOGS;
- locals.catalog = CATALOGS[locals.catalogId];
- locals.url = this.url;
- var sql, r;
- locals.menu = {};
- switch (catalogPart) {
- case 'room':
- sql = `
- SELECT name, slug
- FROM catalog_room_roombrand INNER JOIN catalog_brand ON (catalog_room_roombrand.brand_id = catalog_brand.id)
- ORDER BY name DESC`;
- r = yield this.knex.raw(sql);
- locals.menu.sections = r.rows;
- sql = `
- SELECT name, request_string
- FROM catalog_room_extrasection
- ORDER BY the_order`;
- r = yield this.knex.raw(sql);
- locals.menu.extraSections = r.rows;
- break;
- case 'metal':
- sql = `
- SELECT name, slug
- FROM catalog_metal_metalbrand INNER JOIN catalog_brand ON (catalog_metal_metalbrand.brand_id = catalog_brand.id)
- ORDER BY name DESC`;
- r = yield this.knex.raw(sql);
- locals.menu.sections = r.rows;
- sql = `
- SELECT name, request_string
- FROM catalog_metal_extrasection
- ORDER BY the_order`;
- r = yield this.knex.raw(sql);
- locals.menu.extraSections = r.rows;
- break;
- case 'furniture':
- sql = `
- SELECT name, slug
- FROM catalog_furniture_section
- ORDER BY name DESC`;
- r = yield this.knex.raw(sql);
- locals.menu.sections = r.rows;
- sql = `
- SELECT name, request_string
- FROM catalog_furniture_extrasection
- ORDER BY the_order`;
- r = yield this.knex.raw(sql);
- locals.menu.extraSections = r.rows;
- break;
- }
- if (sectionPart) {
- switch (catalogPart) {
- case 'room':
- sql = `
- SELECT name, slug
- FROM catalog_room_roombrand INNER JOIN catalog_brand ON (catalog_room_roombrand.brand_id = catalog_brand.id)
- WHERE slug = ?`;
- break;
- case 'metal':
- sql = `
- SELECT name, slug
- FROM catalog_metal_metalbrand INNER JOIN catalog_brand ON (catalog_metal_metalbrand.brand_id = catalog_brand.id)
- WHERE slug = ?`;
- break;
- case 'furniture':
- sql = `
- SELECT name, slug
- FROM catalog_furniture_section
- WHERE slug = ?`;
- }
- r = yield this.knex.raw(sql, sectionPart);
- locals.section = r.rows[0];
- }
- var where;
- if (false) {
- where = 'AND catalog_room_roomproduct.brand_id = 1';
- } else {
- where = '';
- }
- sql = `
- SELECT
- DISTINCT ON (product_slug)
- price,
- image,
- catalog_room_roomproduct.name product_name,
- catalog_room_roomproduct.slug product_slug,
- catalog_brand.name brand_name,
- catalog_brand.slug brand_slug
- FROM
- catalog_room_roomoption
- INNER JOIN catalog_articul ON catalog_room_roomoption.articul_ptr_id = catalog_articul.id
- INNER JOIN catalog_room_roomproduct ON catalog_room_roomoption.product_id = catalog_room_roomproduct.id
- INNER JOIN catalog_room_roombrand ON catalog_room_roomproduct.brand_id = catalog_room_roombrand.id
- INNER JOIN catalog_brand ON catalog_room_roombrand.brand_id = catalog_brand.id
- WHERE
- catalog_articul.is_index = true`;
- r = yield this.knex.raw(sql);
- //console.log(r.rows);
- locals.items = JSON.stringify(r.rows);
- this.body = yield render('catalog_list', locals);
- } else {
- yield next;
- }
- });
- app.use(function* salons(next) {
- if (this.url == '/salons/') {
- this.body = yield render('salons', {url: this.url, catalogs: CATALOGS});
- } else {
- yield next;
- }
- });
- app.use(function* home(next) {
- if (this.url == '/') {
- var menu = CATALOGS;
- var sql, r;
- // sections
- sql = `
- SELECT name, slug
- FROM catalog_room_roombrand INNER JOIN catalog_brand ON (catalog_room_roombrand.brand_id = catalog_brand.id)
- ORDER BY name DESC`;
- r = yield this.knex.raw(sql);
- menu[0].sections = r.rows;
- sql = `
- SELECT name, slug
- FROM catalog_metal_metalbrand INNER JOIN catalog_brand ON (catalog_metal_metalbrand.brand_id = catalog_brand.id)
- ORDER BY name DESC`;
- r = yield this.knex.raw(sql);
- menu[1].sections = r.rows;
- sql = `
- SELECT name, slug
- FROM catalog_furniture_section
- ORDER BY name DESC`;
- r = yield this.knex.raw(sql);
- menu[2].sections = r.rows;
- // extra sections
- sql = `
- SELECT name, request_string
- FROM catalog_room_extrasection
- ORDER BY the_order`;
- r = yield this.knex.raw(sql);
- menu[0].extraSections = r.rows;
- sql = `
- SELECT name, request_string
- FROM catalog_metal_extrasection
- ORDER BY the_order`;
- r = yield this.knex.raw(sql);
- menu[1].extraSections = r.rows;
- sql = `
- SELECT name, request_string
- FROM catalog_furniture_extrasection
- ORDER BY the_order`;
- r = yield this.knex.raw(sql);
- menu[2].extraSections = r.rows;
- var rows = yield this.knex.select('url', 'title', 'content').from('django_flatpage').where('url', this.url);
- if (rows.length) {
- var locals = rows[0];
- locals.menu = menu;
- locals.catalogs = CATALOGS;
- this.body = yield render('home', locals);
- }
- } else {
- yield next;
- }
- });
- app.use(function* flatpage() {
- var rows = yield this.knex.select('url', 'title', 'content').from('django_flatpage').where('url', this.url);
- if (rows.length) {
- var locals = rows[0];
- locals.url = this.url;
- locals.catalogs = CATALOGS;
- this.body = yield render('flatpage', locals);
- }
- });
- app.listen(3000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement