Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var db = require("../dbconnection");
  2. var mysql = require("mysql");
  3.  
  4. var parkingLog = {
  5.  
  6.     /**
  7.      * Creates a new parkingLog.
  8.      */
  9.     addParkingLog: function (id, currentParked, logDate, callback) {
  10.         this.newHistoricParkCount(id, currentParked, () => {
  11.             this.insertParkingLog();
  12.         })
  13.     },
  14.  
  15.     /**
  16.      * Checks the totalParked value of the latest dated log, to check if it should increment
  17.      */
  18.     newHistoricParkCount: function(id, currentParked, callback) {
  19.         this.getAParkingLotsLatestParkingLog(id, (err, rows) => {
  20.             let old;
  21.             let increment = 0;
  22.             if (err) {
  23.                 old = parseRowDataIntoSingleEntity(rows);
  24.             }
  25.             else {
  26.                 old = parseRowDataIntoSingleEntity(rows);
  27.             }
  28.             if (currentParked > old.currentParked) {
  29.                 increment = currentParked - old.currentParked;
  30.             }
  31.             let historicParkCount = old.historicParkCount + increment;
  32.             console.log("historicParkCount " + historicParkCount);
  33.             console.log("Callback(historicParkCount)"); // prints a valid number
  34.             callback(id, currentParked, historicParkCount);
  35.         });
  36.     }
  37.  
  38.     /**
  39.      * Insert parkingLog, do not call this method directly.
  40.      */
  41.     insertParkingLog: function(id, currentParked, historicParkCount, callback) {
  42.         console.log("id: " + id); // prints undefined
  43.         console.log("currentParked: " + currentParked); // prints undefined
  44.         console.log("historicParkCount: " + historicParkCount); // prints undefined
  45.         if (typeof logDate === "undefined") {
  46.             //    console.log("inside if");
  47.             var query = "INSERT INTO ??(??,??) VALUES (?,?,?)";
  48.             var table = ["parkingLog", "currentParked", "parkingLot_id", "historicParkCount",
  49.                 currentParked, id, historicParkCount];
  50.         }
  51.         else {
  52.             //     console.log("inside else");
  53.             var query = "INSERT INTO ??(??,??,??) VALUES (?,?,?)";
  54.             var table = ["parkingLog", "currentParked", "parkingLot_id", "logDate", "historicParkCount",
  55.                 currentParked, id, logDate, historicParkCount];
  56.         }
  57.         query = mysql.format(query, table);
  58.         db.query(query, callback);
  59.     },
  60.  
  61.     /**
  62.      * Returns the latest parkingLog based on logDate.
  63.      */
  64.     getAParkingLotsLatestParkingLog: function(parkingLot_id, callback) {
  65.  
  66.         db.query("SELECT * FROM parkingLog WHERE parkingLot_id=? ORDER BY logDate DESC LIMIT 1",
  67.             parkingLot_id, callback);
  68.     }
  69. };
  70.  
  71. module.exports = parkingLog;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement