Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.98 KB | None | 0 0
  1. #!/usr/bin/env node
  2.  
  3. // todo: trailing slash hard check
  4.  
  5. const KNEX_CONNECTION = {
  6. client: 'pg',
  7. connection: {
  8. user: 'vek',
  9. password: 'vek',
  10. database: 'vek'
  11. }
  12. };
  13.  
  14. const CATALOGS = [
  15. {name: 'Межкомнатные двери', url: '/catalog/room/'},
  16. {name: 'Металлические двери', url: '/catalog/metal/'},
  17. {name: 'Фурнитура', url: '/catalog/furniture/'}
  18. ];
  19. const CATALOG_ROOM_ID = 0;
  20. const CATALOG_METAL_ID = 1;
  21. const CATALOG_FURNITURE_ID = 2;
  22. const CATALOGS_MAP = {
  23. room: CATALOG_ROOM_ID,
  24. metal: CATALOG_METAL_ID,
  25. furniture: CATALOG_FURNITURE_ID
  26. };
  27.  
  28. var koa = require('koa');
  29. var views = require('co-views');
  30. var parse = require('co-body');
  31. var knex = require('koa-knex');
  32. var inspect = require('util').inspect;
  33.  
  34. var app = koa();
  35. var render = views(__dirname + '/tpl', {default: 'jade'});
  36.  
  37. var dev = process.argv.indexOf('--production') == -1;
  38.  
  39. if (dev) {
  40. app.use(require('koa-static')(__dirname + '/static'));
  41. }
  42.  
  43. app.use(knex(KNEX_CONNECTION));
  44.  
  45. app.use(function* catalog(next) {
  46. // todo: total parsing
  47. if (this.url.indexOf('/catalog/') != -1) {
  48. var parts = this.url.split('/').filter(val => { return val });
  49. var catalogPart = parts[1];
  50. var sectionPart = parts[2];
  51. var productPart = parts[3];
  52.  
  53. var locals = {};
  54. locals.catalogId = CATALOGS_MAP[catalogPart];
  55. if (locals.catalogId === undefined) {
  56. return this.status = 404;
  57. }
  58. locals.catalogs = CATALOGS;
  59. locals.catalog = CATALOGS[locals.catalogId];
  60. locals.url = this.url;
  61.  
  62. var sql, r;
  63. locals.menu = {};
  64. switch (catalogPart) {
  65. case 'room':
  66. sql = `
  67. SELECT name, slug
  68. FROM catalog_room_roombrand INNER JOIN catalog_brand ON (catalog_room_roombrand.brand_id = catalog_brand.id)
  69. ORDER BY name DESC`;
  70. r = yield this.knex.raw(sql);
  71. locals.menu.sections = r.rows;
  72. sql = `
  73. SELECT name, request_string
  74. FROM catalog_room_extrasection
  75. ORDER BY the_order`;
  76. r = yield this.knex.raw(sql);
  77. locals.menu.extraSections = r.rows;
  78. break;
  79. case 'metal':
  80. sql = `
  81. SELECT name, slug
  82. FROM catalog_metal_metalbrand INNER JOIN catalog_brand ON (catalog_metal_metalbrand.brand_id = catalog_brand.id)
  83. ORDER BY name DESC`;
  84. r = yield this.knex.raw(sql);
  85. locals.menu.sections = r.rows;
  86. sql = `
  87. SELECT name, request_string
  88. FROM catalog_metal_extrasection
  89. ORDER BY the_order`;
  90. r = yield this.knex.raw(sql);
  91. locals.menu.extraSections = r.rows;
  92. break;
  93. case 'furniture':
  94. sql = `
  95. SELECT name, slug
  96. FROM catalog_furniture_section
  97. ORDER BY name DESC`;
  98. r = yield this.knex.raw(sql);
  99. locals.menu.sections = r.rows;
  100. sql = `
  101. SELECT name, request_string
  102. FROM catalog_furniture_extrasection
  103. ORDER BY the_order`;
  104. r = yield this.knex.raw(sql);
  105. locals.menu.extraSections = r.rows;
  106. break;
  107. }
  108.  
  109. if (sectionPart) {
  110. switch (catalogPart) {
  111. case 'room':
  112. sql = `
  113. SELECT name, slug
  114. FROM catalog_room_roombrand INNER JOIN catalog_brand ON (catalog_room_roombrand.brand_id = catalog_brand.id)
  115. WHERE slug = ?`;
  116. break;
  117. case 'metal':
  118. sql = `
  119. SELECT name, slug
  120. FROM catalog_metal_metalbrand INNER JOIN catalog_brand ON (catalog_metal_metalbrand.brand_id = catalog_brand.id)
  121. WHERE slug = ?`;
  122. break;
  123. case 'furniture':
  124. sql = `
  125. SELECT name, slug
  126. FROM catalog_furniture_section
  127. WHERE slug = ?`;
  128. }
  129. r = yield this.knex.raw(sql, sectionPart);
  130. locals.section = r.rows[0];
  131. }
  132.  
  133. var where;
  134. if (false) {
  135. where = 'AND catalog_room_roomproduct.brand_id = 1';
  136. } else {
  137. where = '';
  138. }
  139. sql = `
  140. SELECT
  141. DISTINCT ON (product_slug)
  142. price,
  143. image,
  144. catalog_room_roomproduct.name product_name,
  145. catalog_room_roomproduct.slug product_slug,
  146. catalog_brand.name brand_name,
  147. catalog_brand.slug brand_slug
  148. FROM
  149. catalog_room_roomoption
  150. INNER JOIN catalog_articul ON catalog_room_roomoption.articul_ptr_id = catalog_articul.id
  151. INNER JOIN catalog_room_roomproduct ON catalog_room_roomoption.product_id = catalog_room_roomproduct.id
  152. INNER JOIN catalog_room_roombrand ON catalog_room_roomproduct.brand_id = catalog_room_roombrand.id
  153. INNER JOIN catalog_brand ON catalog_room_roombrand.brand_id = catalog_brand.id
  154. WHERE
  155. catalog_articul.is_index = true`;
  156. r = yield this.knex.raw(sql);
  157. //console.log(r.rows);
  158. locals.items = JSON.stringify(r.rows);
  159.  
  160. this.body = yield render('catalog_list', locals);
  161. } else {
  162. yield next;
  163. }
  164. });
  165.  
  166. app.use(function* salons(next) {
  167. if (this.url == '/salons/') {
  168. this.body = yield render('salons', {url: this.url, catalogs: CATALOGS});
  169. } else {
  170. yield next;
  171. }
  172. });
  173.  
  174. app.use(function* home(next) {
  175. if (this.url == '/') {
  176. var menu = CATALOGS;
  177. var sql, r;
  178.  
  179. // sections
  180.  
  181. sql = `
  182. SELECT name, slug
  183. FROM catalog_room_roombrand INNER JOIN catalog_brand ON (catalog_room_roombrand.brand_id = catalog_brand.id)
  184. ORDER BY name DESC`;
  185. r = yield this.knex.raw(sql);
  186. menu[0].sections = r.rows;
  187.  
  188. sql = `
  189. SELECT name, slug
  190. FROM catalog_metal_metalbrand INNER JOIN catalog_brand ON (catalog_metal_metalbrand.brand_id = catalog_brand.id)
  191. ORDER BY name DESC`;
  192. r = yield this.knex.raw(sql);
  193. menu[1].sections = r.rows;
  194.  
  195. sql = `
  196. SELECT name, slug
  197. FROM catalog_furniture_section
  198. ORDER BY name DESC`;
  199. r = yield this.knex.raw(sql);
  200. menu[2].sections = r.rows;
  201.  
  202. // extra sections
  203.  
  204. sql = `
  205. SELECT name, request_string
  206. FROM catalog_room_extrasection
  207. ORDER BY the_order`;
  208. r = yield this.knex.raw(sql);
  209. menu[0].extraSections = r.rows;
  210.  
  211. sql = `
  212. SELECT name, request_string
  213. FROM catalog_metal_extrasection
  214. ORDER BY the_order`;
  215. r = yield this.knex.raw(sql);
  216. menu[1].extraSections = r.rows;
  217.  
  218. sql = `
  219. SELECT name, request_string
  220. FROM catalog_furniture_extrasection
  221. ORDER BY the_order`;
  222. r = yield this.knex.raw(sql);
  223. menu[2].extraSections = r.rows;
  224.  
  225. var rows = yield this.knex.select('url', 'title', 'content').from('django_flatpage').where('url', this.url);
  226. if (rows.length) {
  227. var locals = rows[0];
  228. locals.menu = menu;
  229. locals.catalogs = CATALOGS;
  230. this.body = yield render('home', locals);
  231. }
  232. } else {
  233. yield next;
  234. }
  235. });
  236.  
  237. app.use(function* flatpage() {
  238. var rows = yield this.knex.select('url', 'title', 'content').from('django_flatpage').where('url', this.url);
  239. if (rows.length) {
  240. var locals = rows[0];
  241. locals.url = this.url;
  242. locals.catalogs = CATALOGS;
  243. this.body = yield render('flatpage', locals);
  244. }
  245. });
  246.  
  247. app.listen(3000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement