diff --git a/client/src/App.js b/client/src/App.js index 14c25b2..e2deca6 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -1,9 +1,13 @@ +import { useState } from 'react'; + import './App.css'; function App() { + let [content, setContent] = useState("Loading…") + fetch('/api/').then(res => res.json()).then(data => setContent(data['message'])) return (
- Hello world! + {content}
); } diff --git a/package.json b/package.json index 01c4c6c..f31d843 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "scripts": { "build": "cd client && npm run build", "dev": "nodemon --exec npm start", + "import": "node server/cli/import.js", "migrate": "npm run migrate:up", "migrate:up": "cd server && npx sequelize-cli db:migrate", "migrate:undo": "cd server && npx sequelize-cli db:migrate:undo", diff --git a/server/cli/import.js b/server/cli/import.js new file mode 100644 index 0000000..d3119f9 --- /dev/null +++ b/server/cli/import.js @@ -0,0 +1,45 @@ +const fs = require('fs') +const db = require("../models") + +const Airport = db['Airport'] +const Flight = db['Flight'] + +db.sequelize.sync() + +fs.readFile(__dirname + '/../data/airports.json', 'utf-8', (error, data) => { + if (error) { + console.log(error) + return + } + + data = JSON.parse(data) + Airport.findAll({attributes: ['icao_code', 'iata_code']}).then(results => { + let icao_codes = [] + let iata_codes = [] + results.forEach(result => { + if (result.icao_code) + icao_codes.push(result.icao_code) + if (result.iata_code) + iata_codes.push(result.iata_code) + }) + + Airport.bulkCreate(data['response'].filter(airport => !icao_codes.includes(airport.icao_code) + && !iata_codes.includes(airport.iata_code))).then() + }).then(() => { + Airport.findAll({attributes: ['icao_code']}).then(airports => { + airports.forEach(airport => { + fs.readFile(__dirname + `/../data/routes/${airport.icao_code}.json`, 'utf-8', (error, data) => { + if (error) { + console.log(error) + return + } + + data = JSON.parse(data) + Flight.destroy({where: {dep_icao: airport.icao_code}}).then(() => { + Flight.bulkCreate(data['response']).then() + }) + }) + }) + }) + }) +}) diff --git a/server/index.js b/server/index.js index 770249a..afe6b67 100644 --- a/server/index.js +++ b/server/index.js @@ -19,7 +19,7 @@ app.get("/api", (req, res) => { }) app.get("/api/airports", (req, res) => { - res.json(Airport.findAll()) + Airport.findAll().then(airports => res.json(airports)) }) app.get("/api/airport/:id", (req, res) => {