Express.js) uuid 및 postman을 활용한 post test

김명성·2022년 6월 22일
0


const { v4 } = require('uuid');
const {v4: uuidv4} = require('uuid')

const HttpError = require('../models/http-error')

const DUMMY_PLACES = [
  {
    id:'p-22743454',
    title: '전주 한옥 마을',
    description:'전통 한옥, 서예 박물관, 전통술 박물관이 있는 문화 마을입니다.',
    imageURL:'https://lh5.googleusercontent.com/p/AF1QipMH_S511fGMCBX4Wjx5mgNamkO_oycZScZbp4Oe=w426-h240-k-no',
    address:'전라북도 전주시 완산구 풍남동3가 기린대로 99',
    location:{
      lat:35.8147105,
      lng:127.1526312
    },
    creator:'user-4414'
  },
  {
    id:'p-93645672',
    title: '세빛 둥둥섬',
    description:'3개의 인공 섬으로 이루어져 있으며, 카페와 레스토랑, 야간 조명 장식이 특징입니다.',
    imageURL:'https://lh5.googleusercontent.com/p/AF1QipPuLCiKhVs5DmdSWhAqBlYrRD2CzCWaF67pB2T9=w408-h306-k-no',
    address:'서울특별시 서초구 올림픽대로 2085-14(반포동)',
    location:{
      lat:37.5116807,
      lng:126.9947194
    },
    creator:'user-8716'
  },
  {
    id:'p-58596324',
    title: '서울숲공원',
    description:'나무가 많고 자전거 도로가 있는 넓은 공원으로 장미, 호수, 나비 정원이 있고 사슴에게 먹이를 줄 수 있습니다.',
    imageURL:'https://lh5.googleusercontent.com/p/AF1QipOLUECJXZJqEVIpnSuMWuoSs5_OJJKGZKNKEThs=w408-h272-k-no',
    address:'전라북도 전주시 완산구 풍남동3가 기린대로 99',
    location:{
      lat:37.5443878,
      lng:127.0374424
    },
    creator:'user-3334'
  },
];


const getPlaceById = (req,res,next) => {
  // req.params.dynamicSegment를 통해 사용자가 입력한 값을 받아올 수 있다.
  const placeId = req.params.pid;
  const place = DUMMY_PLACES.find(p => {
    return p.id === placeId
  })
  if(!place) {
    throw new HttpError('Could not find a place for the provided id.',404)
  }
  res.json({place}); // shorthand
  
}

const getPlaceByUserId = (req,res,next) => {
  const userId = req.params.uid;
  const user = DUMMY_PLACES.find(place => {
    return place.creator === userId
  })

  if(!user){
    throw new HttpError('Could not find a place for the provided id.',404)
    
  }
  res.json({user})
}

const createPlace = (req,res,next) => {
  const {title, description, coordinates, address, creator} = req.body;
  const createdPlace = {
    id: v4(),
    title,
    description,
    location: coordinates,
    address,
    creator
  };
  DUMMY_PLACES.push(createdPlace); // 먼저 오게 하려면 unshift(createdPlace)

  // 생성은 201
  res.status(201).json({place: createdPlace})
}
console.log(DUMMY_PLACES)

exports.getPlaceById = getPlaceById
exports.getPlaceByUserId = getPlaceByUserId
exports.createPlace = createPlace

postman으로 post 후
자신이 지정한 url ex) localhost:5000/api/places/user/:uid(user-8878)로 이동하여 createdPlace가 정상적으로 post 되고 있는지 확인.

uuid를 통해 고유 식별자를 생성하여 posting 되는 것을 확인할 수 있다.

0개의 댓글