Skip to content

Commit

Permalink
[BE] feat : min,max Price에 따른 데이터 필터링 기능 구현
Browse files Browse the repository at this point in the history
- min, max Price에 따른 필터링 기능 구현
- 우선적으로 Controller에서 문자열로 값을 입력받도록 하였다.
- 혹시모를 RequestParam조작을 대비하여 문자열로 받고, Controller에서
  파싱하도록 하였다.
- 파싱하는 과정에서 에러가 발생할 경우 min, max값을 DEFAULT로 설정한
  값으로 가도록 하였다.

issue : #5, #6

-------------------------
-------------------------
  • Loading branch information
haveagood committed May 26, 2020
1 parent fffed2f commit d2ee23d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public class AirBnbControllerAlex {
@GetMapping("/main")
public ResponseEntity<List<PropertiesDtoAlex>> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,41 @@ public class PropertiesDaoAlex {
@Autowired
NamedParameterJdbcTemplate jdbcTemplate;

public List<PropertiesDtoAlex> getStayedProperties(int propertyRange, int accommodates, Date checkInDate, Date checkOutDate) {
public List<PropertiesDtoAlex> 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<PropertiesDtoAlex>() {
@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<String> imageParser(String images) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ public class AirBnbService {
@Autowired
PropertiesDaoAlex propertiesDao;

//, Double minPrice, Double maxPrice
public List<PropertiesDtoAlex> stayedProperties(Integer pageNumber, Integer adult, Integer children, Integer infants, String checkIn, String checkOut) {
//
public List<PropertiesDtoAlex> 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) {
Expand All @@ -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;
}
}
}

0 comments on commit d2ee23d

Please sign in to comment.