-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[team-21] 데이먼 첫 번째 PR 드립니다. #44
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package team21.airbnb.controller; | ||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import team21.airbnb.dto.response.RoomChargeDistributionResponse; | ||
import team21.airbnb.service.RoomService; | ||
|
||
@RestController | ||
public class HomeController { | ||
|
||
private final RoomService roomService; | ||
|
||
public HomeController(RoomService roomService) { | ||
this.roomService = roomService; | ||
} | ||
|
||
@GetMapping("/room_charge_distribution") | ||
public RoomChargeDistributionResponse getRoomChargeDistribution() { | ||
return roomService.getRoomChargeDistribution(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,12 @@ | |
import javax.persistence.Enumerated; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.ManyToOne; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
|
@@ -20,7 +22,7 @@ | |
public class Room { | ||
|
||
@Id | ||
@GeneratedValue | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "room_id") | ||
private Long id; | ||
|
||
|
@@ -35,17 +37,17 @@ public class Room { | |
|
||
private String name; | ||
|
||
private int maxNumOfGuests; | ||
private Integer maxNumOfGuests; | ||
|
||
private int numOfBedrooms; | ||
private Integer numOfBedrooms; | ||
|
||
private int numOfBaths; | ||
private Integer numOfBaths; | ||
|
||
private int cleaningFee; | ||
private Integer cleaningFee; | ||
|
||
private int roomCharge; | ||
private Integer roomCharge; | ||
|
||
private int weeklyDiscountPercent; | ||
private Integer weeklyDiscountPercent; | ||
|
||
@Embedded | ||
private Location location; | ||
|
@@ -54,4 +56,22 @@ public class Room { | |
@JoinColumn(name = "host_id") | ||
private User host; | ||
|
||
@Builder | ||
public Room(RoomType roomType, SpaceType spaceType, String description, String name, | ||
Integer maxNumOfGuests, Integer numOfBedrooms, Integer numOfBaths, | ||
Integer cleaningFee, Integer roomCharge, Integer weeklyDiscountPercent, | ||
Location location, User host) { | ||
this.roomType = roomType; | ||
this.spaceType = spaceType; | ||
this.description = description; | ||
this.name = name; | ||
this.maxNumOfGuests = maxNumOfGuests; | ||
this.numOfBedrooms = numOfBedrooms; | ||
this.numOfBaths = numOfBaths; | ||
this.cleaningFee = cleaningFee; | ||
this.roomCharge = roomCharge; | ||
this.weeklyDiscountPercent = weeklyDiscountPercent; | ||
this.location = location; | ||
this.host = host; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. builder 패턴의 장단점을 고민하시고 다음 리뷰에 생각을 써주시면 좋을 것 같아요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 충분한 공부 이후 생각을 작성해보도록 하겠습니다 |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package team21.airbnb.dto.response; | ||
|
||
import java.util.Map; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class RoomChargeDistributionResponse { | ||
|
||
private Integer minRoomCharge; | ||
private Integer maxRoomCharge; | ||
private Map<Integer, Integer> graph; | ||
|
||
public RoomChargeDistributionResponse(Integer minRoomCharge, Integer maxRoomCharge, | ||
Map<Integer, Integer> graph) { | ||
this.minRoomCharge = minRoomCharge; | ||
this.maxRoomCharge = maxRoomCharge; | ||
this.graph = graph; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package team21.airbnb.repository; | ||
|
||
import java.util.List; | ||
import javax.persistence.EntityManager; | ||
import org.springframework.stereotype.Repository; | ||
import team21.airbnb.domain.Room; | ||
|
||
@Repository | ||
public class RoomRepository { | ||
|
||
private final EntityManager em; | ||
|
||
public RoomRepository(EntityManager em) { | ||
this.em = em; | ||
} | ||
|
||
public List<Room> findAll() { | ||
return em.createQuery("select r from Room r", Room.class) | ||
.getResultList(); | ||
} | ||
|
||
public Long save(Room room) { | ||
em.persist(room); | ||
return room.getId(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package team21.airbnb.service; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import team21.airbnb.domain.Room; | ||
import team21.airbnb.dto.response.RoomChargeDistributionResponse; | ||
import team21.airbnb.repository.RoomRepository; | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
public class RoomService { | ||
|
||
public static final double DIGIT = 10.0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저는 이게 무슨 변수인지 잘 모르겠어요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DISTRIBUTION_RANGE 가 더 적절한 이름 같습니다.! |
||
|
||
private final RoomRepository roomRepository; | ||
|
||
public RoomService(RoomRepository roomRepository) { | ||
this.roomRepository = roomRepository; | ||
} | ||
|
||
public RoomChargeDistributionResponse getRoomChargeDistribution() { | ||
List<Room> rooms = roomRepository.findAll(); | ||
Map<Integer, Integer> graph = new HashMap<>(); | ||
int maxRoomCharge = Integer.MIN_VALUE; | ||
int minRoomCharge = Integer.MAX_VALUE; | ||
|
||
for (Room room : rooms) { | ||
int roomCharge = room.getRoomCharge(); | ||
maxRoomCharge = Math.max(maxRoomCharge, roomCharge); | ||
minRoomCharge = Math.min(minRoomCharge, roomCharge); | ||
|
||
graph.merge((int) (Math.ceil(roomCharge / DIGIT) * DIGIT), 1, Integer::sum); | ||
} | ||
|
||
return new RoomChargeDistributionResponse(minRoomCharge, maxRoomCharge, graph); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 메서드는 무슨 일을 하는건가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
@Transactional | ||
public Long save(Room room) { | ||
// TODO : parameter DTO 로 받기, Host 추가 | ||
return roomRepository.save(room); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package team21.airbnb.controller; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.boot.test.web.server.LocalServerPort; | ||
import team21.airbnb.domain.Room; | ||
import team21.airbnb.dto.response.RoomChargeDistributionResponse; | ||
import team21.airbnb.repository.RoomRepository; | ||
import team21.airbnb.service.RoomService; | ||
|
||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) | ||
class HomeControllerTest { | ||
|
||
@LocalServerPort int port; | ||
|
||
@Autowired TestRestTemplate restTemplate; | ||
|
||
public static final int MIN_ROOM_CHARGE = 10005; | ||
public static final int MAX_ROOM_CHARGE = MIN_ROOM_CHARGE + 1000; | ||
|
||
|
||
@Autowired RoomRepository roomRepository; | ||
@Autowired RoomService roomService; | ||
@Autowired HomeController homeController; | ||
|
||
@Test | ||
@DisplayName("가격 분포 요청 시 응답한다") | ||
public void getChargeDistribution() { | ||
// given | ||
Room room1 = Room.builder().name("room1").roomCharge(MIN_ROOM_CHARGE).build(); | ||
Room room2 = Room.builder().name("room2").roomCharge(ceil(MIN_ROOM_CHARGE)).build(); | ||
Room room3 = Room.builder().name("room3").roomCharge(MAX_ROOM_CHARGE).build(); | ||
|
||
roomService.save(room1); | ||
roomService.save(room2); | ||
roomService.save(room3); | ||
|
||
String url = "http://localhost:" + port + "/room_charge_distribution"; | ||
|
||
// when | ||
RoomChargeDistributionResponse roomChargeDistribution | ||
= restTemplate.getForObject(url, RoomChargeDistributionResponse.class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spring에서는 resttemplate보다 좋은 웹 레이어 테스트 방법을 지원해줄 것 같아요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 좋은 방법을 찾아보겠습니다. |
||
|
||
// then | ||
assertThat(roomChargeDistribution.getMinRoomCharge()).isEqualTo(MIN_ROOM_CHARGE); | ||
assertThat(roomChargeDistribution.getMaxRoomCharge()).isEqualTo(MAX_ROOM_CHARGE); | ||
assertThat(roomChargeDistribution.getGraph().get(ceil(MIN_ROOM_CHARGE))).isEqualTo(2); | ||
assertThat(roomChargeDistribution.getGraph().get(ceil(MAX_ROOM_CHARGE))).isEqualTo(1); | ||
} | ||
|
||
private int ceil(int num) { | ||
return (int) (Math.ceil(num / RoomService.DIGIT) * RoomService.DIGIT); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 메서드의 설계에 대해 데이먼의 생각을 알려주세요. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Room 에 ceilRoomCharge() 메서드를 만들어 책임을 넘기는 게 더 적절하다고 생각하는데 Dan이 의도한 사항이 맞을까요? |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit
lombok을 활용할 수 있을 것 같아요.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@requiredargsconstructor 를 활용하겠습니다