const { filter } = require('../repository/flightList');
const flights = require('../repository/flightList');
module.exports = {
// [GET] /flight
// 요청 된 departure_times, arrival_times, destination, departure 값과 동일한 값을 가진 항공편 데이터를 조회합니다.
findAll: async (req, res) => {
//TODO:
let filterFlights = flights;
if (req.query.departure_times) {
filterFlights = await filterFlights.filter((flight) => flight.departure_times === req.query.departure_times)
}
if (req.query.arrival_times) {
filterFlights = await filterFlights.filter((flight) => flight.arrival_times === req.query.arrival_times)
}
if (req.query.destination) {
filterFlights = await filterFlights.filter((flight) => flight.destination === req.query.destination)
}
if (req.query.departure) {
filterFlights = await filterFlights.filter((flight) => flight.departure === req.query.departure)
}
return res.json(filterFlights);
},
// [GET] /flight/{:id}
// 요청 된 id 값과 동일한 uuid 값을 가진 항공편 데이터를 조회합니다.
findById: async (req, res) => {
//TODO:
let filterFlights = flights;
if (req.params.id) {
filterFlights = await filterFlights.filter((el) => el.uuid === req.params.id);
}
return res.json(filterFlights);
},
// [PUT] /flight/{:id} 요청을 수행합니다.
// 요청 된 id 값과 동일한 uuid 값을 가진 항공편 데이터를 요청 된 Body 데이터로 수정합니다.
update: async (req, res) => {
//TODO:
let data;
flights.map((el) => {
if (el.uuid === req.params.id) {
if (req.body.departure) {
el.departure = req.body.departure
}
if (req.body.destination) {
el.destination = req.body.destination
}
if (req.body.departure_times) {
el.departure_times = req.body.departure_times
}
if (req.body.arrival_times) {
el.arrival_times = req.body.arrival_times
}
data = el;
}
return el;
})
return res.status(200).json(data);
}
};
JavaScript
복사