Skip to content

Commit

Permalink
fix: 멤버가 없는 상황에 대해 0으로 나누는 상황 방지
Browse files Browse the repository at this point in the history
  • Loading branch information
3Juhwan committed Aug 16, 2024
1 parent 0298a1a commit 8b218a5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,16 @@ public class BillActionService {
public void saveAllBillAction(String eventToken, List<BillActionAppRequest> requests) {
Event event = getEvent(eventToken);
Action action = createStartAction(event);

/* 이 부분 함께 쓰이는 경우가 많은데, 도메인 또는 별개로 */
List<MemberAction> findMemberActions = memberActionRepository.findAllByEvent(event);
CurrentMembers currentMembers = CurrentMembers.of(findMemberActions);

for (BillActionAppRequest request : requests) {
BillAction billAction = request.toBillAction(action);
/* 로직 숨기기 */
long pricePerMember = billAction.getPrice() / currentMembers.getMembers().size();
billActionRepository.save(billAction);
currentMembers.getMembers().stream()
.map(memberName -> new BillActionDetail(billAction, memberName, pricePerMember))
.forEach(billActionDetailRepository::save);
action = action.next();
if (currentMembers.isNotEmpty()) {
saveBillActionDetails(billAction, currentMembers);
}
}
}

Expand All @@ -58,6 +54,13 @@ private Action createStartAction(Event event) {
.orElse(Action.createFirst(event));
}

private void saveBillActionDetails(BillAction billAction, CurrentMembers currentMembers) {
long pricePerMember = billAction.getPrice() / currentMembers.size();
currentMembers.getMembers().stream()
.map(memberName -> new BillActionDetail(billAction, memberName, pricePerMember))
.forEach(billActionDetailRepository::save);
}

@Transactional
public void updateBillAction(String token, Long actionId, BillActionUpdateAppRequest request) {
BillAction billAction = billActionRepository.findByAction_Id(actionId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public boolean isEmpty() {
return members.isEmpty();
}

public boolean isNotEmpty() {
return !members.isEmpty();
}

public int size() {
return members.size();
}
Expand Down

0 comments on commit 8b218a5

Please sign in to comment.