Skip to content

Commit d995774

Browse files
authored
[#179] User 매너 온도 BigDecimal 타입으로 변경하여 소수점 에러 해결 (#188)
* refactor: UserProfileResponse 매너온도 타입 String 변경 * refactor: 매너온도 타입 String 변경 * refactor: 중복 제거 및 매너온도 타입 String 변경 * refactor: reviewer, reviewee처럼 User에 대한 매너온도 타입 String 변경 * refactor: User mannerScore type 변경 - Double 타입에서 BigDecimal 으로 변경 * refactor: User mannerScore type 변경 & 유효성 검증 메서드 추가 - Double 타입에서 BigDecimal 으로 변경 - 음수값 검증하는 validate method 추가 * test: User mannerScore 검증 로직 * test: Review 작성시 mannerScore 변경되는지 test * test: mannerScore 연산처리 전 not null 검증 추가 * test: mannerScore 연산처리 전 not null 검증 test
1 parent 0833b97 commit d995774

File tree

11 files changed

+144
-44
lines changed

11 files changed

+144
-44
lines changed

src/main/java/com/prgrms/mukvengers/domain/user/dto/response/UserProfileResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ public record UserProfileResponse(
88
Integer leaderCount,
99
Integer crewCount,
1010
Integer tasteScore,
11-
Double mannerScore
11+
String mannerScore
1212
) {
1313
}

src/main/java/com/prgrms/mukvengers/domain/user/model/User.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import static javax.persistence.GenerationType.*;
44
import static lombok.AccessLevel.*;
5+
import static org.springframework.util.Assert.*;
56

7+
import java.math.BigDecimal;
68
import java.util.Objects;
79

810
import javax.persistence.Column;
@@ -31,8 +33,8 @@
3133
public class User extends BaseEntity {
3234

3335
public static final String DEFAULT_INTRODUCE = "자기소개를 작성해주세요";
34-
public static final Double DEFAULT_MANNER_VALUE = 36.5;
3536
public static final Integer ZERO = 0;
37+
public static final BigDecimal CRITERIA = new BigDecimal("0.1");
3638

3739
@Id
3840
@GeneratedValue(strategy = IDENTITY)
@@ -54,7 +56,7 @@ public class User extends BaseEntity {
5456
private String oauthId;
5557

5658
@Column(nullable = false)
57-
private Double mannerScore;
59+
private BigDecimal mannerScore;
5860

5961
@Column(nullable = false)
6062
private Integer tasteScore;
@@ -78,7 +80,7 @@ protected User(String nickname, String profileImgUrl, String provider, String oa
7880
this.provider = provider;
7981
this.oauthId = oauthId;
8082
this.introduction = DEFAULT_INTRODUCE;
81-
this.mannerScore = DEFAULT_MANNER_VALUE;
83+
this.mannerScore = new BigDecimal("36.5");
8284
this.tasteScore = ZERO;
8385
this.leaderCount = ZERO;
8486
this.crewCount = ZERO;
@@ -115,7 +117,10 @@ public boolean isSameUser(Long userId) {
115117
}
116118

117119
public void addMannerScore(Integer mannerScore) {
118-
this.mannerScore += (0.1 * mannerScore);
120+
notNull(mannerScore, "유효하지 않는 매너 온도입니다.");
121+
BigDecimal score = CRITERIA.multiply(new BigDecimal(mannerScore));
122+
BigDecimal result = this.mannerScore.add(score);
123+
this.mannerScore = checkMinusMannerScore(result);
119124
}
120125

121126
public void addTasteScore(Integer tasteScore) {
@@ -130,4 +135,14 @@ public void updateCrewCount() {
130135
this.crewCount++;
131136
}
132137

138+
public BigDecimal checkMinusMannerScore(BigDecimal mannerScore) {
139+
140+
BigDecimal zero = new BigDecimal("0.0");
141+
142+
// 값 비교를 위해 사용하며 값이 동일하면 0, 적으면 -1, 많으면 1을 반환함
143+
if (mannerScore.compareTo(zero) > 0) {
144+
return mannerScore;
145+
}
146+
return zero;
147+
}
133148
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE users
2+
MODIFY manner_score DECIMAL(3, 1) NOT NULL DEFAULT 36.5;

src/test/java/com/prgrms/mukvengers/domain/proposal/api/ProposalControllerTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ void getById_success() throws Exception {
117117
fieldWithPath("data.user.leaderCount").type(NUMBER).description("방장 횟수"),
118118
fieldWithPath("data.user.crewCount").type(NUMBER).description("모임 참여 횟수"),
119119
fieldWithPath("data.user.tasteScore").type(NUMBER).description("맛잘알 점수"),
120-
fieldWithPath("data.user.mannerScore").type(NUMBER).description("매너 온도"),
121-
fieldWithPath("data.user.mannerScore").type(NUMBER).description("매너 온도"))
120+
fieldWithPath("data.user.mannerScore").type(STRING).description("매너 온도"))
122121
.build()
123122
)
124123
));
@@ -150,7 +149,7 @@ void getProposalsByLeaderId_success() throws Exception {
150149
fieldWithPath("data.responses.[].user.leaderCount").type(NUMBER).description("방장 횟수"),
151150
fieldWithPath("data.responses.[].user.crewCount").type(NUMBER).description("모임 참여 횟수"),
152151
fieldWithPath("data.responses.[].user.tasteScore").type(NUMBER).description("맛잘알 점수"),
153-
fieldWithPath("data.responses.[].user.mannerScore").type(NUMBER).description("매너 온도"),
152+
fieldWithPath("data.responses.[].user.mannerScore").type(STRING).description("매너 온도"),
154153
fieldWithPath("data.responses.[].id").type(NUMBER).description("신청서 아이디"),
155154
fieldWithPath("data.responses.[].content").type(STRING).description("신청서 내용"),
156155
fieldWithPath("data.responses.[].status").type(STRING).description("신청서 상태"),
@@ -220,8 +219,7 @@ void getProposalsByMemberId_success() throws Exception {
220219
fieldWithPath("data.responses.[].user.leaderCount").type(NUMBER).description("방장 횟수"),
221220
fieldWithPath("data.responses.[].user.crewCount").type(NUMBER).description("모임 참여 횟수"),
222221
fieldWithPath("data.responses.[].user.tasteScore").type(NUMBER).description("맛잘알 점수"),
223-
fieldWithPath("data.responses.[].user.mannerScore").type(NUMBER).description("매너 온도"),
224-
fieldWithPath("data.responses.[].user.mannerScore").type(NUMBER).description("매너 온도"),
222+
fieldWithPath("data.responses.[].user.mannerScore").type(STRING).description("매너 온도"),
225223
fieldWithPath("data.responses.[].id").type(NUMBER).description("신청서 아이디"),
226224
fieldWithPath("data.responses.[].content").type(STRING).description("신청서 내용"),
227225
fieldWithPath("data.responses.[].status").type(STRING).description("신청서 상태"),

src/test/java/com/prgrms/mukvengers/domain/proposal/service/ProposalServiceImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ void getById() {
263263
.hasFieldOrPropertyWithValue("leaderCount", user.getLeaderCount())
264264
.hasFieldOrPropertyWithValue("crewCount", user.getCrewCount())
265265
.hasFieldOrPropertyWithValue("tasteScore", user.getTasteScore())
266-
.hasFieldOrPropertyWithValue("mannerScore", user.getMannerScore());
266+
.hasFieldOrPropertyWithValue("mannerScore", String.valueOf(user.getMannerScore()));
267267
}
268268

269269
@Test

src/test/java/com/prgrms/mukvengers/domain/review/api/ReviewControllerTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ void getSingleReview() throws Exception {
173173
fieldWithPath("data.reviewee.leaderCount").type(NUMBER).description("리뷰이의 리더 횟수"),
174174
fieldWithPath("data.reviewee.crewCount").type(NUMBER).description("리뷰이의 밥모임 참여한 횟수"),
175175
fieldWithPath("data.reviewee.tasteScore").type(NUMBER).description("리뷰이의 맛잘알 점수"),
176-
fieldWithPath("data.reviewee.mannerScore").type(NUMBER).description("리뷰이의 매너 점수"),
176+
fieldWithPath("data.reviewee.mannerScore").type(STRING).description("리뷰이의 매너 점수"),
177177

178178
fieldWithPath("data.crew.currentMember").type(NULL).description("밥모임의 아이디"),
179179
fieldWithPath("data.crew.id").type(NUMBER).description("밥모임의 아이디"),
@@ -239,7 +239,7 @@ void getAllReceivedReview() throws Exception {
239239
.description("리뷰를 작성하고자 하는 사용자의 밥모임 참여 횟수"),
240240
fieldWithPath("data.content.[].reviewer.tasteScore").type(NUMBER)
241241
.description("리뷰를 작성하고자 하는 사용자의 맛잘알 점수"),
242-
fieldWithPath("data.content.[].reviewer.mannerScore").type(NUMBER)
242+
fieldWithPath("data.content.[].reviewer.mannerScore").type(STRING)
243243
.description("리뷰를 작성하고자 하는 사용자의 매너 온도 점수"),
244244

245245
fieldWithPath("data.content.[].reviewee.id").type(NUMBER).description("리뷰 남기고자하는 사용자의 아이디"),
@@ -255,7 +255,7 @@ void getAllReceivedReview() throws Exception {
255255
.description("리뷰 남기고자하는 사용자의 밥모임 참여 횟수"),
256256
fieldWithPath("data.content.[].reviewee.tasteScore").type(NUMBER)
257257
.description("리뷰 남기고자하는 사용자의 맛잘알 점수"),
258-
fieldWithPath("data.content.[].reviewee.mannerScore").type(NUMBER)
258+
fieldWithPath("data.content.[].reviewee.mannerScore").type(STRING)
259259
.description("리뷰 남기고자하는 사용자의 매너 온도 점수"),
260260

261261
fieldWithPath("data.content.[].crew.id").type(NUMBER).description("리뷰하고자하는 밥 모임 아이디"),
@@ -346,7 +346,7 @@ void getAllWroteReview() throws Exception {
346346
.description("리뷰를 작성하고자 하는 사용자의 밥모임 참여 횟수"),
347347
fieldWithPath("data.content.[].reviewer.tasteScore").type(NUMBER)
348348
.description("리뷰를 작성하고자 하는 사용자의 맛잘알 점수"),
349-
fieldWithPath("data.content.[].reviewer.mannerScore").type(NUMBER)
349+
fieldWithPath("data.content.[].reviewer.mannerScore").type(STRING)
350350
.description("리뷰를 작성하고자 하는 사용자의 매너 온도 점수"),
351351

352352
fieldWithPath("data.content.[].reviewee.id").type(NUMBER).description("리뷰 남기고자하는 사용자의 아이디"),
@@ -362,7 +362,7 @@ void getAllWroteReview() throws Exception {
362362
.description("리뷰 남기고자하는 사용자의 밥모임 참여 횟수"),
363363
fieldWithPath("data.content.[].reviewee.tasteScore").type(NUMBER)
364364
.description("리뷰 남기고자하는 사용자의 맛잘알 점수"),
365-
fieldWithPath("data.content.[].reviewee.mannerScore").type(NUMBER)
365+
fieldWithPath("data.content.[].reviewee.mannerScore").type(STRING)
366366
.description("리뷰 남기고자하는 사용자의 매너 온도 점수"),
367367

368368
fieldWithPath("data.content.[].crew.id").type(NUMBER).description("리뷰하고자하는 밥 모임 아이디"),

src/test/java/com/prgrms/mukvengers/domain/review/service/ReviewServiceImplTest.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import static com.prgrms.mukvengers.utils.ReviewObjectProvider.*;
55
import static com.prgrms.mukvengers.utils.UserObjectProvider.*;
66
import static org.assertj.core.api.Assertions.*;
7+
import static org.junit.jupiter.api.Assertions.*;
78

9+
import java.math.BigDecimal;
810
import java.util.List;
911
import java.util.Optional;
1012

@@ -63,8 +65,13 @@ void createLeaderReviewTest_success() {
6365
Optional<Review> findReview = reviewRepository.findById(leaderReview.id());
6466

6567
// then
66-
assertThat(findReview).isPresent();
67-
assertThat(findReview.get().getReviewee().getId()).isEqualTo(leader.getUserId());
68+
assertThat(findReview)
69+
.hasValueSatisfying(review -> assertAll(
70+
() -> assertThat(review.getReviewee().getId()).isEqualTo(leader.getUserId()),
71+
() -> assertThat(review.getMannerScore()).isEqualTo(MANNER_SCORE),
72+
() -> assertThat(review.getTasteScore()).isEqualTo(TASTE_SCORE),
73+
() -> assertThat(review.getReviewee().getMannerScore()).isEqualTo(new BigDecimal("37.0"))
74+
));
6875
}
6976

7077
@Test
@@ -85,13 +92,16 @@ void createMemberReviewTest_success() {
8592
CreateMemberReviewRequest memberReviewRequest = createMemberReviewRequest(reviewee.getId());
8693

8794
// when
88-
IdResponse review = reviewService.createMemberReview(memberReviewRequest, reviewer.getId(), crew.getId());
89-
Optional<Review> findReview = reviewRepository.findById(review.id());
95+
IdResponse memberReview = reviewService.createMemberReview(memberReviewRequest, reviewer.getId(), crew.getId());
96+
Optional<Review> findReview = reviewRepository.findById(memberReview.id());
9097

9198
// then
92-
assertThat(findReview).isPresent();
93-
assertThat(findReview.get().getMannerScore()).isEqualTo(MANNER_SCORE);
94-
assertThat(findReview.get().getTasteScore()).isZero();
99+
assertThat(findReview)
100+
.hasValueSatisfying(review -> assertAll(
101+
() -> assertThat(review.getMannerScore()).isEqualTo(MANNER_SCORE),
102+
() -> assertThat(review.getTasteScore()).isZero(),
103+
() -> assertThat(review.getReviewee().getMannerScore()).isEqualTo(new BigDecimal("37.0"))
104+
));
95105
}
96106

97107
@Test

src/test/java/com/prgrms/mukvengers/domain/user/api/UserControllerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ void getMyProfile_success() throws Exception {
5555
fieldWithPath("data.leaderCount").type(NUMBER).description("방장 횟수"),
5656
fieldWithPath("data.crewCount").type(NUMBER).description("모임 참여 횟수"),
5757
fieldWithPath("data.tasteScore").type(NUMBER).description("맛잘알 점수"),
58-
fieldWithPath("data.mannerScore").type(NUMBER).description("매너 온도"))
58+
fieldWithPath("data.mannerScore").type(STRING).description("매너 온도"))
5959
.build()
6060
))
6161
);
@@ -93,7 +93,7 @@ void getUserProfile_success() throws Exception {
9393
fieldWithPath("data.leaderCount").type(NUMBER).description("방장 횟수"),
9494
fieldWithPath("data.crewCount").type(NUMBER).description("모임 참여 횟수"),
9595
fieldWithPath("data.tasteScore").type(NUMBER).description("맛잘알 점수"),
96-
fieldWithPath("data.mannerScore").type(NUMBER).description("매너 온도"))
96+
fieldWithPath("data.mannerScore").type(STRING).description("매너 온도"))
9797
.build()
9898
))
9999
);
@@ -134,7 +134,7 @@ void updateMyProfile_success() throws Exception {
134134
fieldWithPath("data.leaderCount").type(NUMBER).description("방장 횟수"),
135135
fieldWithPath("data.crewCount").type(NUMBER).description("모임 참여 횟수"),
136136
fieldWithPath("data.tasteScore").type(NUMBER).description("맛잘알 점수"),
137-
fieldWithPath("data.mannerScore").type(NUMBER).description("매너 온도"))
137+
fieldWithPath("data.mannerScore").type(STRING).description("매너 온도"))
138138
.build()
139139
))
140140
);

src/test/java/com/prgrms/mukvengers/domain/user/model/UserTest.java

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.prgrms.mukvengers.domain.user.model;
22

33
import static org.assertj.core.api.Assertions.*;
4+
import static org.junit.jupiter.api.Assertions.*;
45

6+
import java.math.BigDecimal;
57
import java.util.stream.Stream;
68

79
import org.junit.jupiter.api.DisplayName;
@@ -41,7 +43,7 @@ void create_success() {
4143
.hasFieldOrPropertyWithValue("leaderCount", 0)
4244
.hasFieldOrPropertyWithValue("crewCount", 0)
4345
.hasFieldOrPropertyWithValue("tasteScore", 0)
44-
.hasFieldOrPropertyWithValue("mannerScore", 36.5)
46+
.hasFieldOrPropertyWithValue("mannerScore", new BigDecimal("36.5"))
4547
.hasFieldOrPropertyWithValue("provider", "kakao")
4648
.hasFieldOrPropertyWithValue("oauthId", "12345")
4749
.hasFieldOrPropertyWithValue("reportedCount", 0)
@@ -58,4 +60,77 @@ void validateNickname_success(String inputNickName) {
5860
.isInstanceOf(IllegalArgumentException.class);
5961
}
6062

63+
@Test
64+
@DisplayName("[성공] mannerScore 를 양수값으로 받으면 mannerScore 값이 증가한다.")
65+
void addMannerScore_add_success() {
66+
67+
//given
68+
User user = User.builder()
69+
.nickname("테스트")
70+
.profileImgUrl("https://defaultImg.jpg")
71+
.provider("kakao")
72+
.oauthId("12345")
73+
.build();
74+
75+
// when
76+
user.addMannerScore(5);
77+
78+
// then
79+
assertThat(user.getMannerScore()).isEqualTo(new BigDecimal("37.0"));
80+
}
81+
82+
@Test
83+
@DisplayName("[성공] mannerScore 를 음수값으로 받으면 mannerScore 값이 감소한다.")
84+
void addMannerScore_minus_success() {
85+
86+
//given
87+
User user = User.builder()
88+
.nickname("테스트")
89+
.profileImgUrl("https://defaultImg.jpg")
90+
.provider("kakao")
91+
.oauthId("12345")
92+
.build();
93+
94+
// when
95+
user.addMannerScore(-3);
96+
97+
// then
98+
assertThat(user.getMannerScore()).isEqualTo(new BigDecimal("36.2"));
99+
}
100+
101+
@Test
102+
@DisplayName("[성공] mannerScore 값은 0.0 이하로 내려갈 수 없다.")
103+
void mannerScore_notMinus_success() {
104+
105+
//given
106+
User user = User.builder()
107+
.nickname("테스트")
108+
.profileImgUrl("https://defaultImg.jpg")
109+
.provider("kakao")
110+
.oauthId("12345")
111+
.build();
112+
113+
// when
114+
user.addMannerScore(-400);
115+
116+
// then
117+
assertThat(user.getMannerScore()).isEqualTo(new BigDecimal("0.0"));
118+
}
119+
120+
@Test
121+
@DisplayName("[실패] mannerScore에 null 값이 들어온다면 매너 온도 연산처리할 수 없다.")
122+
void mannerScore_null_fail() {
123+
124+
//given
125+
User user = User.builder()
126+
.nickname("테스트")
127+
.profileImgUrl("https://defaultImg.jpg")
128+
.provider("kakao")
129+
.oauthId("12345")
130+
.build();
131+
132+
133+
// when & then
134+
assertThrows(IllegalArgumentException.class, () -> user.addMannerScore(null));
135+
}
61136
}

src/test/java/com/prgrms/mukvengers/domain/user/service/UserServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void updateUserProfile_success() {
6060
.hasFieldOrPropertyWithValue("leaderCount", 0)
6161
.hasFieldOrPropertyWithValue("crewCount", 0)
6262
.hasFieldOrPropertyWithValue("tasteScore", 0)
63-
.hasFieldOrPropertyWithValue("mannerScore", 36.5)
63+
.hasFieldOrPropertyWithValue("mannerScore", "36.5")
6464
;
6565
}
6666

0 commit comments

Comments
 (0)