Skip to content

Commit

Permalink
feat: 지출 내역 추가 시, 상세 내역 생성 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
3Juhwan committed Aug 16, 2024
1 parent 92dd8ce commit 67f8a08
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
import server.haengdong.domain.action.Action;
import server.haengdong.domain.action.ActionRepository;
import server.haengdong.domain.action.BillAction;
import server.haengdong.domain.action.BillActionDetail;
import server.haengdong.domain.action.BillActionDetailRepository;
import server.haengdong.domain.action.BillActionRepository;
import server.haengdong.domain.action.CurrentMembers;
import server.haengdong.domain.action.MemberAction;
import server.haengdong.domain.action.MemberActionRepository;
import server.haengdong.domain.event.Event;
import server.haengdong.domain.event.EventRepository;
import server.haengdong.exception.HaengdongErrorCode;
Expand All @@ -21,6 +26,8 @@
public class BillActionService {

private final BillActionRepository billActionRepository;
private final BillActionDetailRepository billActionDetailRepository;
private final MemberActionRepository memberActionRepository;
private final ActionRepository actionRepository;
private final EventRepository eventRepository;

Expand All @@ -29,9 +36,18 @@ public void saveAllBillAction(String eventToken, List<BillActionAppRequest> requ
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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ public class BillActionDetail {
private String memberName;

private Long price;

public BillActionDetail(BillAction billAction, String memberName, Long price) {
this.billAction = billAction;
this.memberName = memberName;
this.price = price;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
import server.haengdong.application.request.BillActionUpdateAppRequest;
import server.haengdong.domain.action.Action;
import server.haengdong.domain.action.BillAction;
import server.haengdong.domain.action.BillActionDetail;
import server.haengdong.domain.action.BillActionDetailRepository;
import server.haengdong.domain.action.BillActionRepository;
import server.haengdong.domain.action.MemberAction;
import server.haengdong.domain.action.MemberActionRepository;
import server.haengdong.domain.action.MemberActionStatus;
import server.haengdong.domain.event.Event;
import server.haengdong.domain.event.EventRepository;
import server.haengdong.exception.HaengdongException;
Expand All @@ -30,6 +35,12 @@ class BillActionServiceTest extends ServiceTestSupport {
@Autowired
private BillActionRepository billActionRepository;

@Autowired
private BillActionDetailRepository billActionDetailRepository;

@Autowired
private MemberActionRepository memberActionRepository;

@DisplayName("지출 내역을 생성한다.")
@Test
void saveAllBillAction() {
Expand All @@ -52,6 +63,37 @@ void saveAllBillAction() {
);
}

@DisplayName("지출 내역을 생성하면 지출 상세 내역이 생성된다.")
@Test
void saveAllBillActionTest1() {
Event event = Fixture.EVENT1;
Event savedEvent = eventRepository.save(event);
Action action1 = new Action(event, 1L);
Action action2 = new Action(event, 2L);
MemberAction memberAction1 = new MemberAction(action1, "백호", MemberActionStatus.IN, 1L);
MemberAction memberAction2 = new MemberAction(action2, "망쵸", MemberActionStatus.IN, 2L);
memberActionRepository.saveAll(List.of(memberAction1, memberAction2));

List<BillActionAppRequest> request = List.of(
new BillActionAppRequest("뽕족", 10_000L),
new BillActionAppRequest("인생맥주", 15_000L)
);

billActionService.saveAllBillAction(event.getToken(), request);

List<BillActionDetail> billActionDetails = billActionDetailRepository.findAll();

assertThat(billActionDetails)
.hasSize(4)
.extracting("memberName", "price")
.containsExactlyInAnyOrder(
tuple("백호", 5_000L),
tuple("망쵸", 5_000L),
tuple("백호", 7_500L),
tuple("망쵸", 7_500L)
);
}

@DisplayName("이벤트가 존재하지 않으면 지출 내역을 생성할 수 없다.")
@Test
void saveAllBillAction1() {
Expand Down

0 comments on commit 67f8a08

Please sign in to comment.