Update API search
This commit is contained in:
parent
eed19a225f
commit
f3448723cb
|
@ -19,12 +19,21 @@ app.get("/api", (req, res) => {
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get("/api/airports", (req, res) => {
|
app.get("/api/airports", (req, res) => {
|
||||||
Airport.findAll().then(airports => res.json(airports))
|
const search = req.query.search.toLowerCase()
|
||||||
|
let where = {}
|
||||||
|
if (search) {
|
||||||
|
let or_array = []
|
||||||
|
for (let col_name of ['name', 'icao_code', 'iata_code'])
|
||||||
|
or_array.push(db.sequelize.where(db.sequelize.fn('lower', db.sequelize.col(col_name)),
|
||||||
|
{[db.Sequelize.Op.like]: `%${search}%`}))
|
||||||
|
where = {[db.Sequelize.Op.or]: or_array}
|
||||||
|
}
|
||||||
|
Airport.findAll({where: where}).then(airports => res.json(airports))
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get("/api/airport/:id", (req, res) => {
|
app.get("/api/airport/:icao_code", (req, res) => {
|
||||||
const id = req.params.id
|
const icao_code = req.params.icao_code
|
||||||
Airport.findByPk(id).then(data => {
|
Airport.findOne({where: {icao_code: icao_code}}).then(data => {
|
||||||
if (data)
|
if (data)
|
||||||
res.json(data)
|
res.json(data)
|
||||||
else
|
else
|
||||||
|
@ -42,6 +51,17 @@ app.get("/api/flights/:dep_icao", (req, res) => {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
app.get("/api/flights/:dep_icao/:arr_icao", (req, res) => {
|
||||||
|
const dep_icao = req.params.dep_icao
|
||||||
|
const arr_icao = req.params.arr_icao
|
||||||
|
Flight.findAll({where: {dep_icao: dep_icao, arr_icao: arr_icao}}).then(data => {
|
||||||
|
if (data)
|
||||||
|
res.json(data)
|
||||||
|
else
|
||||||
|
res.status(500).json({message: `An error occurred while fetching flights.`})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
app.get('*', (req, res) => {
|
app.get('*', (req, res) => {
|
||||||
res.sendFile(path.resolve(__dirname, '../client/build', req.path))
|
res.sendFile(path.resolve(__dirname, '../client/build', req.path))
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue