Skip to content

Commit

Permalink
refactor: 할인율 계산 로직 수정 (#359)
Browse files Browse the repository at this point in the history
* refactor: 할인율 계산 로직 수정

Co-authored-by: fromitive <fromitive@gmail.com>

* refactor: 소수점 둘째 자리에서 반올림하도록 변경

Co-authored-by: fromitive <fromitive@gmail.com>

* test: 할인율 계산 로직

* fix: 할인율 단위 백분율로 수정

---------

Co-authored-by: fromitive <fromitive@gmail.com>
  • Loading branch information
helenason and fromitive committed Nov 28, 2024
1 parent e7e9d23 commit 2efa781
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
@AllArgsConstructor
public class OfferingPrice {

private static final int ROUNDING_SCALE = 1;

private final int totalCount;
private final int totalPrice;
private final Integer originPrice;
Expand All @@ -21,12 +23,23 @@ public int calculateDividedPrice() {
.intValue();
}

public double calculateDiscountRate() {
return (double) (originPrice - totalPrice / totalCount) / originPrice;
public Double calculateDiscountRate() {
validateOriginPrice();
if (originPrice == null) {
return null;
}
int dividedPrice = totalPrice / totalCount;
double discountRate = (double) (originPrice - dividedPrice) / originPrice * 100;
return roundHalfUp(discountRate, ROUNDING_SCALE);
}

private double roundHalfUp(double number, int roundingScale) {
BigDecimal bigDecimal = new BigDecimal(number);
bigDecimal = bigDecimal.setScale(roundingScale, RoundingMode.HALF_UP);
return bigDecimal.doubleValue();
}
// TODO : 소수점 밑 둘째 자리에서 반올림

public void validateOriginPrice() {
private void validateOriginPrice() {
int dividedPrice = totalPrice / totalCount;
if (originPrice != null && originPrice < dividedPrice) {
throw new MarketException(OfferingErrorCode.CANNOT_ORIGIN_PRICE_LESS_THEN_DIVIDED_PRICE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected List<OfferingEntity> fetchOfferingsWithoutLastId(String searchKeyword,
@Override
protected List<OfferingEntity> fetchOfferingsWithLastOffering(
OfferingEntity lastOffering, String searchKeyword, Pageable pageable) {
double lastDiscountRate = lastOffering.getDiscountRate();
Double lastDiscountRate = lastOffering.getDiscountRate();
return offeringRepository.findHighDiscountOfferingsWithKeyword(
lastDiscountRate, lastOffering.getId(), searchKeyword, pageable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ List<OfferingEntity> findHighDiscountOfferingsWithKeyword(
""")
List<OfferingEntity> findJoinableOfferingsWithKeyword(Long lastId, String keyword, Pageable pageable);


@Query("SELECT MAX(o.id) FROM OfferingEntity o")
Long findMaxId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ private void validateIsProposer(OfferingEntity offering, MemberEntity member) {

public Long saveOffering(OfferingSaveRequest request, MemberEntity member) {
OfferingEntity offering = request.toEntity(member);
OfferingPrice offeringPrice = offering.toOfferingPrice();
offeringPrice.validateOriginPrice();
OfferingEntity savedOffering = offeringRepository.save(offering);

OfferingMemberEntity offeringMember = new OfferingMemberEntity(member, offering, OfferingMemberRole.PROPOSER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonFormat;
import com.zzang.chongdae.member.repository.entity.MemberEntity;
import com.zzang.chongdae.offering.domain.CommentRoomStatus;
import com.zzang.chongdae.offering.domain.OfferingPrice;
import com.zzang.chongdae.offering.domain.OfferingStatus;
import com.zzang.chongdae.offering.repository.entity.OfferingEntity;
import jakarta.validation.constraints.NotBlank;
Expand Down Expand Up @@ -39,9 +40,12 @@ public record OfferingSaveRequest(@NotBlank
String description) {

public OfferingEntity toEntity(MemberEntity member) {
OfferingPrice offeringPrice = new OfferingPrice(totalCount, totalPrice, originPrice);
Double discountRate = offeringPrice.calculateDiscountRate();

return new OfferingEntity(member, title, description, thumbnailUrl, productUrl, meetingDate, meetingAddress,
meetingAddressDetail, meetingAddressDong, totalCount, 1, false,
totalPrice, originPrice, 33.3, OfferingStatus.AVAILABLE, CommentRoomStatus.GROUPING);
totalPrice, originPrice, discountRate, OfferingStatus.AVAILABLE, CommentRoomStatus.GROUPING);
// TODO : 각자 맡은 팀에서 계산하는 로직 생각하기, 객체로 빼서 만드셈~
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ void should_createOffering_when_givenOfferingCreateRequest() {
"www.naver.com/favicon.ico",
5,
10000,
2000,
5000,
"서울특별시 광진구 구의강변로 3길 11",
"상세주소아파트",
"구의동",
Expand All @@ -439,6 +439,33 @@ void should_createOffering_when_givenOfferingCreateRequest() {
.statusCode(201);
}

@DisplayName("원가격을 작성하지 않은 공모 정보를 받아 공모를 작성 할 수 있다.")
@Test
void should_createOffering_when_givenOfferingWithoutOriginPriceCreateRequest() {
OfferingSaveRequest request = new OfferingSaveRequest(
"공모 제목",
"www.naver.com",
"www.naver.com/favicon.ico",
5,
10000,
null,
"서울특별시 광진구 구의강변로 3길 11",
"상세주소아파트",
"구의동",
LocalDateTime.parse("2024-10-11T10:00:00"),
"내용입니다."
);

given(spec).log().all()
.filter(document("create-offering-success-without-originPrice", resource(successSnippets)))
.cookies(cookieProvider.createCookies())
.contentType(ContentType.JSON)
.body(request)
.when().post("/offerings")
.then().log().all()
.statusCode(201);
}

@DisplayName("요청 값에 빈값이 들어오는 경우 예외가 발생한다.")
@Test
void should_throwException_when_emptyValue() {
Expand Down

0 comments on commit 2efa781

Please sign in to comment.