Jieunny์˜ ๋ธ”๋กœ๊ทธ

S2) Unit 10. [์‹ค์Šต] StatesAirline Server ๋ณธ๋ฌธ

CodeStates/Training

S2) Unit 10. [์‹ค์Šต] StatesAirline Server

Jieunny 2023. 2. 8. 16:00

๐Ÿ“ฃ  Express ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ StatesAirline Server ๊ตฌ์ถ•ํ•˜๊ธฐ

๐Ÿ“ req.query

โžฐ ๊ฒฝ๋กœ์˜ ๊ฐ ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด ๋งค๊ฐœ ๋ณ€์ˆ˜์— ๋Œ€ํ•œ ์†์„ฑ์ด ํฌํ•จ ๋œ ๊ฐ์ฒด(GET ๋ฐฉ์‹์œผ๋กœ ๋„˜์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ์˜ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์ด ๋‹ด๊ธด๋‹ค)

โžฐ www.example.com/post/1/jun?title=hello! ์ด๋ฉด, title=hello! ๋ฅผ ๊ฐ์ฒด๋กœ ๊ฐ€์ ธ์˜จ๋‹ค.

 

๐Ÿ“ req.params

โžฐ ๋ผ์šฐํ„ฐ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜

โžฐ /:id/:name ๊ฒฝ๋กœ๊ฐ€ ์žˆ์œผ๋ฉด ':id' ์†์„ฑ๊ณผ ':name' ์†์„ฑ์„ req.params.id, req.params.name์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ“ req.body

โžฐ json ๋“ฑ์˜ ๋ฐ”๋”” ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค(POST ๋ฐฉ์‹์œผ๋กœ ๋„˜์–ด์˜ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๋Š”๋‹ค)

โžฐ ์š”์ฒญ ๋ณธ๋ฌธ์— ์ œ์ถœ ๋œ ํ‚ค,๊ฐ’ ๋ฐ์ดํ„ฐ ์Œ์„ ํฌํ•จํ•œ๋‹ค.

โžฐ req.body๋Š” body-parser๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์—๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ undefined๋กœ ์„ค์ •๋˜๊ธฐ ๋•Œ๋ฌธ์— body-parser๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.

โžฐ express.jon(), express.urlencoded() ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

 

 

1๏ธโƒฃ flightController.js

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;
    // ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ์ •๋ณด๋ฅผ req.query๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋‹ด์•„์˜จ๋‹ค.
    // TODO:
    if(departure_times && arrival_times){
    // ๋งŒ์•ฝ ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญํ•œ ์ •๋ณด๊ฐ€ ์žˆ๋‹ค๋ฉด
      const filteredFlights = flights.filter((flight) => {
      // filter ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญํ•œ ์ •๋ณด๋ž‘ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋งŒ ์กฐํšŒํ•œ๋‹ค.
        return flight.departure_times === departure_times && flight.arrival_times === arrival_times;
      });
      return res.status(200).json(filteredFlights);
      // ์š”์ฒญ์ด ์„ฑ๊ณตํ–ˆ์Œ๊ณผ ๋™์‹œ์— ๋‚ด๊ฐ€ ํ•„ํ„ฐ๋งํ•œ ๋ฐ์ดํ„ฐ๋ฅผ json ํ˜•ํƒœ๋กœ ๋ณด๋‚ธ๋‹ค.
    }
    if(destination && departure){
    // ์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์€ ๋ฐฉ์‹
      const filteredFlights = flights.filter((flight) => {
        return flight.destination === destination && flight.departure === departure;
      });
      return res.status(200).json(filteredFlights);
    }

    return res.status(200).json(flights);
  },
  // [GET] /flight/:uuid
  // ์š”์ฒญ ๋œ uuid ๊ฐ’๊ณผ ๋™์ผํ•œ uuid ๊ฐ’์„ ๊ฐ€์ง„ ํ•ญ๊ณตํŽธ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
  findById: (req, res) => {
    const { uuid } = req.params;
    // req.params์„ ์‚ฌ์šฉํ•ด์„œ uuid ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.
    // TODO:
    if(uuid){
    // ์š”์ฒญ ๋œ uuid ๊ฐ’๊ณผ ๊ฐ™์€ uuid ๊ฐ’์„ ๊ฐ€์ง„ ํ•ญ๊ณตํŽธ์„ ์กฐํšŒํ•œ๋‹ค.
      const filteredFlights = flights.filter((flight) => {
        return flight.uuid === uuid;
      });
      return res.status(200).json(filteredFlights);
    }
  },

  // Advanced
  // [PUT] /flight/:uuid ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  // ์š”์ฒญ ๋œ uuid ๊ฐ’๊ณผ ๋™์ผํ•œ uuid ๊ฐ’์„ ๊ฐ€์ง„ ํ•ญ๊ณตํŽธ ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ณฅ ๋œ Body ๋ฐ์ดํ„ฐ๋กœ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.
  update: (req, res) => {
    const { uuid } = req.params;
    const bodyData = req.body;
    if(uuid){
    // ์š”์ฒญ ๋œ uuid ๊ฐ’๊ณผ ๊ฐ™์€ uuid ๊ฐ’์„ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•ด์„œ ๋‹ด๋Š”๋‹ค.
      const filtered = flights.filter((flight) => {
        return flight.uuid === uuid;
      });
      const updateData = Object.assign(filtered[0], bodyData);
      // ํ•„ํ„ฐ๋ง๋œ ๋ฐ์ดํ„ฐ๋Š” ๋ฐฐ์—ด๋กœ ๋‹ด๊ฒจ ์žˆ์œผ๋ฏ€๋กœ ๊ทธ ๋ฐ์ดํ„ฐ์˜ 0๋ฒˆ์งธ๋ฅผ ์š”์ฒญ ๋œ body ๋ฐ์ดํ„ฐ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.
      return res.status(200).json(updateData);
      // ๋ฐ”๊พผ ๋ฐ์ดํ„ฐ๋ฅผ ์‘๋‹ต์œผ๋กœ ๋Œ๋ ค์ค€๋‹ค.
    }
  }
};

 

2๏ธโƒฃ bookController.js

// 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;
    // ์š”์ฒญ ๋œ phone ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
    if(phone){
    // ์š”์ฒญ ๋œ phone ๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ํ•„ํ„ฐ๋ง
      const filteredList = booking.filter((item) => {
        return item.phone === phone;
      })
      return res.status(200).json(filteredList);
    } 
  },
  // [GET] /book/:phone/:flight_uuid ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  // ์š”์ฒญ ๋œ id, phone๊ณผ ๋™์ผํ•œ uuid, phone ์˜ˆ์•ฝ ๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.
  findByPhoneAndFlightId: (req,res) => {
    const {phone, flight_uuid} = req.params;
    // ์œ„์™€ ๊ฐ™์€ ๋ฐฉ์‹
    // TODO:
    if(phone && flight_uuid){
      const filteredList = booking.filter((item) => {
        return item.phone === phone && item.flight_uuid === flight_uuid;
      })
      return res.status(200).json(filteredList);
    } 
  },
  // [POST] /book ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  // ์š”์ฒญ ๋œ ์˜ˆ์•ฝ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  create: (req, res) => {
    // POST /book์—์„œ ์‚ฌ์šฉํ•  booking_uuid์ž…๋‹ˆ๋‹ค.
    const booking_uuid = uuid();
    // uuid๋ฅผ ์ƒ์„ฑํ•˜๊ณ 
    // TODO:
    req.body.booking_uuid = booking_uuid;  
    // ์ €์žฅํ•  ๋ฐ์ดํ„ฐ์˜ uuid๋ฅผ ์ƒ์„ฑํ•œ uuid๋กœ ์ง€์ •ํ•ด์ค€๋‹ค.
    booking.push(req.body);
    // booking ๋ฐ์ดํ„ฐ ๋ฐฐ์—ด์— req.body๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์–ด์ค€๋‹ค. 
    return res.status(200).json(booking);
    // ์ตœ์ข…์ ์œผ๋กœ ๋ฐ์ดํ„ฐ๋“ค์ด ๋‹ด๊ธด booking ๋ฐ์ดํ„ฐ๋ฅผ ์‘๋‹ต์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  },

  // Optional
  // [DELETE] /book/:booking_uuid ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  // ์š”์ฒญ ๋œ id, phone ๊ฐ’๊ณผ ๋™์ผํ•œ ์˜ˆ์•ฝ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
  deleteByBookingId: (req, res) => {
    const {booking_uuid} = req.params;
    // ์š”์ฒญ ๋œ uuid๋ฅผ ๋ฐ›์•„์˜ค๊ณ 
    // TODO:
    if(booking_uuid){
      const filteredList = booking.filter((item) => {
      // ์‚ญ์ œํ•˜๋Š”๊ฑฐ๋‹ˆ๊นŒ ๊ทธ uuid์™€ ๊ฐ™์ง€ ์•Š์€ ๋ฐ์ดํ„ฐ๋“ค๋งŒ ํ•„ํ„ฐ๋ง ํ•ด์ค€๋‹ค.
        return item.booking_uuid !== booking_uuid;
      });
      return res.status(200).json(filteredList);
      // ๊ฒฐ๊ณผ์ ์œผ๋กœ uuid์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๋Š” ํ•„ํ„ฐ๋ง๋˜์„œ ์‚ญ์ œ๋œ ๊ฒƒ ์ฒ˜๋Ÿผ ๊ตฌํ˜„๋œ๋‹ค.
    }
  }
};