diff --git a/server/src/main/java/server/haengdong/application/BillActionService.java b/server/src/main/java/server/haengdong/application/BillActionService.java index 203bc39be..9f577c1b4 100644 --- a/server/src/main/java/server/haengdong/application/BillActionService.java +++ b/server/src/main/java/server/haengdong/application/BillActionService.java @@ -35,20 +35,16 @@ public class BillActionService { public void saveAllBillAction(String eventToken, List requests) { Event event = getEvent(eventToken); Action action = createStartAction(event); - - /* 이 부분 함께 쓰이는 경우가 많은데, 도메인 또는 별개로 */ List 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); + } } } @@ -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) diff --git a/server/src/main/java/server/haengdong/domain/action/CurrentMembers.java b/server/src/main/java/server/haengdong/domain/action/CurrentMembers.java index 7706c05d1..ba380aae5 100644 --- a/server/src/main/java/server/haengdong/domain/action/CurrentMembers.java +++ b/server/src/main/java/server/haengdong/domain/action/CurrentMembers.java @@ -67,6 +67,10 @@ public boolean isEmpty() { return members.isEmpty(); } + public boolean isNotEmpty() { + return !members.isEmpty(); + } + public int size() { return members.size(); }