first commit

This commit is contained in:
rui hildt 2020-02-07 17:55:43 +01:00
commit 06b2078e4d
18 changed files with 2818 additions and 0 deletions

59
.gitignore vendored Normal file
View File

@ -0,0 +1,59 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# VS Code settings
.vscode

17
Instructions.txt Normal file
View File

@ -0,0 +1,17 @@
# start
By creating a new web app that you will call Dijkstra, I would like you to:
- Find a DB modelization that allows you to store any country given its cities and its roads between them (see Belgian graph);
- Store the Belgian data (see belgium.rb);
- Allow a user to select Belgium as a country (or if he/she navigates to /countries/belgium), then allow to select a starting point (a Belgian city) and a destination (also a Belgian city);
- Once the user has selected the starting point and the destination, tell the shortest distance between those two cities;
- Describe to the user the path (for example: Bruges -> Ghent -> Brussels -> Liège) that corresponds to the shortest distance you returned to the user;
- Make your code shines!
# end

17
api/server.js Normal file
View File

@ -0,0 +1,17 @@
const express = require("express");
// const authRouter = require('../services/auth/authRouter');
// const usersRouter = require('../services/users/usersRouter');
// const sessionsRouter = require('../services/sessions/sessionsRouter');
// const dailyAveragesRouter = require('../services/dailyAverages/dailyAveragesRouter');
const server = express();
server.use(express.json());
// server.use('/api/auth', authRouter);
// server.use('/api/users', authenticate, usersRouter);
// server.use('/api/users', authenticate, sessionsRouter);
// server.use('/api/users', authenticate, dailyAveragesRouter);
module.exports = server;

BIN
data/database_file.db3 Normal file

Binary file not shown.

7
data/dbConfig.js Normal file
View File

@ -0,0 +1,7 @@
const knex = require('knex');
const config = require('../knexfile');
const environment = 'development';
module.exports = knex(config[environment]);

View File

@ -0,0 +1,13 @@
exports.up = function(knex, Promise) {
return knex.schema.createTable('countries', function(countries) {
countries.increments();
countries
.string('name', 128)
.notNullable()
.unique();
});
};
exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists('countries');
};

View File

@ -0,0 +1,20 @@
exports.up = function(knex) {
return knex.schema.createTable('cities', function(cities) {
cities.increments();
cities.string('name', 128).notNullable();
cities
.integer('country_id')
.unsigned()
.notNullable()
.references('id')
.inTable('countries')
.onDelete('CASCADE')
.onUpdate('CASCADE');
});
};
exports.down = function(knex) {
return knex.schema.dropTableIfExists('cities');
};

View File

@ -0,0 +1,32 @@
exports.up = function(knex) {
return knex.schema.createTable('roads', function(roads) {
roads.increments();
roads
.integer('start_city_id')
.unsigned()
.notNullable()
.references('id')
.inTable('cities')
.onDelete('CASCADE')
.onUpdate('CASCADE');
roads
.integer('end_city_id')
.unsigned()
.notNullable()
.references('id')
.inTable('cities')
.onDelete('CASCADE')
.onUpdate('CASCADE');
roads
.integer('distance')
.unsigned()
.notNullable()
});
};
exports.down = function(knex) {
return knex.schema.dropTableIfExists('roads');
};

View File

@ -0,0 +1,12 @@
exports.seed = function(knex) {
return knex('countries').truncate()
.then(function () {
return knex('countries').insert([
{
id: 1,
name: 'Belgium',
}
]);
});
};

59
data/seeds/02-cities.js Normal file
View File

@ -0,0 +1,59 @@
exports.seed = function(knex) {
return knex('cities').truncate()
.then(function () {
return knex('cities').insert([
{
"id": 1,
"name": 'bruges',
"country_id": 1
},
{
"id": 2,
"name": 'antwerp',
"country_id": 1
},
{
"id": 3,
"name": 'ghent',
"country_id": 1
},
{
"id": 4,
"name": 'mechelen',
"country_id": 1
},
{
"id": 5,
"name": 'brussels',
"country_id": 1
},
{
"id": 6,
"name": 'mons',
"country_id": 1
},
{
"id": 7,
"name": 'namur',
"country_id": 1
},
{
"id": 8,
"name": 'liege',
"country_id": 1
},
{
"id": 9,
"name": 'arlon',
"country_id": 1
},
{
"id": 10,
"name": 'tournai',
"country_id": 1
},
]);
});
};

92
data/seeds/03-roads.js Normal file
View File

@ -0,0 +1,92 @@
exports.seed = function(knex) {
return knex('roads').truncate()
.then(function () {
return knex('roads').insert([
{
"id": 1,
"start_city_id": 1,
"end_city_id": 3,
"distance": 50
},
{
"id": 2,
"start_city_id": 3,
"end_city_id": 10,
"distance": 80
},
{
"id": 3,
"start_city_id": 10,
"end_city_id": 5,
"distance": 89
},
{
"id": 4,
"start_city_id": 3,
"end_city_id": 5,
"distance": 56
},
{
"id": 5,
"start_city_id": 3,
"end_city_id": 2,
"distance": 60
},
{
"id": 6,
"start_city_id": 2,
"end_city_id": 4,
"distance": 25
},
{
"id": 7,
"start_city_id": 4,
"end_city_id": 5,
"distance": 27
},
{
"id": 8,
"start_city_id": 5,
"end_city_id": 6,
"distance": 80
},
{
"id": 9,
"start_city_id": 6,
"end_city_id": 7,
"distance": 91
},
{
"id": 10,
"start_city_id": 6,
"end_city_id": 10,
"distance": 51
},
{
"id": 11,
"start_city_id": 7,
"end_city_id": 9,
"distance": 129
},
{
"id": 12,
"start_city_id": 9,
"end_city_id": 7,
"distance": 123
},
{
"id": 13,
"start_city_id": 8,
"end_city_id": 7,
"distance": 65
},
{
"id": 14,
"start_city_id": 8,
"end_city_id": 5,
"distance": 97
}
]);
});
};

14
index.js Normal file
View File

@ -0,0 +1,14 @@
const server = require("./api/server");
server.get("/", (req, res) => {
res.json({
message: `API server for Dijkstra. :)`,
documentation: `Coming`
});
});
const PORT = process.env.PORT || 4000;
server.listen(PORT, () => {
console.log(`Listening on port ${PORT}...`);
});

23
knexfile.js Normal file
View File

@ -0,0 +1,23 @@
module.exports = {
development: {
client: 'sqlite3',
connection: {
filename: './data/database_file.db3',
},
useNullAsDefault: true,
migrations: {
directory: './data/migrations'
},
seeds: {
directory: './data/seeds'
},
pool: {
afterCreate: (conn, done) => {
// runs after a connection is made to the sqlite engine
conn.run('PRAGMA foreign_keys = ON', done); // turn on FK enforcement
},
},
}
};

0
models/citiesModel.js Normal file
View File

16
models/countriesModel.js Normal file
View File

@ -0,0 +1,16 @@
const db = require('../data/dbConfig');
module.exports = {
getCountries,
getCountryByName,
};
function getCountries() {
return db('countries')
}
function getCountryByName(name) {
return db('countries')
.where({ name })
.first()
}

0
models/roadsModel.js Normal file
View File

2418
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

19
package.json Normal file
View File

@ -0,0 +1,19 @@
{
"name": "dijkstra",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"express": "^4.17.1",
"knex": "^0.20.8",
"nodejs": "^0.0.0",
"sqlite3": "^4.1.1"
},
"devDependencies": {},
"scripts": {
"test": "test",
"start": "node index.js"
},
"author": "",
"license": "ISC"
}