Advertisement
Guest User

Untitled

a guest
Apr 11th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. var express = require('express');
  2. var router = express.Router();
  3. var EventEmitter = require('events');
  4. var util = require('util');
  5. var itemController = require('../controllers/itemController');
  6.  
  7. const { Pool } = require('pg')
  8. const pool = new Pool({
  9. user: 'postgres',
  10. host: 'localhost',
  11. database: 'stuffsharing',
  12. password: 'postgres',
  13. port: 5432,
  14. });
  15.  
  16. const sqlQuery = 'SELECT * from Stuff where stuffid=$1';
  17. const borrowQuery = 'INSERT INTO Transactions(transId, loaner, loanee, stuffid, loanerNum, loanerEmail, startDate, endDate, status, cost, bid) values($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)'
  18.  
  19. function DbEventEmitter(){
  20. EventEmitter.call(this);
  21. }
  22.  
  23. util.inherits(DbEventEmitter, EventEmitter);
  24. var dbEventEmitter = new DbEventEmitter;
  25.  
  26. router.get('/', isLoggedIn, function(req, res) {
  27. const id = req.query.stuffId;
  28. const user = req.app.get('current user');
  29. let displayMsg = "";
  30. let isBorrowed = false;
  31.  
  32. pool.query('SELECT stuffid FROM Transactions WHERE $1 = stuffid AND $2 = status EXCEPT (SELECT ' +
  33. 'stuffId FROM Services WHERE $1 = stuffId UNION SELECT stuffId FROM Intangibles WHERE ' +
  34. '$1 = stuffId);', [id, 'ONGOING'], (err, datum) => {
  35. if (err) {
  36. console.error("Error executing query", err.stack);
  37. return 0;
  38. }
  39.  
  40. if(datum.rowCount == 0) {
  41. displayMsg = "Wanna borrow it? Fill out our form and click borrow now!";
  42. } else {
  43. displayMsg = "Sorry, this item is currently borrowed";
  44. isBorrowed = true;
  45. }
  46.  
  47. pool.query('SELECT * FROM Stuff WHERE $1 = stuffId;', [id], (err, ownerRes) => {
  48. if (err) {
  49. console.log("Error executing query", err.stack);
  50. return 0;
  51. }
  52. console.log(ownerRes);
  53. const loaner = ownerRes.rows[0].owner;
  54. if (loaner == req.user.rows[0].username) {
  55. displayMsg = "This item belongs to you";
  56. isBorrowed = true;
  57. }
  58. pool.query(sqlQuery, [id], (err, result) => {
  59. if (err) {
  60. console.error("Error executing query", err.stack);
  61. return 0;
  62. }
  63.  
  64. res.render('item', {
  65. title: 'Item',
  66. value: result.rows[0],
  67. displayMsg: displayMsg,
  68. isBorrowed: isBorrowed
  69. });
  70. console.log(result.rows[0]);
  71. return 0;
  72. });
  73. });
  74. });
  75. });
  76.  
  77. router.post('/borrow', function(req, res) {
  78. const stuffId = req.query.stuffId;
  79. const num = req.body.number;
  80. const email = req.body.email;
  81. const startDate = req.body.startDate;
  82. const endDate = req.body.endDate;
  83. const bid = req.body.bid;
  84. const user = req.user.rows[0].username;
  85.  
  86. pool.query('BEGIN', function(err, data1) {
  87. pool.query('SELECT MAX(transId) as transId FROM Transactions;', (err, data2) => {
  88. if (err) {
  89. return console.log(err);
  90. }
  91. var transId = data2.rows[0].transid + 1;
  92. console.log("transId = " + transId);
  93. pool.query('SELECT * FROM Stuff WHERE stuffId = $1;', [stuffId], (err, data3) => {
  94. if (err) {
  95. return console.log(err);
  96. }
  97. console.log(user + " this is the current user");
  98. const loaner = data3.rows[0].owner;
  99. const cost = data3.rows[0].price;
  100. console.log("Loaner: " + loaner);
  101. pool.query(borrowQuery, [transId, loaner, user, stuffId, num, email, startDate, endDate, "PENDING", cost, bid], (err, result) => {
  102. if (err) {
  103. return console.error("Error executing query", err.stack);
  104. }
  105.  
  106. // Define the event handlers for each channel name
  107. dbEventEmitter.on('overdue_loan', (msg) => {
  108. // Custom logic for reacting to the event e.g. firing a webhook, writing a log entry etc
  109. console.log('Overdue received: ' + msg);
  110. });
  111.  
  112. // Connect to Postgres (replace with your own connection string)
  113. Pool.connect('postgres://postgres:postgres@localhost:5432/stuffsharing', function(err, client) {
  114. if(err) {
  115. console.log(err);
  116. }
  117.  
  118. // Listen for all pg_notify channel messages
  119. client.on('notification', function(msg) {
  120. let payload = JSON.parse(msg.payload);
  121. dbEventEmitter.emit(msg.channel, payload);
  122. });
  123.  
  124. // Designate which channels we are listening on. Add additional channels with multiple lines.
  125. client.query('LISTEN overdue_loan');
  126. });
  127.  
  128. pool.query('COMMIT', function(err, data4) {
  129. if(err) console.log('error1');
  130. });
  131.  
  132. res.redirect('/');
  133. return console.log(result.rows);
  134. });
  135. });
  136. });
  137. });
  138. });
  139.  
  140. // route middleware to make sure a user is logged in
  141. function isLoggedIn(req, res, next) {
  142.  
  143. // if user is authenticated in the session, carry on
  144. if (req.isAuthenticated())
  145. return next();
  146.  
  147. // if they aren't redirect them to the home page
  148. res.redirect('/login');
  149. }
  150.  
  151. module.exports = router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement