Import airports and flights data
This commit is contained in:
parent
a35e1bf093
commit
4d61358e03
@ -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 (
|
||||
<div className="App">
|
||||
Hello world!
|
||||
{content}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
@ -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",
|
||||
|
45
server/cli/import.js
Normal file
45
server/cli/import.js
Normal file
@ -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()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
@ -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) => {
|
||||
|
Loading…
Reference in New Issue
Block a user