From d2ee23d0cfed871e9705efb869c021bffdc64852 Mon Sep 17 00:00:00 2001 From: "haveagood@naver.com" Date: Tue, 26 May 2020 20:37:26 +0900 Subject: [PATCH] =?UTF-8?q?[BE]=20feat=20:=20min,max=20Price=EC=97=90=20?= =?UTF-8?q?=EB=94=B0=EB=A5=B8=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=ED=95=84?= =?UTF-8?q?=ED=84=B0=EB=A7=81=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - min, max Price에 따른 필터링 기능 구현 - 우선적으로 Controller에서 문자열로 값을 입력받도록 하였다. - 혹시모를 RequestParam조작을 대비하여 문자열로 받고, Controller에서 파싱하도록 하였다. - 파싱하는 과정에서 에러가 발생할 경우 min, max값을 DEFAULT로 설정한 값으로 가도록 하였다. issue : #5, #6 ------------------------- ------------------------- --- .../common/CommonStaticsProperties.java | 3 ++ .../controller/AirBnbControllerAlex.java | 8 +-- .../airbnb3/dao/PropertiesDaoAlex.java | 50 +++++++++---------- .../airbnb3/service/AirBnbService.java | 27 ++++++++-- 4 files changed, 55 insertions(+), 33 deletions(-) diff --git a/be/src/main/java/com/airbnb3/codesquad/airbnb3/common/CommonStaticsProperties.java b/be/src/main/java/com/airbnb3/codesquad/airbnb3/common/CommonStaticsProperties.java index d9c6f4c..cd5409b 100644 --- a/be/src/main/java/com/airbnb3/codesquad/airbnb3/common/CommonStaticsProperties.java +++ b/be/src/main/java/com/airbnb3/codesquad/airbnb3/common/CommonStaticsProperties.java @@ -3,4 +3,7 @@ public class CommonStaticsProperties { public static final Integer PAGE_VIEW_ITEM_COUNT = 20; public static final Integer DEFAULT_ADULT_NUM = 1; + + public static final Double DEFAULT_MIN_PRICE = 0D; + public static final Double DEFAULT_MAX_PRICE = 100000000D; } diff --git a/be/src/main/java/com/airbnb3/codesquad/airbnb3/controller/AirBnbControllerAlex.java b/be/src/main/java/com/airbnb3/codesquad/airbnb3/controller/AirBnbControllerAlex.java index 4ce1fed..9770b2a 100644 --- a/be/src/main/java/com/airbnb3/codesquad/airbnb3/controller/AirBnbControllerAlex.java +++ b/be/src/main/java/com/airbnb3/codesquad/airbnb3/controller/AirBnbControllerAlex.java @@ -22,14 +22,14 @@ public class AirBnbControllerAlex { @GetMapping("/main") public ResponseEntity> stayedPage( @RequestParam(value = "offset", required = false, defaultValue = "1") Integer pageNum, - @RequestParam(value = "adult", required = false, defaultValue = "1") Integer adult, + @RequestParam(value = "adults", required = false, defaultValue = "1") Integer adults, @RequestParam(value = "children", required = false, defaultValue = "0") Integer children, @RequestParam(value = "infants", required = false, defaultValue = "0") Integer infants, @RequestParam(value = "checkin", required = false) String checkIn, @RequestParam(value = "checkout", required = false) String checkOut, - @RequestParam(value = "price_min", required = false) Double minPrice, - @RequestParam(value = "price_max", required = false) Double maxPrice + @RequestParam(value = "price_min", required = false, defaultValue = "min") String minRange, + @RequestParam(value = "price_max", required = false, defaultValue = "max") String maxRange ) { - return new ResponseEntity<>(airBnbService.stayedProperties(pageNum, adult, children, infants,checkIn,checkOut), HttpStatus.OK); + return new ResponseEntity<>(airBnbService.stayedProperties(pageNum, adults, children, infants, checkIn, checkOut, minRange, maxRange), HttpStatus.OK); } } diff --git a/be/src/main/java/com/airbnb3/codesquad/airbnb3/dao/PropertiesDaoAlex.java b/be/src/main/java/com/airbnb3/codesquad/airbnb3/dao/PropertiesDaoAlex.java index efb50a1..2796638 100644 --- a/be/src/main/java/com/airbnb3/codesquad/airbnb3/dao/PropertiesDaoAlex.java +++ b/be/src/main/java/com/airbnb3/codesquad/airbnb3/dao/PropertiesDaoAlex.java @@ -24,41 +24,41 @@ public class PropertiesDaoAlex { @Autowired NamedParameterJdbcTemplate jdbcTemplate; - public List getStayedProperties(int propertyRange, int accommodates, Date checkInDate, Date checkOutDate) { + public List getStayedProperties(int propertyRange, int accommodates, + Date checkInDate, Date checkOutDate, + Double minPrice, Double maxPrice) { String sql = "select p.id,p.title,p.state,p.city,p.latitude,p.longitude,p.reservable,p.saved,p.host_type,p.price,p.place_type,p.review_average,p.number_of_reviews, GROUP_CONCAT(i.image_url) AS image " + "FROM properties p LEFT JOIN images i ON p.id = i.properties_id " + "LEFT JOIN detail t ON t.id = p.id LEFT JOIN calender c ON c.properties_id = p.id " + - "WHERE t.accommodates > :accommodates " + - "AND p.id NOT IN (SELECT properties_id FROM calender WHERE reservation_date BETWEEN :checkInDate AND :checkOutDate)" + + "WHERE t.accommodates >= :accommodates " + + "AND p.id NOT IN (SELECT DISTINCT properties_id FROM calender WHERE reservation_date BETWEEN :checkInDate AND :checkOutDate)" + + "AND p.price BETWEEN :minPrice AND :maxPrice " + "GROUP BY p.id LIMIT :propertyRange"; MapSqlParameterSource parameterSource = new MapSqlParameterSource() .addValue("propertyRange", propertyRange) .addValue("accommodates", accommodates) .addValue("checkInDate", checkInDate) - .addValue("checkOutDate", checkOutDate); + .addValue("checkOutDate", checkOutDate) + .addValue("minPrice", minPrice) + .addValue("maxPrice", maxPrice); - return jdbcTemplate.query(sql, parameterSource, new RowMapper() { - @Override - public PropertiesDtoAlex mapRow(ResultSet rs, int rowNum) throws SQLException { - return PropertiesDtoAlex.builder() - .id(rs.getLong("id")) - .title(rs.getString("title")) - .state(rs.getString("state")) - .city(rs.getString("city")) - .latitude(rs.getDouble("latitude")) - .longitude(rs.getDouble("longitude")) - .reservable(rs.getBoolean("reservable")) - .saved(rs.getBoolean("saved")) - .hostType(rs.getString("host_type")) - .price(rs.getDouble("price")) - .placeType(rs.getString("place_type")) - .reviewAverage(rs.getDouble("review_average")) - .numberOfReviews(rs.getInt("number_of_reviews")) - .images(imageParser(rs.getString("image"))) - .build(); - } - }); + return jdbcTemplate.query(sql, parameterSource, (rs, rowNum) -> PropertiesDtoAlex.builder() + .id(rs.getLong("id")) + .title(rs.getString("title")) + .state(rs.getString("state")) + .city(rs.getString("city")) + .latitude(rs.getDouble("latitude")) + .longitude(rs.getDouble("longitude")) + .reservable(rs.getBoolean("reservable")) + .saved(rs.getBoolean("saved")) + .hostType(rs.getString("host_type")) + .price(rs.getDouble("price")) + .placeType(rs.getString("place_type")) + .reviewAverage(rs.getDouble("review_average")) + .numberOfReviews(rs.getInt("number_of_reviews")) + .images(imageParser(rs.getString("image"))) + .build()); } private List imageParser(String images) { diff --git a/be/src/main/java/com/airbnb3/codesquad/airbnb3/service/AirBnbService.java b/be/src/main/java/com/airbnb3/codesquad/airbnb3/service/AirBnbService.java index e689be4..51ada49 100644 --- a/be/src/main/java/com/airbnb3/codesquad/airbnb3/service/AirBnbService.java +++ b/be/src/main/java/com/airbnb3/codesquad/airbnb3/service/AirBnbService.java @@ -18,13 +18,16 @@ public class AirBnbService { @Autowired PropertiesDaoAlex propertiesDao; - //, Double minPrice, Double maxPrice - public List stayedProperties(Integer pageNumber, Integer adult, Integer children, Integer infants, String checkIn, String checkOut) { + // + public List stayedProperties(Integer pageNumber, Integer adults, Integer children, + Integer infants, String checkIn, String checkOut, String minRange, String maxRange) { int propertyRange = pageNumber * PAGE_VIEW_ITEM_COUNT; - int accommodates = adult + children; + int accommodates = adults + children; Date checkInDate = parseStringToDate(checkIn); Date checkOutDate = parseStringToDate(checkOut); - return propertiesDao.getStayedProperties(propertyRange, accommodates,checkInDate,checkOutDate); + Double minPrice = parseStringToMinDouble(minRange); + Double maxPrice = parseStringToMaxDouble(maxRange); + return propertiesDao.getStayedProperties(propertyRange, accommodates, checkInDate, checkOutDate, minPrice, maxPrice); } private Date parseStringToDate(String date) { @@ -34,4 +37,20 @@ private Date parseStringToDate(String date) { return Date.valueOf(LocalDate.now()); } } + + private Double parseStringToMinDouble(String price) { + try { + return Double.parseDouble(price); + } catch (IllegalArgumentException e) { + return DEFAULT_MIN_PRICE; + } + } + + private Double parseStringToMaxDouble(String price) { + try { + return Double.parseDouble(price); + } catch (IllegalArgumentException e) { + return DEFAULT_MAX_PRICE; + } + } }