์ค๋์ StatesAirline ์๋ฒ๋ฅผ Express ํ๋ ์์ํฌ๋ฅผ ์ด์ฉํด ๋ง๋ค๊ณ , ๋ก์ปฌ ํธ์คํธ์ ์ฐ๊ฒฐํด์ ๊ตฌํํ๋ค.
์๋ฒ๋ ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ๋ฐ๋ผ ํญ๊ณตํธ๊ณผ ์์ฝ ๋ฐ์ดํฐ๋ฅผ ์กฐํ, ์์ฑ, ์์ , ๊ทธ๋ฆฌ๊ณ ์ญ์ ํ๋ ๊ธฐ๋ฅ์ ์ํํด์ผ ํ๋ค.
๐ก ์ค๋์ KeyPoint
- ๊ฐ ์๋ํฌ์ธํธ๋ง๋ค ๋ผ์ฐํฐ๊ฐ ์กด์ฌํ๋ค.
- ํด๋น ๋ผ์ฐํฐ์ ๋ง๋ ์๊ตฌ์กฐ๊ฑด ๊ธฐ๋ฅ์ ๊ตฌํํ๋ค.
- 5๊ฐ์ง HTTP method๋ฅผ ๊ตฌํํ๋ค.
Advanced ์ฝํ
์ธ ์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ค๋ฉด, ์ถฉ๋ถํ ์ปจํ
์คํธ๊ฐ ํ์ํ ์ ์์ต๋๋ค.
Bare minimum requirements๋ฅผ ์๋ฃํ๋ค๋ฉด ๋์ ํ์ธ์.
์์ฑํ ๋ฐ์ดํฐ๋ฅผ ์์ ํ ์ ์๋ค๋ฉด, ๋งค๋ฒ ๋ฐ์ดํฐ๋ฅผ ์ง์ฐ๊ณ ๋ค์ ์์ฑํด์ผ ํฉ๋๋ค.
์ด๋ฐ ๋ถํธ์ ์๋น์ค์์ ์ ๊ฑฐํ๋ ค๋ฉด, ๋ฐ์ดํฐ๋ฅผ ์์ ํ ์ ์๋ ๊ธฐ๋ฅ์ ์ถ๊ฐํ๋ฉด ๋ฉ๋๋ค.
Advanced ์ฝํ
์ธ ์์๋ PUT ์์ฒญ์ ๋ฐ๋ผ ํญ๊ณตํธ์ ์์ ํ๋ ๊ธฐ๋ฅ์ ๊ตฌํํฉ๋๋ค.
๋ชฉํ
์ฐธ๊ณ ๋ด์ฉ
โ๏ธ Code
// ๐ฌ ์ฃผ์ด์ง ์ฝ๋ (flightController)
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:
return res.json(flights);
},
// [GET] /flight/:uuid
// ์์ฒญ ๋ uuid ๊ฐ๊ณผ ๋์ผํ uuid ๊ฐ์ ๊ฐ์ง ํญ๊ณตํธ ๋ฐ์ดํฐ๋ฅผ ์กฐํํฉ๋๋ค.
findById: (req, res) => {
const { uuid } = req.params;
// TODO:
},
// Advanced
// [PUT] /flight/:uuid ์์ฒญ์ ์ํํฉ๋๋ค.
// ์์ฒญ ๋ uuid ๊ฐ๊ณผ ๋์ผํ uuid ๊ฐ์ ๊ฐ์ง ํญ๊ณตํธ ๋ฐ์ดํฐ๋ฅผ ์์ณฅ ๋ Body ๋ฐ์ดํฐ๋ก ์์ ํฉ๋๋ค.
update: (req, res) => {
const { uuid } = req.params;
const bodyData = req.body;
// TODO:
},
};
// ๐ฌ ์ฃผ์ด์ง ์ฝ๋ (bookController)
// 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;
},
// [GET] /book/:phone/:flight_uuid ์์ฒญ์ ์ํํฉ๋๋ค.
// ์์ฒญ ๋ id, phone๊ณผ ๋์ผํ uuid, phone ์์ฝ ๋ฐ์ดํฐ๋ฅผ ์กฐํํฉ๋๋ค.
findByPhoneAndFlightId: (req, res) => {
const { phone, flight_uuid } = req.params;
// TODO:
},
// [POST] /book ์์ฒญ์ ์ํํฉ๋๋ค.
// ์์ฒญ ๋ ์์ฝ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํฉ๋๋ค.
create: (req, res) => {
// POST /book์์ ์ฌ์ฉํ booking_uuid์
๋๋ค.
const booking_uuid = uuid();
// TODO:
},
// Optional
// [DELETE] /book/:booking_uuid ์์ฒญ์ ์ํํฉ๋๋ค.
// ์์ฒญ ๋ id, phone ๊ฐ๊ณผ ๋์ผํ ์์ฝ ๋ฐ์ดํฐ๋ฅผ ์ญ์ ํฉ๋๋ค.
deleteByBookingId: (req, res) => {
const { booking_uuid } = req.params;
// TODO:
},
};
// โ
๊ตฌํํ ์ฝ๋ (flightController)
const flights = require("../repository/flightList");
const fs = require("fs");
module.exports = {
findAll: (req, res) => {
const { departure_times, arrival_times, destination, departure } =
req.query;
// router.get("/", findAll); // Get localhost:3001/flight?departure=ICN&destination=CJU
// ๋์ฐฉ์๊ฐ, ์ถ๋ฐ์๊ฐ์ ๋ํ ํํฐ๋ง
if (departure_times && arrival_times) {
const filteredData = flights.filter(
(flight) =>
flight.departure_times === departure_times &&
flight.arrival_times === arrival_times
);
return res.status(200).json(filteredData);
}
// ์ถ๋ฐ์ง, ๋์ฐฉ์ง์ ๋ํ ํํฐ๋ง
else if (departure && destination) {
const filteredData = flights.filter(
(flight) =>
flight.departure === departure && flight.destination === destination
);
return res.status(200).json(filteredData);
}
// router.get("/", findAll); // GET localhost:3001/flight/
// ๊ทธ ์ธ๋ ์ ์ฒด ํญ๊ณตํธ ๋ฐ์ดํฐ๋ฅผ ์๋ต์ผ๋ก ์ ์ก
return res.status(200).json(flights);
},
// router.get("/:uuid", update); // GET localhost:3001/flight/af6fa55c-da65-47dd-af23-578fdba40bed
findById: (req, res) => {
const { uuid } = req.params;
const filteredData = flights.filter((flight) => flight.uuid === uuid);
res.status(200).json(filteredData);
},
// router.put("/:uuid", update); // PUT localhost:3001/flight/af6fa55c-da65-47dd-af23-578fdba40bed
update: (req, res) => {
const { uuid } = req.params;
const bodyData = req.body;
const flightIndex = flights.findIndex((flight) => flight.uuid === uuid);
if (flightIndex === -1) {
return res.status(404).json({ error: "Flight not found" });
}
// ๊ธฐ์กด ํญ๊ณตํธ ๋ฐ์ดํฐ๋ฅผ ์๋ก์ด ๋ฐ์ดํฐ๋ก ์
๋ฐ์ดํธํฉ๋๋ค.
flights[flightIndex] = { ...flights[flightIndex], ...bodyData };
return res.status(200).json(flights[flightIndex]);
},
};
// โ
๊ตฌํํ ์ฝ๋ (bookController)
const { v4: uuid } = require("uuid");
let booking = [];
module.exports = {
// router.get("/", findAll); // GET localhost:3001/book/
findAll: (req, res) => {
return res.status(200).json(booking);
},
// router.get("/:phone", findByPhone); // GET localhost:3001/book/:phone
findByPhone: (req, res) => {
const { phone } = req.params;
const filteredData = booking.filter((booking) => booking.phone === phone);
return res.status(200).json(filteredData);
},
// router.get("/:phone/:flight_uuid", findByPhoneAndFlightId); // GET localhost:3001/book/:phone/:flight_uuid
findByPhoneAndFlightId: (req, res) => {
const { phone, flight_uuid } = req.params;
const filteredData = booking.filter(
(booking) =>
booking.phone === phone && booking.flight_uuid === flight_uuid
);
return res.status(200).json(filteredData);
},
// router.post("/", create); // POST localhost:3001/book/
create: (req, res) => {
const booking_uuid = uuid();
const { phone, flight_uuid, name } = req.body; // ์์ฒญ ๋ฐ์ดํฐ์์ ํ์ํ ๊ฐ์ ์ถ์ถํ๋ค.
const newBooking = {
booking_uuid,
phone,
flight_uuid,
name,
};
booking.push(newBooking); // booking ๋ฐฐ์ด์ ์๋ก์ด ์์ฝ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๋ค.
return res.status(201).json(newBooking);
},
// router.delete("/:booking_uuid", deleteByBookingId); // GET localhost:3001/book/{:booking_uuid}
deleteByBookingId: (req, res) => {
const { booking_uuid } = req.params;
const initialLength = booking.length;
booking = booking.filter(
(booking) => booking.booking_uuid !== booking_uuid
);
if (booking.length === initialLength) {
return res.status(404).json({ error: "Booking not found" });
}
return res.sendStatus(204);
},
};
๊ตฌํํ ์๋ฒ
๐ก ์๋ฒ์ ๋ํด ์๊ฐํด๋ณผ ๊ฒ