From fda4777ff623c3ff6d8e88fb3b3a70ebf9910b06 Mon Sep 17 00:00:00 2001 From: Zubair Ahmed Date: Tue, 2 May 2017 09:25:35 -0700 Subject: [PATCH 1/6] Day One -- design app, build basic skeleton --- .eslintrc.json | 29 +++++++++++++++++++++ Classes/passenger.js | 0 Classes/station.js | 59 ++++++++++++++++++++++++++++++++++++++++++ Classes/train.js | 0 Database/database.js | 0 Test/passenger.test.js | 0 Test/station.test.js | 0 Test/train.test.js | 0 app.js | 7 +++++ knewfile.js | 0 package.json | 34 ++++++++++++++++++++++++ 11 files changed, 129 insertions(+) create mode 100644 .eslintrc.json create mode 100644 Classes/passenger.js create mode 100644 Classes/station.js create mode 100644 Classes/train.js create mode 100644 Database/database.js create mode 100644 Test/passenger.test.js create mode 100644 Test/station.test.js create mode 100644 Test/train.test.js create mode 100644 app.js create mode 100644 knewfile.js create mode 100644 package.json diff --git a/.eslintrc.json b/.eslintrc.json new file mode 100644 index 0000000..a3159ab --- /dev/null +++ b/.eslintrc.json @@ -0,0 +1,29 @@ +{ + "env": { + "browser": true, + "es6": true, + "mocha": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "sourceType": "module" + }, + "rules": { + "indent": [ + "error", + 2 + ], + "linebreak-style": [ + "error", + "unix" + ], + "quotes": [ + "error", + "double" + ], + "semi": [ + "error", + "always" + ] + } +} diff --git a/Classes/passenger.js b/Classes/passenger.js new file mode 100644 index 0000000..e69de29 diff --git a/Classes/station.js b/Classes/station.js new file mode 100644 index 0000000..08b41ff --- /dev/null +++ b/Classes/station.js @@ -0,0 +1,59 @@ +module.exports = class Station { + constructor(locationName = "") { + this.locationName = locationName; + this.id = -1; + this.listOfPassengers = []; + this.nextStation = 0; + this.previousStation = 1; + } + + getId() { + return this.id; + } + + getLocationName() { + return this.locationName; + } + + getWaitingPassengers() { + // returns the passengers waiting in a given station + return this.listOfPassengers; + } + + getPassengersWithTickets() { + // returns passengers whose hasTicket property is true + } + + getPreviousStation() { + // returns the previous station, which it finds from the station id + return Station.findStationById(this.previousStation); + } + + getNextStation() { + // returns the previous station, which it finds from the station id + return Station.findStationById(this.nextStation); + } + + static getNextTrainOf(name) { + // returns the next train that's coming into 'name', which is the station name + } + + static findStationById(id) { + let station1 = { + id: 0, + locationName: "station1" + } + + let station2 = { + id: 1, + locationName: "station2" + } + + let arr = [station1, station2]; + return arr[id]; + } + + static findStationByLocation(locationName) { + // query to retrieve station information from database + } +} diff --git a/Classes/train.js b/Classes/train.js new file mode 100644 index 0000000..e69de29 diff --git a/Database/database.js b/Database/database.js new file mode 100644 index 0000000..e69de29 diff --git a/Test/passenger.test.js b/Test/passenger.test.js new file mode 100644 index 0000000..e69de29 diff --git a/Test/station.test.js b/Test/station.test.js new file mode 100644 index 0000000..e69de29 diff --git a/Test/train.test.js b/Test/train.test.js new file mode 100644 index 0000000..e69de29 diff --git a/app.js b/app.js new file mode 100644 index 0000000..0e4f205 --- /dev/null +++ b/app.js @@ -0,0 +1,7 @@ +const Station = require('./Classes/station'); + +let station1 = new Station("station3"); + +console.log(Station.findStationById(1)); + +console.log(station1.getNextStation()); diff --git a/knewfile.js b/knewfile.js new file mode 100644 index 0000000..e69de29 diff --git a/package.json b/package.json new file mode 100644 index 0000000..dffd852 --- /dev/null +++ b/package.json @@ -0,0 +1,34 @@ +{ + "name": "metrorail", + "version": "1.0.0", + "description": "Create a database API for a model train system", + "main": "app.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "repl": "echo 'this entered here'", + "linter": "./node_modules/.bin/eslint .", + "db:create": "echo \"Error: no create specified\" && exit 1", + "db:migrate": "echo \"Error: no migrate specified\" && exit 1", + "db:seed": "echo \"Error: no seed specified\" && exit 1", + "db:drop": "echo \"Error: no drop specified\" && exit 1", + "db:reset": "echo \"Error: no reset specified\" && exit 1", + "db:console": "echo \"Error: no console specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/zubairnahmed/metrorail.git" + }, + "author": "Zubair and Jose", + "license": "MIT", + "bugs": { + "url": "https://github.com/zubairnahmed/metrorail/issues" + }, + "homepage": "https://github.com/zubairnahmed/metrorail#readme", + "devDependencies": { + "eslint": "^3.19.0" + }, + "dependencies": { + "knex": "^0.13.0", + "pg": "^6.1.5" + } +} From a4a1ab7960a95457c0bac8baba0c300db094e69e Mon Sep 17 00:00:00 2001 From: Zubair Ahmed Date: Tue, 2 May 2017 16:23:19 -0700 Subject: [PATCH 2/6] create schema for metrorail database and setup migration --- Database/database.js | 8 +++++ app.js | 8 ++--- knewfile.js | 0 knexfile.js | 8 +++++ migrations/20170502130947_metrorail.js | 50 ++++++++++++++++++++++++++ package.json | 3 ++ 6 files changed, 71 insertions(+), 6 deletions(-) delete mode 100644 knewfile.js create mode 100644 knexfile.js create mode 100644 migrations/20170502130947_metrorail.js diff --git a/Database/database.js b/Database/database.js index e69de29..b71f4f7 100644 --- a/Database/database.js +++ b/Database/database.js @@ -0,0 +1,8 @@ +const config = require('../knexfile'); +let env = process.env.NODE_ENV || 'development'; + +let knex = require('knex')(config[env]); + +module.exports = knex; + +knex.migrate.latest([config]); diff --git a/app.js b/app.js index 0e4f205..4f0a2a3 100644 --- a/app.js +++ b/app.js @@ -1,7 +1,3 @@ -const Station = require('./Classes/station'); +const db = require('./Database/database'); -let station1 = new Station("station3"); - -console.log(Station.findStationById(1)); - -console.log(station1.getNextStation()); +// test our database API here diff --git a/knewfile.js b/knewfile.js deleted file mode 100644 index e69de29..0000000 diff --git a/knexfile.js b/knexfile.js new file mode 100644 index 0000000..3c85730 --- /dev/null +++ b/knexfile.js @@ -0,0 +1,8 @@ +module.exports = { + development: { + client: 'postgresql', + connection: { + database: 'metrorail_dev' + } + } +}; diff --git a/migrations/20170502130947_metrorail.js b/migrations/20170502130947_metrorail.js new file mode 100644 index 0000000..1d123a5 --- /dev/null +++ b/migrations/20170502130947_metrorail.js @@ -0,0 +1,50 @@ + +exports.up = function(knex, Promise) { + + return Promise.all([ + + knex.schema.createTable('trains', function(table) { + table.increments('id').primary(); + table.integer('num'); + table.integer('capacity'); + table.integer('number_of_passengers'); + table.boolean('full'); + table.integer('current_station_id') + .references('station_id') + .inTable('stations'); + }), + + knex.schema.createTable('stations', function(table){ + table.increments('id').primary(); + table.string('next_train'); + table.string('station_name'); + table.string('next_station'); + table.string('previous_station'); + }), + + knex.schema.createTable('passengers', function(table){ + table.increments('id').primary(); + table.string('name'); + table.string('destination'); + table.boolean('has_ticket'); + table.integer('current_train_id') + .references('train_id') + .inTable('trains'); + table.integer('current_station_id') + .references('station_id') + .inTable('stations'); + }) + ]) + +}; + +exports.down = function(knex, Promise) { + + return Promise.all([ + + knex.schema.dropTable('trains'), + knex.schema.dropTable('stations'), + knex.schema.dropTable('passengers') + + ]) +}; diff --git a/package.json b/package.json index dffd852..4e8c781 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "Create a database API for a model train system", "main": "app.js", "scripts": { + "start": "./node_modules/.bin/nodemon app.js --ignore node_modules/", "test": "echo \"Error: no test specified\" && exit 1", "repl": "echo 'this entered here'", "linter": "./node_modules/.bin/eslint .", @@ -28,7 +29,9 @@ "eslint": "^3.19.0" }, "dependencies": { + "express": "^4.15.2", "knex": "^0.13.0", + "nodemon": "^1.11.0", "pg": "^6.1.5" } } From a7af389dcfca1d8929bbaa49cac88036b1c7087a Mon Sep 17 00:00:00 2001 From: Zubair Ahmed Date: Wed, 3 May 2017 11:05:19 -0700 Subject: [PATCH 3/6] finish seeding initial data into database --- Database/Seeds/seed.js | 101 +++++++++++++++++++++++++ app.js | 8 +- knexfile.js | 3 + migrations/20170502130947_metrorail.js | 14 ++-- package.json | 14 ++-- 5 files changed, 124 insertions(+), 16 deletions(-) create mode 100644 Database/Seeds/seed.js diff --git a/Database/Seeds/seed.js b/Database/Seeds/seed.js new file mode 100644 index 0000000..bd1afa5 --- /dev/null +++ b/Database/Seeds/seed.js @@ -0,0 +1,101 @@ +const db = require('../database'); + +const trainData = [ + { + num: 1, + capacity: 55, + number_of_passengers: 0 + }, + { + num: 2, + capacity: 120, + number_of_passengers: 0 + }, + { + num: 3, + capacity: 23, + number_of_passengers: 0 + }, + { + num: 4, + capacity: 70, + number_of_passengers: 0 + } +]; + +const stationData = [ + { + station_name: "Downtown" + }, + { + station_name: "Elm Street" + }, + { + station_name: "Forest Gardens" + }, + { + station_name: "Annex" + }, + { + station_name: "10th Ave" + }, + { + station_name: "Waterfront" + }, + { + station_name: "Colosseum" + }, + { + station_name: "Central Station" + }, + { + station_name: "Parkside" + }, + { + station_name: "Grand Boulevard" + }, + { + station_name: "Monument Valley" + }, + { + station_name: "Museum Isle" + } +]; + +const passengerData = [ + { + name: "John Dope", + has_ticket: false + }, + { + name: "Sabrina Smith", + has_ticket: false + }, + { + name: "Anish Sadhaji", + has_ticket: false + }, + { + name: "Pooh Bear", + has_ticket: false + } +]; + +exports.seed = (knex, promise) => { + let promises = []; + stationData.forEach(data => { + promises.push(insertData(knex, data, "stations")); + }); + trainData.forEach(data => { + promises.push(insertData(knex, data, "trains")); + }); + passengerData.forEach(data => { + promises.push(insertData(knex, data, "passengers")); + }); + return Promise.all(promises); +} + +function insertData(knex, data, table) { + return knex.insert(data) + .into(table); +} diff --git a/app.js b/app.js index 4f0a2a3..85e1404 100644 --- a/app.js +++ b/app.js @@ -1,3 +1,9 @@ +const express = require('express'); +const app = express(); const db = require('./Database/database'); -// test our database API here +const port = 3000; + +app.listen(port, () => { + console.log('Server is running on port', port); +}); diff --git a/knexfile.js b/knexfile.js index 3c85730..e057722 100644 --- a/knexfile.js +++ b/knexfile.js @@ -3,6 +3,9 @@ module.exports = { client: 'postgresql', connection: { database: 'metrorail_dev' + }, + seeds: { + directory: './Database/Seeds' } } }; diff --git a/migrations/20170502130947_metrorail.js b/migrations/20170502130947_metrorail.js index 1d123a5..a03a767 100644 --- a/migrations/20170502130947_metrorail.js +++ b/migrations/20170502130947_metrorail.js @@ -8,30 +8,28 @@ exports.up = function(knex, Promise) { table.integer('num'); table.integer('capacity'); table.integer('number_of_passengers'); - table.boolean('full'); table.integer('current_station_id') - .references('station_id') + .references('id') .inTable('stations'); }), knex.schema.createTable('stations', function(table){ table.increments('id').primary(); - table.string('next_train'); table.string('station_name'); - table.string('next_station'); - table.string('previous_station'); }), + knex.raw('alter table "stations" add column "order" serial'), + knex.schema.createTable('passengers', function(table){ table.increments('id').primary(); table.string('name'); - table.string('destination'); + table.integer('destination_id'); table.boolean('has_ticket'); table.integer('current_train_id') - .references('train_id') + .references('id') .inTable('trains'); table.integer('current_station_id') - .references('station_id') + .references('id') .inTable('stations'); }) ]) diff --git a/package.json b/package.json index 4e8c781..4300b9c 100644 --- a/package.json +++ b/package.json @@ -7,13 +7,13 @@ "start": "./node_modules/.bin/nodemon app.js --ignore node_modules/", "test": "echo \"Error: no test specified\" && exit 1", "repl": "echo 'this entered here'", - "linter": "./node_modules/.bin/eslint .", - "db:create": "echo \"Error: no create specified\" && exit 1", - "db:migrate": "echo \"Error: no migrate specified\" && exit 1", - "db:seed": "echo \"Error: no seed specified\" && exit 1", - "db:drop": "echo \"Error: no drop specified\" && exit 1", - "db:reset": "echo \"Error: no reset specified\" && exit 1", - "db:console": "echo \"Error: no console specified\" && exit 1" + "linter": "node_modules/.bin/eslint .", + "db:create": "createdb metrorail_dev", + "db:migrate": "node_modules/.bin/knex migrate:latest", + "db:seed": "node_modules/.bin/knex seed:run", + "db:drop": "dropdb metrorail_dev", + "db:reset": "dropdb metrorail_dev && createdb metrorail_dev && node_modules/.bin/knex migrate:latest", + "db:console": "psql metrorail_dev" }, "repository": { "type": "git", From 23f2d65709dfcb5c06cde9a62389f85108132d02 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Wed, 3 May 2017 15:22:55 -0700 Subject: [PATCH 4/6] Complete database operations --- Database/Queries/generic.queries.js | 26 ++++++++++++++++++++++++ Database/Queries/passengers.queries.js | 28 ++++++++++++++++++++++++++ Database/Queries/stations.queries.js | 19 +++++++++++++++++ Database/Queries/trains.queries.js | 11 ++++++++++ Database/database.js | 2 -- 5 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 Database/Queries/generic.queries.js create mode 100644 Database/Queries/passengers.queries.js create mode 100644 Database/Queries/stations.queries.js create mode 100644 Database/Queries/trains.queries.js diff --git a/Database/Queries/generic.queries.js b/Database/Queries/generic.queries.js new file mode 100644 index 0000000..2b82d13 --- /dev/null +++ b/Database/Queries/generic.queries.js @@ -0,0 +1,26 @@ +const db = require("../database.js"); + +const save = (table, object) => { + return db(table) + .returning("id") + .insert(object) +} + +const update = (table, object) => { + return db(table) + .returning("id") + .where("id", "=", object.id) + .update(object); +} + +const del = (table, id) => { + return db(table) + .where("id", "=", id) + .del(); +} + +module.exports = { + save: save, + update: update, + del: del +}; diff --git a/Database/Queries/passengers.queries.js b/Database/Queries/passengers.queries.js new file mode 100644 index 0000000..2aba614 --- /dev/null +++ b/Database/Queries/passengers.queries.js @@ -0,0 +1,28 @@ +const db = require("../database.js"); + +const findPassengersByName = (name) => { + return db("passengers") + .select() + .where("name", name); +}; + +const findPassengersByStation = (stationName) => { + return db("passengers") + .join("stations", "passengers.current_station_id", "=", "stations.id") + .select("passengers.id", "passengers.name", "passengers.destination_id", + "passengers.has_ticket", "passengers.current_train_id", + "passengers.current_station_id") + .where("stations.station_name", stationName); +} + +const findPassengersByTrain = (trainId) => { + return db("passengers") + .select() + .where("current_train_id", trainId); +} + +module.exports = { + findPassengersByName: findPassengersByName, + findPassengersByStation: findPassengersByStation, + findPassengersByTrain: findPassengersByTrain +}; diff --git a/Database/Queries/stations.queries.js b/Database/Queries/stations.queries.js new file mode 100644 index 0000000..053c422 --- /dev/null +++ b/Database/Queries/stations.queries.js @@ -0,0 +1,19 @@ +const db = require("../database.js"); + +const findStationById = (id) => { + return db("stations") + .select() + .where("id", "=", id); +}; + +const findStationByName = (name) => { + return db("stations") + .select() + .where("station_name", "=", name); +}; + + +module.exports = { + findStationById: findStationById, + findStationByName: findStationByName +}; diff --git a/Database/Queries/trains.queries.js b/Database/Queries/trains.queries.js new file mode 100644 index 0000000..e39fceb --- /dev/null +++ b/Database/Queries/trains.queries.js @@ -0,0 +1,11 @@ +const db = require("../database.js"); + +const findTrain = (num) => { + return db("trains") + .select() + .where("num", "=", num); +}; + +module.exports = { + findTrain: findTrain +}; diff --git a/Database/database.js b/Database/database.js index b71f4f7..e6e0725 100644 --- a/Database/database.js +++ b/Database/database.js @@ -4,5 +4,3 @@ let env = process.env.NODE_ENV || 'development'; let knex = require('knex')(config[env]); module.exports = knex; - -knex.migrate.latest([config]); From 3c203ad6667df4e15e29baf494f75f5960ef50dd Mon Sep 17 00:00:00 2001 From: Zubair Ahmed Date: Wed, 3 May 2017 17:49:40 -0700 Subject: [PATCH 5/6] finish Station class and all associated instance and static methods --- Classes/station.js | 139 +++++++++++++++++++-------- Database/Queries/stations.queries.js | 14 ++- Database/Queries/trains.queries.js | 9 +- app.js | 22 +++-- package.json | 3 +- 5 files changed, 139 insertions(+), 48 deletions(-) diff --git a/Classes/station.js b/Classes/station.js index 08b41ff..6d08815 100644 --- a/Classes/station.js +++ b/Classes/station.js @@ -1,59 +1,122 @@ +const trainQueries = require('../Database/Queries/trains.queries'); +const passengerQueries = require('../Database/Queries/passengers.queries'); +const genericQueries = require('../Database/Queries/generic.queries'); +const stationQueries = require('../Database/Queries/stations.queries'); + module.exports = class Station { - constructor(locationName = "") { - this.locationName = locationName; - this.id = -1; - this.listOfPassengers = []; - this.nextStation = 0; - this.previousStation = 1; + constructor(stationName = "", id = undefined, order = undefined) { + this.id = id; + this.stationName = stationName; + this.order = order; } getId() { return this.id; } - getLocationName() { - return this.locationName; - } - - getWaitingPassengers() { - // returns the passengers waiting in a given station - return this.listOfPassengers; + getStationName() { + return this.stationName; } - getPassengersWithTickets() { - // returns passengers whose hasTicket property is true + getWaitingPassengers(callback) { + passengerQueries.findPassengersByStation(this.stationName) + .then(data => { + callback(data); + }) + .catch(error => { + console.log('An error occured', error); + }); } - getPreviousStation() { - // returns the previous station, which it finds from the station id - return Station.findStationById(this.previousStation); + getPassengersWithTickets(callback) { + passengerQueries.findPassengersByStation(this.stationName) + .then(data => { + let filteredData = data.filter(element => { + return element.has_ticket; + }); + callback(filteredData); + }) + .catch(error => { + console.log('An error occured', error); + }); } - getNextStation() { - // returns the previous station, which it finds from the station id - return Station.findStationById(this.nextStation); + getPreviousStation(callback) { + let preOrder = this.order - 1; + if (preOrder === 0) { + stationQueries.getCountOfStations() + .then(number => { + preOrder = number[0].count; + return preOrder; + }) + .then(order => { + return stationQueries.findStationByOrder(order); + }) + .then(station => { + callback(station); + }); + } + else { + stationQueries.findStationByOrder(preOrder) + .then(station => { + callback(station); + }) + } } - static getNextTrainOf(name) { - // returns the next train that's coming into 'name', which is the station name + getNextStation(callback) { + let nextOrder = this.order + 1; + stationQueries.getCountOfStations() + .then(count => { + if(nextOrder > count) { + nextOrder = 1; + } + stationQueries.findStationByOrder(nextOrder) + .then(station => { + callback(station); + }); + }) } - static findStationById(id) { - let station1 = { - id: 0, - locationName: "station1" - } - - let station2 = { - id: 1, - locationName: "station2" - } - - let arr = [station1, station2]; - return arr[id]; + static getNextTrainOf( name, callback ) { + stationQueries.findStationByName(name) + .then(station => { + let preOrder = station[0].order - 1; + if ( preOrder === 0 ) { + stationQueries.getCountOfStations() + .then(count => { + preOrder = count[0].count; + return preOrder; + }) + .then(order => { + return stationQueries.findStationByOrder(order); + }) + .then(station => { + return trainQueries.findTrainsAtStation(station[0].id); + }) + .then(trains => { + callback(trains); + }) + } + else { + stationQueries.findStationByOrder(preOrder) + .then( station => { + return trainQueries.findTrainsAtStation(station[0].id); + }) + .then( trains => { + callback(trains); + }) + } + }) } - static findStationByLocation(locationName) { - // query to retrieve station information from database + // this finds a station by ID and makes it persist + loadInstanceOfStationById(id = this.id) { + return stationQueries.findStationById(id) + .then(station => { + this.stationName = station[0].station_name; + this.order = station[0].order; + this.id = station[0].id; + }) } } diff --git a/Database/Queries/stations.queries.js b/Database/Queries/stations.queries.js index 053c422..141e8db 100644 --- a/Database/Queries/stations.queries.js +++ b/Database/Queries/stations.queries.js @@ -6,14 +6,26 @@ const findStationById = (id) => { .where("id", "=", id); }; +const findStationByOrder = (order) => { + return db("stations") + .select() + .where("order", "=", order); +}; + const findStationByName = (name) => { return db("stations") .select() .where("station_name", "=", name); }; +const getCountOfStations = () => { + return db("stations").count(); +} + module.exports = { findStationById: findStationById, - findStationByName: findStationByName + findStationByOrder: findStationByOrder, + findStationByName: findStationByName, + getCountOfStations: getCountOfStations }; diff --git a/Database/Queries/trains.queries.js b/Database/Queries/trains.queries.js index e39fceb..eee83d7 100644 --- a/Database/Queries/trains.queries.js +++ b/Database/Queries/trains.queries.js @@ -6,6 +6,13 @@ const findTrain = (num) => { .where("num", "=", num); }; +const findTrainsAtStation = (stationId) => { + return db("trains") + .select() + .where("current_station_id", "=", stationId); +}; + module.exports = { - findTrain: findTrain + findTrain: findTrain, + findTrainsAtStation: findTrainsAtStation }; diff --git a/app.js b/app.js index 85e1404..fa28b69 100644 --- a/app.js +++ b/app.js @@ -1,9 +1,19 @@ -const express = require('express'); -const app = express(); -const db = require('./Database/database'); +const Station = require('./Classes/station'); -const port = 3000; +const station1 = new Station('sdfksdfkjh', 1, 1); -app.listen(port, () => { - console.log('Server is running on port', port); +// station1.getWaitingPassengers(data => { +// console.log(data); +// }); + +// station1.getPassengersWithTickets(data => { +// console.log(data); +// }); + +// station1.getPreviousStation(console.log); +// station1.getNextStation(console.log); +// Station.getNextTrainOf(station1.stationName, console.log); + +station1.loadInstanceOfStationById().then(data => { + console.log('This is the station', station1); }); diff --git a/package.json b/package.json index 4300b9c..71db764 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Create a database API for a model train system", "main": "app.js", "scripts": { - "start": "./node_modules/.bin/nodemon app.js --ignore node_modules/", + "start": "node app.js", "test": "echo \"Error: no test specified\" && exit 1", "repl": "echo 'this entered here'", "linter": "node_modules/.bin/eslint .", @@ -31,7 +31,6 @@ "dependencies": { "express": "^4.15.2", "knex": "^0.13.0", - "nodemon": "^1.11.0", "pg": "^6.1.5" } } From 9d49b9d436427762a742501954fb81a576346226 Mon Sep 17 00:00:00 2001 From: Zubair Ahmed Date: Thu, 4 May 2017 15:13:28 -0700 Subject: [PATCH 6/6] finish Train class and associated static, method, and database functions --- Classes/train.js | 133 +++++++++++++++++++++++++ Database/Queries/generic.queries.js | 2 +- Database/Queries/passengers.queries.js | 32 +++++- Database/Queries/trains.queries.js | 13 ++- Database/Seeds/seed.js | 8 +- app.js | 21 ++-- migrations/20170502130947_metrorail.js | 2 +- 7 files changed, 182 insertions(+), 29 deletions(-) diff --git a/Classes/train.js b/Classes/train.js index e69de29..8f7dfb0 100644 --- a/Classes/train.js +++ b/Classes/train.js @@ -0,0 +1,133 @@ +const trainQueries = require('../Database/Queries/trains.queries'); +const passengerQueries = require('../Database/Queries/passengers.queries'); +const genericQueries = require('../Database/Queries/generic.queries'); +const stationQueries = require('../Database/Queries/stations.queries'); + +module.exports = class Train { + constructor(id = undefined, number = undefined, capacity = undefined) { + this.id = id; + this.number = number; + this.numberOfPassengers = undefined; + this.capacity = capacity; + this.currentStationId = undefined; + } + + getNumber() { + return this.number; + } + + getCapacity() { + return this.capacity; + } + + getPassengers() { + return passengerQueries.findPassengersByTrain(this.id) + .catch(error => { + console.log('An error occured:', error); + }) + } + + isFull() { + return this.capacity === this.numberOfPassengers; + } + + getCurrentStation() { + return stationQueries.findStationById(this.currentStationId) + .then(station => { + return station[0]; + }) + .catch(error => { + console.log('An error occured:', error); + }); + } + + getNextStation() { + return Promise.all([ + stationQueries.findStationById( this.currentStationId ), + stationQueries.getCountOfStations() + ]) + .then( ([ station, count ]) => { + // return (( station[ 0 ].order + 1 ) % ( station[ 0 ].order + 1 )) || 1; + if ((station[0].order + 1) > count[0].count) { + return 1; + } + else return station[0].order + 1; + }) + .then( stationQueries.findStationByOrder ) + .then( nextStation => nextStation[ 0 ] ) + .catch( error => console.log( 'An error occurred', error )) + } + + moveTrain() { + return this.getNextStation() + .then(station => { + this.currentStationId = station.id; + return station; + }) + } + + offBoard() { + return passengerQueries.findDesAndTrainId(this.currentStationId, this.id) + .then(passengers => { + let promises = []; + passengers.forEach(passenger => { + passenger.destination_id = null; + passenger.current_station_id = this.currentStationId; + passenger.current_train_id = null; + passenger.has_ticket = false; + promises.push(genericQueries.update("passengers", passenger)); + }) + return Promise.all(promises); + }) + .catch(error => { + console.log('An error occurred: ', error); + }) + } + + onBoard() { + return passengerQueries.findByStationIdAndTicket(this.currentStationId) + .then(passengers => { + let promises = []; + passengers.forEach(passenger => { + passenger.current_station_id = null; + passenger.current_train_id = this.id; + promises.push(genericQueries.update("passengers", passenger)); + }) + return Promise.all(promises); + }) + } + + loadInstanceOfTrainById(id = this.id) { + return trainQueries.findTrainById(id) + .then(train => { + this.id = train[0].id; + this.number = train[0].number; + this.numberOfPassengers = train[0].number_of_passengers; + this.capacity = train[0].capacity; + this.currentStationId = train[0].current_station_id; + return this; + }) + .catch(error => { + console.log(error); + }); + } + + loadInstanceOfTrainByNum(number = this.number) { + return trainQueries.findTrainByNumber(number) + .then(train => { + if (train.length > 1) { + console.log('There are more than one trains with this number. We are loading the first instance of the train.'); + } + this.id = train[0].id; + this.number = train[0].number; + this.numberOfPassengers = train[0].number_of_passengers; + this.capacity = train[0].capacity; + this.currentStationId = train[0].current_station_id; + return this; + }) + .catch(error => { + console.log(error); + }); + } + +} diff --git a/Database/Queries/generic.queries.js b/Database/Queries/generic.queries.js index 2b82d13..75d3f86 100644 --- a/Database/Queries/generic.queries.js +++ b/Database/Queries/generic.queries.js @@ -10,7 +10,7 @@ const update = (table, object) => { return db(table) .returning("id") .where("id", "=", object.id) - .update(object); + .update(object) } const del = (table, id) => { diff --git a/Database/Queries/passengers.queries.js b/Database/Queries/passengers.queries.js index 2aba614..ff14f92 100644 --- a/Database/Queries/passengers.queries.js +++ b/Database/Queries/passengers.queries.js @@ -2,7 +2,6 @@ const db = require("../database.js"); const findPassengersByName = (name) => { return db("passengers") - .select() .where("name", name); }; @@ -13,16 +12,39 @@ const findPassengersByStation = (stationName) => { "passengers.has_ticket", "passengers.current_train_id", "passengers.current_station_id") .where("stations.station_name", stationName); -} +}; +const findByStationIdAndTicket = (stationId) => { + return db("passengers") + .where({ + current_station_id: stationId, + has_ticket: true + }) +}; const findPassengersByTrain = (trainId) => { return db("passengers") - .select() .where("current_train_id", trainId); -} +}; + +const findPassengerById = (passengerId) => { + return db("passengers") + .where("id", passengerId) + .first(); +}; + +const findDesAndTrainId = (destinationId, trainId) => { + return db("passengers") + .where({ + destination_id: destinationId, + current_train_id: trainId + }) +}; module.exports = { findPassengersByName: findPassengersByName, findPassengersByStation: findPassengersByStation, - findPassengersByTrain: findPassengersByTrain + findPassengersByTrain: findPassengersByTrain, + findPassengerById: findPassengerById, + findDesAndTrainId: findDesAndTrainId, + findByStationIdAndTicket: findByStationIdAndTicket }; diff --git a/Database/Queries/trains.queries.js b/Database/Queries/trains.queries.js index eee83d7..7290817 100644 --- a/Database/Queries/trains.queries.js +++ b/Database/Queries/trains.queries.js @@ -1,9 +1,15 @@ const db = require("../database.js"); -const findTrain = (num) => { +const findTrainByNumber = (number) => { return db("trains") .select() - .where("num", "=", num); + .where("number", "=", number); +}; + +const findTrainById = (id) => { + return db("trains") + .select() + .where("id", "=", id); }; const findTrainsAtStation = (stationId) => { @@ -13,6 +19,7 @@ const findTrainsAtStation = (stationId) => { }; module.exports = { - findTrain: findTrain, + findTrainByNumber: findTrainByNumber, + findTrainById: findTrainById, findTrainsAtStation: findTrainsAtStation }; diff --git a/Database/Seeds/seed.js b/Database/Seeds/seed.js index bd1afa5..ddb13d7 100644 --- a/Database/Seeds/seed.js +++ b/Database/Seeds/seed.js @@ -2,22 +2,22 @@ const db = require('../database'); const trainData = [ { - num: 1, + number: 1, capacity: 55, number_of_passengers: 0 }, { - num: 2, + number: 2, capacity: 120, number_of_passengers: 0 }, { - num: 3, + number: 3, capacity: 23, number_of_passengers: 0 }, { - num: 4, + number: 4, capacity: 70, number_of_passengers: 0 } diff --git a/app.js b/app.js index fa28b69..6e26644 100644 --- a/app.js +++ b/app.js @@ -1,19 +1,10 @@ const Station = require('./Classes/station'); +const Train = require('./Classes/train') +const db = require('./Database/database'); -const station1 = new Station('sdfksdfkjh', 1, 1); +// User: add code here -// station1.getWaitingPassengers(data => { -// console.log(data); -// }); -// station1.getPassengersWithTickets(data => { -// console.log(data); -// }); - -// station1.getPreviousStation(console.log); -// station1.getNextStation(console.log); -// Station.getNextTrainOf(station1.stationName, console.log); - -station1.loadInstanceOfStationById().then(data => { - console.log('This is the station', station1); -}); +setTimeout( () => { + db.destroy(); +}, 2000); diff --git a/migrations/20170502130947_metrorail.js b/migrations/20170502130947_metrorail.js index a03a767..596635d 100644 --- a/migrations/20170502130947_metrorail.js +++ b/migrations/20170502130947_metrorail.js @@ -5,7 +5,7 @@ exports.up = function(knex, Promise) { knex.schema.createTable('trains', function(table) { table.increments('id').primary(); - table.integer('num'); + table.integer('number'); table.integer('capacity'); table.integer('number_of_passengers'); table.integer('current_station_id')