index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. var express = require('express');
  2. const app = require('../app');
  3. var router = express.Router();
  4. const posts = require('../db/Posts');
  5. /* GET home page. */
  6. router.get('/', function (req, res) {
  7. console.log('PASO')
  8. posts.fetchAll(`
  9. SELECT p.id, p.title, p.tags, p.status, t.count AS translations_count
  10. FROM posts p
  11. INNER JOIN translations t ON t.id = p.translation_id
  12. WHERE language = $language ORDER BY p.id ASC
  13. `,
  14. {
  15. $language: process.env.DEFAULT_LANGUAGE
  16. },
  17. function (err, rows) {
  18. console.log('PASO')
  19. if (err) {
  20. res.render('partial/error.njk', { error: err })
  21. } else {
  22. const columns = ['id', 'title', 'html', 'tags', 'status']
  23. if (Number(req.cookies.currentId)) {
  24. posts.fetchByPK(columns, req.cookies.currentId, function (err, row) {
  25. if (err) {
  26. res.render('partial/error.njk', { error: err })
  27. } else {
  28. res.render('index.njk', { rows: rows, selected: row });
  29. }
  30. }
  31. )
  32. } else {
  33. console.log('ELSE');
  34. posts.fectchFirst(columns, function (err, row) {
  35. if (err) {
  36. res.render('partial/error.njk', { error: err })
  37. } else {
  38. res.render('index.njk', { rows: rows, selected: row });
  39. }
  40. }
  41. )
  42. }
  43. }
  44. /* posts.fetchOne(`SELECT id, title, html, tags, status FROM posts WHERE ${condition} ORDER BY id ASC LIMIT 1`, param,
  45. function (err, row) {
  46. res.render('index.njk', { rows: rows, selected: row });
  47. }
  48. );*/
  49. })
  50. });
  51. module.exports = router;