Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var db = require("../dbconnection");
- var mysql = require("mysql");
- var parkingLog = {
- /**
- * Creates a new parkingLog.
- */
- addParkingLog: function (id, currentParked, logDate, callback) {
- this.newHistoricParkCount(id, currentParked, () => {
- this.insertParkingLog();
- })
- },
- /**
- * Checks the totalParked value of the latest dated log, to check if it should increment
- */
- newHistoricParkCount: function(id, currentParked, callback) {
- this.getAParkingLotsLatestParkingLog(id, (err, rows) => {
- let old;
- let increment = 0;
- if (err) {
- old = parseRowDataIntoSingleEntity(rows);
- }
- else {
- old = parseRowDataIntoSingleEntity(rows);
- }
- if (currentParked > old.currentParked) {
- increment = currentParked - old.currentParked;
- }
- let historicParkCount = old.historicParkCount + increment;
- console.log("historicParkCount " + historicParkCount);
- console.log("Callback(historicParkCount)"); // prints a valid number
- callback(id, currentParked, historicParkCount);
- });
- }
- /**
- * Insert parkingLog, do not call this method directly.
- */
- insertParkingLog: function(id, currentParked, historicParkCount, callback) {
- console.log("id: " + id); // prints undefined
- console.log("currentParked: " + currentParked); // prints undefined
- console.log("historicParkCount: " + historicParkCount); // prints undefined
- if (typeof logDate === "undefined") {
- // console.log("inside if");
- var query = "INSERT INTO ??(??,??) VALUES (?,?,?)";
- var table = ["parkingLog", "currentParked", "parkingLot_id", "historicParkCount",
- currentParked, id, historicParkCount];
- }
- else {
- // console.log("inside else");
- var query = "INSERT INTO ??(??,??,??) VALUES (?,?,?)";
- var table = ["parkingLog", "currentParked", "parkingLot_id", "logDate", "historicParkCount",
- currentParked, id, logDate, historicParkCount];
- }
- query = mysql.format(query, table);
- db.query(query, callback);
- },
- /**
- * Returns the latest parkingLog based on logDate.
- */
- getAParkingLotsLatestParkingLog: function(parkingLot_id, callback) {
- db.query("SELECT * FROM parkingLog WHERE parkingLot_id=? ORDER BY logDate DESC LIMIT 1",
- parkingLot_id, callback);
- }
- };
- module.exports = parkingLog;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement