Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
Empty file added Classes/passenger.js
Empty file.
122 changes: 122 additions & 0 deletions Classes/station.js
Original file line number Diff line number Diff line change
@@ -0,0 +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(stationName = "", id = undefined, order = undefined) {
this.id = id;
this.stationName = stationName;
this.order = order;
}

getId() {
return this.id;
}

getStationName() {
return this.stationName;
}

getWaitingPassengers(callback) {
passengerQueries.findPassengersByStation(this.stationName)
.then(data => {
callback(data);
})
.catch(error => {
console.log('An error occured', error);
});
}

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);
});
}

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);
})
}
}

getNextStation(callback) {
let nextOrder = this.order + 1;
stationQueries.getCountOfStations()
.then(count => {
if(nextOrder > count) {
nextOrder = 1;
}
stationQueries.findStationByOrder(nextOrder)
.then(station => {
callback(station);
});
})
}

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);
})
}
})
}

// 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;
})
}
}
133 changes: 133 additions & 0 deletions Classes/train.js
Original file line number Diff line number Diff line change
@@ -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);
});
}

}
26 changes: 26 additions & 0 deletions Database/Queries/generic.queries.js
Original file line number Diff line number Diff line change
@@ -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
};
50 changes: 50 additions & 0 deletions Database/Queries/passengers.queries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const db = require("../database.js");

const findPassengersByName = (name) => {
return db("passengers")
.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 findByStationIdAndTicket = (stationId) => {
return db("passengers")
.where({
current_station_id: stationId,
has_ticket: true
})
};
const findPassengersByTrain = (trainId) => {
return db("passengers")
.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,
findPassengerById: findPassengerById,
findDesAndTrainId: findDesAndTrainId,
findByStationIdAndTicket: findByStationIdAndTicket
};
31 changes: 31 additions & 0 deletions Database/Queries/stations.queries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const db = require("../database.js");

const findStationById = (id) => {
return db("stations")
.select()
.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,
findStationByOrder: findStationByOrder,
findStationByName: findStationByName,
getCountOfStations: getCountOfStations
};
Loading