Skip to content
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

Merged
merged 4 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions BE/src/main/java/team21/airbnb/controller/HomeController.java
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;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit
lombok을 활용할 수 있을 것 같아요.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@requiredargsconstructor 를 활용하겠습니다


@GetMapping("/room_charge_distribution")
public RoomChargeDistributionResponse getRoomChargeDistribution() {
return roomService.getRoomChargeDistribution();
}
}
3 changes: 2 additions & 1 deletion BE/src/main/java/team21/airbnb/domain/Amenity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import lombok.AccessLevel;
import lombok.Getter;
Expand All @@ -14,7 +15,7 @@
public class Amenity {

@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "amenity_id")
private Long id;

Expand Down
3 changes: 2 additions & 1 deletion BE/src/main/java/team21/airbnb/domain/Booking.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand All @@ -20,7 +21,7 @@
public class Booking {

@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "booking_id")
private Long id;

Expand Down
3 changes: 2 additions & 1 deletion BE/src/main/java/team21/airbnb/domain/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
Expand All @@ -17,7 +18,7 @@
public class Image {

@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "image_id")
private Long id;

Expand Down
34 changes: 27 additions & 7 deletions BE/src/main/java/team21/airbnb/domain/Room.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -20,7 +22,7 @@
public class Room {

@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "room_id")
private Long id;

Expand All @@ -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;
Expand All @@ -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;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

builder 패턴의 장단점을 고민하시고 다음 리뷰에 생각을 써주시면 좋을 것 같아요.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

충분한 공부 이후 생각을 작성해보도록 하겠습니다

}
3 changes: 2 additions & 1 deletion BE/src/main/java/team21/airbnb/domain/RoomAmenity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
Expand All @@ -17,7 +18,7 @@
public class RoomAmenity {

@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "room_amenity_id")
private Long id;

Expand Down
3 changes: 2 additions & 1 deletion BE/src/main/java/team21/airbnb/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import lombok.AccessLevel;
Expand All @@ -16,7 +17,7 @@
public class User {

@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id")
private Long id;

Expand Down
3 changes: 2 additions & 1 deletion BE/src/main/java/team21/airbnb/domain/Wish.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
Expand All @@ -17,7 +18,7 @@
public class Wish {

@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "wish_id")
private Long id;

Expand Down
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;
}

}
27 changes: 27 additions & 0 deletions BE/src/main/java/team21/airbnb/repository/RoomRepository.java
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();
}

}
47 changes: 47 additions & 0 deletions BE/src/main/java/team21/airbnb/service/RoomService.java
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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 이게 무슨 변수인지 잘 모르겠어요.
네이밍을 더 고려하는 방법도 있겠지만, 적절한 주석도 좋은 코딩의 일부라고 생각합니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 메서드는 무슨 일을 하는건가요?
네이밍이 직관적이지 않고, 방의 전체 목록을 일단 조회하고 시작하는 것 부터가 일반적이진 않은 느낌이라고 생각해서 잘 이해가 안됩니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 메서드는
image

요금 버튼을 눌렀을 때 위 처럼 그래프를 구성할 수 있게
Room 가격에 따른 Room 갯수를 DIGIT 단위로 distribution하여 전달하는 메서드입니다.


@Transactional
public Long save(Room room) {
// TODO : parameter DTO 로 받기, Host 추가
return roomRepository.save(room);
}

}
3 changes: 2 additions & 1 deletion BE/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spring:
datasource:
url: jdbc:h2:tcp://localhost/~/study/codesquad/team_project/airbnb
url: jdbc:h2:tcp://localhost/~/study/codesquad/team_project/airbnb;MODE=MYSQL

username: sa
password:
Expand All @@ -12,6 +12,7 @@ spring:
properties:
hibernate:
format_sql: true
dialect: org.hibernate.dialect.MySQL5InnoDBDialect

logging.level:
org.hibernate.SQL: debug
Expand Down
60 changes: 60 additions & 0 deletions BE/src/test/java/team21/airbnb/controller/HomeControllerTest.java
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);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spring에서는 resttemplate보다 좋은 웹 레이어 테스트 방법을 지원해줄 것 같아요.
조금 더 찾아보시면 좋을 것 같습니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 메서드의 설계에 대해 데이먼의 생각을 알려주세요.
저는 개선이 필요하다고 생각합니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Room 에 ceilRoomCharge() 메서드를 만들어 책임을 넘기는 게 더 적절하다고 생각하는데 Dan이 의도한 사항이 맞을까요?

}
Loading