-
Notifications
You must be signed in to change notification settings - Fork 5
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
[BE] BillActionDetail 초기화시 totalPrice와 정합성 안맞는 버그 수정 #460
Changes from all commits
27b40f1
3b2b01c
18833c8
d6aaa83
ab69ba8
bb6be27
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 |
---|---|---|
|
@@ -10,7 +10,10 @@ | |
import jakarta.persistence.OneToMany; | ||
import jakarta.persistence.OneToOne; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
@@ -45,13 +48,8 @@ public class BillAction implements Comparable<BillAction> { | |
private List<BillActionDetail> billActionDetails = new ArrayList<>(); | ||
|
||
public BillAction(Action action, String title, Long price) { | ||
this(null, action, title, price); | ||
} | ||
|
||
private BillAction(Long id, Action action, String title, Long price) { | ||
validateTitle(title); | ||
validatePrice(price); | ||
this.id = id; | ||
this.action = action; | ||
this.title = title.trim(); | ||
this.price = price; | ||
|
@@ -70,23 +68,69 @@ private void validatePrice(Long price) { | |
} | ||
} | ||
|
||
public static BillAction create(Action action, String title, Long price, CurrentMembers currentMembers) { | ||
BillAction billAction = new BillAction(action, title, price); | ||
billAction.resetBillActionDetails(currentMembers); | ||
return billAction; | ||
} | ||
|
||
public void resetBillActionDetails(CurrentMembers currentMembers) { | ||
this.billActionDetails.clear(); | ||
Iterator<Long> priceIterator = distributePrice(currentMembers.size()).iterator(); | ||
|
||
for (String member : currentMembers.getMembers()) { | ||
BillActionDetail billActionDetail = new BillActionDetail(this, member, priceIterator.next(), false); | ||
this.billActionDetails.add(billActionDetail); | ||
} | ||
} | ||
|
||
private void resetBillActionDetails() { | ||
Iterator<Long> priceIterator = distributePrice(billActionDetails.size()).iterator(); | ||
|
||
billActionDetails.forEach(billActionDetail -> { | ||
billActionDetail.updatePrice(priceIterator.next()); | ||
billActionDetail.updateIsFixed(false); | ||
}); | ||
} | ||
|
||
private List<Long> distributePrice(int memberCount) { | ||
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. A: 지금은 나머지 값을 한 명에게 몰아주는데, 적절하게 분배하는 방향도 좋을 것 같아요. 이 작업은 프론트랑도 함께 논의해야 겠어요. |
||
if (memberCount == 0) { | ||
return new ArrayList<>(); | ||
} | ||
long eachPrice = price / memberCount; | ||
long remainder = price % memberCount; | ||
|
||
List<Long> results = Stream.generate(() -> eachPrice) | ||
.limit(memberCount - 1) | ||
.collect(Collectors.toList()); | ||
results.add(eachPrice + remainder); | ||
Comment on lines
+103
to
+106
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. A: 가만보니, add 순서도 중요하겠군요. 현재는 프론트랑 백 모두 마지막 사람에게 나머지를 몰아주는 방식이에요. 양쪽 모두 정책이 동일하게 유지되어야 하는 비용이 있네요. |
||
return results; | ||
} | ||
|
||
public void update(String title, Long price) { | ||
validateTitle(title); | ||
validatePrice(price); | ||
this.title = title; | ||
this.title = title.trim(); | ||
this.price = price; | ||
resetBillActionDetails(); | ||
} | ||
|
||
public boolean isSamePrice(Long price) { | ||
return this.price.equals(price); | ||
public void addDetails(List<BillActionDetail> billActionDetails) { | ||
billActionDetails.forEach(this::addDetail); | ||
} | ||
|
||
public Long getSequence() { | ||
return action.getSequence(); | ||
private void addDetail(BillActionDetail billActionDetail) { | ||
this.billActionDetails.add(billActionDetail); | ||
billActionDetail.setBillAction(this); | ||
} | ||
|
||
public Event getEvent() { | ||
return action.getEvent(); | ||
public boolean isFixed() { | ||
return billActionDetails.stream() | ||
.anyMatch(BillActionDetail::isFixed); | ||
} | ||
|
||
public boolean isSamePrice(Long price) { | ||
return this.price.equals(price); | ||
} | ||
|
||
public Long findPriceByMemberName(String memberName) { | ||
|
@@ -97,20 +141,12 @@ public Long findPriceByMemberName(String memberName) { | |
.orElse(DEFAULT_PRICE); | ||
} | ||
|
||
public boolean isFixed() { | ||
return billActionDetails.stream() | ||
.map(BillActionDetail::getPrice) | ||
.distinct() | ||
.count() != 1L; | ||
} | ||
|
||
public void addDetails(List<BillActionDetail> billActionDetails) { | ||
billActionDetails.forEach(this::addDetail); | ||
public Long getSequence() { | ||
return action.getSequence(); | ||
} | ||
|
||
private void addDetail(BillActionDetail billActionDetail) { | ||
this.billActionDetails.add(billActionDetail); | ||
billActionDetail.setBillAction(this); | ||
Comment on lines
-111
to
-113
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. c: 테스트에서만 사용하는 메소드 제거 대상 |
||
public Event getEvent() { | ||
return action.getEvent(); | ||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.stereotype.Repository; | ||
import server.haengdong.domain.event.Event; | ||
|
||
@Repository | ||
public interface BillActionDetailRepository extends JpaRepository<BillActionDetail, Long> { | ||
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. c: 테스트에서만 사용하는 클래스 제거 대상 |
||
|
@@ -15,8 +14,4 @@ public interface BillActionDetailRepository extends JpaRepository<BillActionDeta | |
where bd.billAction = :billAction | ||
""") | ||
List<BillActionDetail> findAllByBillAction(BillAction billAction); | ||
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. c: 테스트에서만 사용하는 메소드 제거 대상 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. ㅋㅋㅋㅋㅋㅋㅋㅋ 제거 대상 왤케 많아 |
||
|
||
void deleteAllByBillAction(BillAction billAction); | ||
|
||
void deleteByBillAction_Action_EventAndBillAction_ActionId(Event event, Long actionId); | ||
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. A: 악마 같은 메서드가 지워져서 다행입니다 😀 |
||
} |
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.
c: private으로 닫고 싶다