Code States
Front-end boost camp
Today
I
Learned
🐶 38일차.. 커리큘럼이 복습 파트라 이해 안되는 서버 부분 처음부터 훑어보겠다... with. 생활코딩 선생님
상세 내용 : https://url.kr/23gvht
module.exports = {
// [GET] /airport?query={query} 요청을 수행합니다.
// 공항 이름 자동완성 기능을 수행합니다!
findAll: (req, res) => {
if (req.query.query !== undefined) {
console.log(req.query.query);
const filteredAirports = airports.filter((airport) => {
return airport.code.includes(req.query.query.toUpperCase());
});
return res.status(200).json(filteredAirports);
}
res.json(airports);
}
};
// POST /book에서 사용할 uuid입니다.
const { v4: uuid } = require('uuid');
// 항공편 예약 데이터를 저장합니다.
let booking = [];
module.exports = {
// [GET] /book 요청을 수행합니다.
// 전체 예약 데이터를 조회합니다.
findAll: (req, res) => {
return res.status(200).json(booking);
},
// [GET] /book/:phone 요청을 수행합니다.
// 요청 된 phone과 동일한 phone 예약 데이터를 조회합니다.
findByPhone: (req, res) => {
const {phone} = req.params;
if (req.params !== undefined) {
const filteredPhone = booking.filter((el) => {
return el.phone === phone;
})
return res.json(filteredPhone)
}
return res.json(booking)
},
// [GET] /book/:phone/:flight_uuid 요청을 수행합니다.
// 요청 된 id, phone과 동일한 uuid, phone 예약 데이터를 조회합니다.
findByPhoneAndFlightId: (req,res) => {
const {phone, flight_uuid} = req.params;
// TODO:
if (req.params !== undefined) {
let filtered = booking
if (phone !== undefined) {
filtered = filtered.filter((el) => {
return el.phone === phone;
})
}
if (flight_uuid !== undefined) {
filtered = filtered.filter((el) => {
return el.flight_uuid === flight_uuid;
})
}
return res.json(filtered);
}
return res.json(booking);
},
// [POST] /book 요청을 수행합니다.
// 요청 된 예약 데이터를 저장합니다.
create: (req, res) => {
// POST /book에서 사용할 booking_uuid입니다.
const booking_uuid = uuid();
// TODO:
const reservation = {
booking_uuid,
flight_uuid : req.body.flight_uuid,
name : req.body.name,
phone : req.body.phone
}
booking.push(reservation);
return res.json(reservation);
},
// Optional
// [DELETE] /book/:booking_uuid 요청을 수행합니다.
// 요청 된 id, phone 값과 동일한 예약 데이터를 삭제합니다.
deleteByBookingId: (req, res) => {
const {booking_uuid} = req.params;
// TODO:
}
};
const flights = require('../repository/flightList');
const fs = require('fs');
module.exports = {
// [GET] /flight
// 요청 된 파라미터 departure_times, arrival_times 값과 동일한 값을 가진 항공편 데이터를 조회합니다.
// 요청 된 파라미터 departure, destination 값과 동일한 값을 가진 항공편 데이터를 조회합니다.
findAll: (req, res) => {
const { departure_times, arrival_times, destination, departure } = req.query;
// TODO:
if (req.query !== undefined) {
let filtered = flights
if (departure !== undefined) {
filtered = filtered.filter((el) => {
return el.departure === departure;
})
}
if (destination !== undefined) {
filtered = filtered.filter((el) => {
return el.destination === destination;
})
}
if (departure_times !== undefined) {
filtered = filtered.filter((el) => {
return el.departure_times === departure_times;
})
}
if (arrival_times !== undefined) {
filtered = filtered.filter((el) => {
return el.arrival_times === arrival_times;
})
}
return res.json(filtered);
}
return res.json(flights);
},
// [GET] /flight/:uuid
// 요청 된 uuid 값과 동일한 uuid 값을 가진 항공편 데이터를 조회합니다.
findById: (req, res) => {
const { uuid } = req.params;
// TODO:
if (req.params !== undefined) {
let filteredUuid = flights.filter((el) => {
return el.uuid === uuid;
})
return res.json(filteredUuid);
}
return res.json(flights)
},
// Advanced
// [PUT] /flight/:uuid 요청을 수행합니다.
// 요청 된 uuid 값과 동일한 uuid 값을 가진 항공편 데이터를 요쳥 된 Body 데이터로 수정합니다.
update: (req, res) => {
const { uuid } = req.params;
const bodyData = req.body;
// TODO:
if (uuid !== undefined) {
let filteredUuid = flights.filter((el) => {
return el.uuid === uuid;
})[0]
let index = flights.indexOf(filteredUuid)
let newFlight = {
...filteredUuid,
...bodyData
}
flights.splice(index, 1, newFlight)
return res.json(newFlight)
}
}
};