Import airports and flights data

This commit is contained in:
Emmy D'Anello 2023-08-11 18:07:34 +02:00
parent a35e1bf093
commit 4d61358e03
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
4 changed files with 52 additions and 2 deletions

View File

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

View File

@ -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
View 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()
})
})
})
})
})
})

View File

@ -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) => {