Skip to content

Commit

Permalink
[BE] feat : checkin, checkout 별로 필터링 기능 구현
Browse files Browse the repository at this point in the history
- 날짜가 입력되지 않으면 현재의 날짜를 표시하도록 구현
- calender 테이블에 booking이 되지 않는 숙소만 나오도록 구현

issue : #5, #6

-------------------------
-------------------------
  • Loading branch information
haveagood committed May 26, 2020
1 parent cdf1830 commit fffed2f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,11 @@ public ResponseEntity<List<PropertiesDtoAlex>> stayedPage(
@RequestParam(value = "adult", required = false, defaultValue = "1") Integer adult,
@RequestParam(value = "children", required = false, defaultValue = "0") Integer children,
@RequestParam(value = "infants", required = false, defaultValue = "0") Integer infants,
@RequestParam(value = "checkin", required = false) Date checkIn,
@RequestParam(value = "checkout", required = false) Date checkOut,
@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
) {
return new ResponseEntity<>(airBnbService.stayedProperties(pageNum,adult,children,infants), HttpStatus.OK);
return new ResponseEntity<>(airBnbService.stayedProperties(pageNum, adult, children, infants,checkIn,checkOut), HttpStatus.OK);
}
// @GetMapping()
// public void filterdPage(@PathVariable(value = "guest_count",required = false) Integer guestCount,
// @PathVariable(value = "min_price")) {
//
// }
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.airbnb3.codesquad.airbnb3.dao;

import com.airbnb3.codesquad.airbnb3.dto.PropertiesDtoAlex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
Expand All @@ -16,18 +19,24 @@
@Repository
public class PropertiesDaoAlex {

private static final Logger logger = LoggerFactory.getLogger(PropertiesDaoAlex.class);

@Autowired
NamedParameterJdbcTemplate jdbcTemplate;

public List<PropertiesDtoAlex> getStayedProperties(int propertyRange, int accommodates) {
public List<PropertiesDtoAlex> getStayedProperties(int propertyRange, int accommodates, Date checkInDate, Date checkOutDate) {
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 WHERE t.accommodates > :accommodates " +
"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)" +
"GROUP BY p.id LIMIT :propertyRange";

MapSqlParameterSource parameterSource = new MapSqlParameterSource()
.addValue("propertyRange", propertyRange)
.addValue("accommodates", accommodates);
.addValue("accommodates", accommodates)
.addValue("checkInDate", checkInDate)
.addValue("checkOutDate", checkOutDate);

return jdbcTemplate.query(sql, parameterSource, new RowMapper<PropertiesDtoAlex>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.sql.Date;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

Expand All @@ -16,14 +17,21 @@ public class AirBnbService {

@Autowired
PropertiesDaoAlex propertiesDao;
//Date checkIn, Date checkOut, Double minPrice, Double maxPrice
public List<PropertiesDtoAlex> stayedProperties(Integer pageNumber, Integer adult, Integer children, Integer infants) {
Integer propertyRange = pageNumber * PAGE_VIEW_ITEM_COUNT;
Integer accommodates = adult + children;
return propertiesDao.getStayedProperties(propertyRange,accommodates);

//, Double minPrice, Double maxPrice
public List<PropertiesDtoAlex> stayedProperties(Integer pageNumber, Integer adult, Integer children, Integer infants, String checkIn, String checkOut) {
int propertyRange = pageNumber * PAGE_VIEW_ITEM_COUNT;
int accommodates = adult + children;
Date checkInDate = parseStringToDate(checkIn);
Date checkOutDate = parseStringToDate(checkOut);
return propertiesDao.getStayedProperties(propertyRange, accommodates,checkInDate,checkOutDate);
}

private Date parseStringToDate(String date) {
try {
return Date.valueOf(date);
} catch (IllegalArgumentException e) {
return Date.valueOf(LocalDate.now());
}
}
//
// public List<PropertiesDtoAlex> filterdProperties() {
//
// }
}

0 comments on commit fffed2f

Please sign in to comment.