Skip to content
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] 수정된 지출 상세의 상태 저장 #405

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public BillActionDetailsAppResponse findBillActionDetails(String token, Long act
BillAction billAction = billActionRepository.findByAction_Id(actionId)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.BILL_ACTION_NOT_FOUND));
validateToken(token, billAction);

List<BillActionDetail> billActionDetails = billActionDetailRepository.findAllByBillAction(billAction);

return BillActionDetailsAppResponse.of(billActionDetails);
Expand All @@ -52,6 +52,7 @@ public void updateBillActionDetails(String token, Long actionId, BillActionDetai
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.BILL_ACTION_DETAIL_NOT_FOUND));

detailToUpdate.updatePrice(updateRequest.price());
detailToUpdate.updateIsFixed(updateRequest.isFixed());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private Action createStartAction(Event event) {
private void saveBillActionDetails(BillAction billAction, CurrentMembers currentMembers) {
long pricePerMember = billAction.getPrice() / currentMembers.size();
currentMembers.getMembers().stream()
.map(memberName -> new BillActionDetail(billAction, memberName, pricePerMember))
.map(memberName -> new BillActionDetail(billAction, memberName, pricePerMember, false))
.forEach(billActionDetailRepository::save);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package server.haengdong.application;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -86,26 +88,49 @@ public void deleteMemberAction(String token, Long actionId) {
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.MEMBER_ACTION_NOT_FOUND));

memberActionRepository.deleteAllByMemberNameAndMinSequence(memberAction.getMemberName(),
memberAction.getSequence());
memberAction.getSequence());

List<BillAction> billActions = billActionRepository.findByEventAndGreaterThanSequence(event,
action.getSequence());
action.getSequence());
billActions.forEach(billAction -> resetBillAction(event, billAction));
}

private void resetBillAction(Event event, BillAction billAction) {
List<MemberAction> memberActions = memberActionRepository.findByEventAndSequence(event,
billAction.getSequence());
billAction.getSequence());
CurrentMembers currentMembers = CurrentMembers.of(memberActions);

billActionDetailRepository.deleteAllByBillAction(billAction);

if (currentMembers.isNotEmpty()) {
Long eachPrice = billAction.getPrice() / currentMembers.size();
for (String member : currentMembers.getMembers()) {
BillActionDetail billActionDetail = new BillActionDetail(billAction, member, eachPrice);
billActionDetailRepository.save(billActionDetail);
}
Long price = billAction.getPrice();
int currentMemberCount = currentMembers.size();
long eachPrice = price / currentMemberCount;
long remainder = price % currentMemberCount;
List<BillActionDetail> billActionDetails = getBillActionDetails(
billAction,
currentMembers,
eachPrice,
remainder
);
billActionDetailRepository.saveAll(billActionDetails);
}
}

private List<BillActionDetail> getBillActionDetails(
BillAction billAction,
CurrentMembers currentMembers,
long eachPrice,
long remainder
) {
List<String> members = currentMembers.getMembers().stream().toList();
List<BillActionDetail> billActionDetails = IntStream.range(0, members.size() - 1)
.mapToObj(index -> new BillActionDetail(billAction, members.get(index), eachPrice, false))
.collect(Collectors.toList());
BillActionDetail lastBillActionDetail = new BillActionDetail(billAction, members.get(members.size() - 1),
eachPrice + remainder, false);
billActionDetails.add(lastBillActionDetail);

return billActionDetails;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

public record BillActionDetailUpdateAppRequest(
String name,
Long price
Long price,
boolean isFixed
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

public record BillActionDetailAppResponse(
String name,
Long price
Long price,
boolean isFixed
) {

public static BillActionDetailAppResponse of(BillActionDetail billActionDetail) {
return new BillActionDetailAppResponse(billActionDetail.getMemberName(), billActionDetail.getPrice());
return new BillActionDetailAppResponse(
billActionDetail.getMemberName(),
billActionDetail.getPrice(),
billActionDetail.isFixed()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,21 @@ public class BillActionDetail {

private Long price;

public BillActionDetail(String memberName, Long price) {
this.memberName = memberName;
this.price = price;
}
private boolean isFixed;

public BillActionDetail(BillAction billAction, String memberName, Long price) {
public BillActionDetail(BillAction billAction, String memberName, Long price, boolean isFixed) {
this.billAction = billAction;
this.memberName = memberName;
this.price = price;
this.isFixed = isFixed;
}

public BillActionDetail(Long id, BillAction billAction, String memberName, Long price) {
this.id = id;
this.billAction = billAction;
this.memberName = memberName;
public void updatePrice(Long price) {
this.price = price;
}

public void updatePrice(Long price) {
this.price = price;
public void updateIsFixed(boolean isFixed) {
this.isFixed = isFixed;
}

public boolean hasMemberName(String memberName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ public record BillActionDetailUpdateRequest(
String name,

@NotNull(message = "지출 금액은 공백일 수 없습니다.")
Long price
Long price,

@NotNull(message = "지출 금액은 공백일 수 없습니다.")
boolean isFixed
) {
public BillActionDetailUpdateAppRequest toAppRequest() {
return new BillActionDetailUpdateAppRequest(this.name, this.price);
return new BillActionDetailUpdateAppRequest(this.name, this.price, this.isFixed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

public record BillActionDetailResponse(
String name,
Long price
Long price,
boolean isFixed
) {

public static BillActionDetailResponse of(BillActionDetailAppResponse billActionDetailAppResponse) {
return new BillActionDetailResponse(billActionDetailAppResponse.name(), billActionDetailAppResponse.price());
return new BillActionDetailResponse(
billActionDetailAppResponse.name(),
billActionDetailAppResponse.price(),
billActionDetailAppResponse.isFixed()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.assertj.core.api.Assertions.tuple;
import static server.haengdong.domain.action.MemberActionStatus.IN;
import static server.haengdong.domain.action.MemberActionStatus.OUT;
import static server.haengdong.support.fixture.Fixture.BILL_ACTION;

import java.util.List;
import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -54,15 +55,15 @@ void getMemberBillReports() {
);
billActions.get(0).addDetails(
List.of(
new BillActionDetail("소하", 10_000L),
new BillActionDetail("감자", 40_000L),
new BillActionDetail("쿠키", 10_000L)
new BillActionDetail(BILL_ACTION, "소하", 10_000L, false),
new BillActionDetail(BILL_ACTION, "감자", 40_000L, true),
new BillActionDetail(BILL_ACTION, "쿠키", 10_000L, false)
)
);
billActions.get(1).addDetails(
List.of(
new BillActionDetail("소하", 5_000L),
new BillActionDetail("쿠키", 15_000L)
new BillActionDetail(BILL_ACTION, "소하", 5_000L, true),
new BillActionDetail(BILL_ACTION, "쿠키", 15_000L, true)
)
);
memberActionRepository.saveAll(memberActions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ void findBillActionDetailsTest() {
Action action = new Action(event1, 1L);
BillAction billAction = new BillAction(action, "뽕족", 10000L);
billActionRepository.save(billAction);
BillActionDetail billActionDetail1 = new BillActionDetail(1L, billAction, "토다리", 6000L);
BillActionDetail billActionDetail2 = new BillActionDetail(2L, billAction, "쿠키", 4000L);
BillActionDetail billActionDetail1 = new BillActionDetail(billAction, "토다리", 6000L, true);
BillActionDetail billActionDetail2 = new BillActionDetail(billAction, "쿠키", 4000L, true);
billActionDetailRepository.saveAll(List.of(billActionDetail1, billActionDetail2));

BillActionDetailsAppResponse response = billActionDetailService.findBillActionDetails(
Expand All @@ -67,13 +67,13 @@ void updateBillActionDetailsTest1() {
Action action = new Action(event1, 1L);
BillAction billAction = new BillAction(action, "뽕족", 10000L);
billActionRepository.save(billAction);
BillActionDetail billActionDetail1 = new BillActionDetail(1L, billAction, "토다리", 5000L);
BillActionDetail billActionDetail2 = new BillActionDetail(2L, billAction, "쿠키", 5000L);
BillActionDetail billActionDetail1 = new BillActionDetail(billAction, "토다리", 5000L, false);
BillActionDetail billActionDetail2 = new BillActionDetail(billAction, "쿠키", 5000L, false);
billActionDetailRepository.saveAll(List.of(billActionDetail1, billActionDetail2));

BillActionDetailsUpdateAppRequest request = new BillActionDetailsUpdateAppRequest(List.of(
new BillActionDetailUpdateAppRequest("토다리", 3000L),
new BillActionDetailUpdateAppRequest("쿠키", 4000L)
new BillActionDetailUpdateAppRequest("토다리", 3000L, true),
new BillActionDetailUpdateAppRequest("쿠키", 4000L, true)
));
assertThatCode(
() -> billActionDetailService.updateBillActionDetails(event1.getToken(), action.getId(), request))
Expand All @@ -89,13 +89,13 @@ void updateBillActionDetailsTest2() {
Action action = new Action(event1, 1L);
BillAction billAction = new BillAction(action, "뽕족", 10000L);
billActionRepository.save(billAction);
BillActionDetail billActionDetail1 = new BillActionDetail(1L, billAction, "토다리", 5000L);
BillActionDetail billActionDetail2 = new BillActionDetail(2L, billAction, "쿠키", 5000L);
BillActionDetail billActionDetail1 = new BillActionDetail(billAction, "토다리", 5000L, false);
BillActionDetail billActionDetail2 = new BillActionDetail(billAction, "쿠키", 5000L, false);
billActionDetailRepository.saveAll(List.of(billActionDetail1, billActionDetail2));

BillActionDetailsUpdateAppRequest request = new BillActionDetailsUpdateAppRequest(List.of(
new BillActionDetailUpdateAppRequest("토다리", 3000L),
new BillActionDetailUpdateAppRequest("쿠키", 7000L)
new BillActionDetailUpdateAppRequest("토다리", 3000L, true),
new BillActionDetailUpdateAppRequest("쿠키", 7000L, true)
));
billActionDetailService.updateBillActionDetails(event1.getToken(), action.getId(), request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,10 @@ void updateBillAction2() {
Event savedEvent = eventRepository.save(event);
Action action = Action.createFirst(savedEvent);
BillAction billAction = new BillAction(action, "뽕족", 10_000L);
BillActionDetail billActionDetail1 = new BillActionDetail(billAction, "감자", 3000L);
BillActionDetail billActionDetail2 = new BillActionDetail(billAction, "고구마", 2000L);
BillActionDetail billActionDetail3 = new BillActionDetail(billAction, "당근", 3000L);
BillActionDetail billActionDetail4 = new BillActionDetail(billAction, "양파", 2000L);
BillActionDetail billActionDetail1 = new BillActionDetail(billAction, "감자", 3000L, true);
BillActionDetail billActionDetail2 = new BillActionDetail(billAction, "고구마", 2000L, true);
BillActionDetail billActionDetail3 = new BillActionDetail(billAction, "당근", 3000L, true);
BillActionDetail billActionDetail4 = new BillActionDetail(billAction, "양파", 2000L, true);
billAction.addDetails(List.of(billActionDetail1, billActionDetail2, billActionDetail3, billActionDetail4));
BillAction savedBillAction = billActionRepository.save(billAction);

Expand Down Expand Up @@ -208,8 +208,8 @@ void deleteBillActionTest1() {
MemberAction memberAction1 = new MemberAction(new Action(event, 1L), "백호", MemberActionStatus.IN, 1L);
MemberAction memberAction2 = new MemberAction(new Action(event, 2L), "망쵸", MemberActionStatus.IN, 2L);
BillAction billAction = new BillAction(new Action(event, 3L), "커피", 50_900L);
BillActionDetail billActionDetail1 = new BillActionDetail(billAction, "백호", 25_450L);
BillActionDetail billActionDetail2 = new BillActionDetail(billAction, "망쵸", 25_450L);
BillActionDetail billActionDetail1 = new BillActionDetail(billAction, "백호", 25_450L, false);
BillActionDetail billActionDetail2 = new BillActionDetail(billAction, "망쵸", 25_450L, false);
memberActionRepository.saveAll(List.of(memberAction1, memberAction2));
billActionRepository.save(billAction);
billActionDetailRepository.saveAll(List.of(billActionDetail1, billActionDetail2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ void deleteMember1() {
);
BillAction billAction = new BillAction(new Action(event, 6L), "뽕족", 100_000L);
billActionRepository.save(billAction);
BillActionDetail billActionDetail1 = new BillActionDetail(billAction, "쿠키", 40_000L);
BillActionDetail billActionDetail2 = new BillActionDetail(billAction, "웨디", 30_000L);
BillActionDetail billActionDetail3 = new BillActionDetail(billAction, "감자", 30_000L);
BillActionDetail billActionDetail1 = new BillActionDetail(billAction, "쿠키", 40_000L, true);
BillActionDetail billActionDetail2 = new BillActionDetail(billAction, "웨디", 30_000L, false);
BillActionDetail billActionDetail3 = new BillActionDetail(billAction, "감자", 30_000L, false);
billActionDetailRepository.saveAll(List.of(billActionDetail1, billActionDetail2, billActionDetail3));
List<BillActionDetail> allByBillAction = billActionDetailRepository.findAllByBillAction(billAction);
System.out.println("allByBillAction = " + allByBillAction.isEmpty());

memberActionService.deleteMember(event.getToken(), "쿠키");

Expand Down Expand Up @@ -220,9 +222,9 @@ void deleteMemberAction1() {
);
BillAction billAction = new BillAction(new Action(event, 6L), "뽕족", 100_000L);
billActionRepository.save(billAction);
BillActionDetail billActionDetail1 = new BillActionDetail(billAction, "쿠키", 40_000L);
BillActionDetail billActionDetail2 = new BillActionDetail(billAction, "웨디", 30_000L);
BillActionDetail billActionDetail3 = new BillActionDetail(billAction, "감자", 30_000L);
BillActionDetail billActionDetail1 = new BillActionDetail(billAction, "쿠키", 40_000L, true);
BillActionDetail billActionDetail2 = new BillActionDetail(billAction, "웨디", 30_000L, false);
BillActionDetail billActionDetail3 = new BillActionDetail(billAction, "감자", 30_000L, false);
billActionDetailRepository.saveAll(List.of(billActionDetail1, billActionDetail2, billActionDetail3));

memberActionService.deleteMemberAction(event.getToken(), targetAction.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected Object initController() {
@Test
void findBillActionDetailsTest() throws Exception {
BillActionDetailsAppResponse appResponse = new BillActionDetailsAppResponse(
List.of(new BillActionDetailAppResponse("토다리", 1000L)));
List.of(new BillActionDetailAppResponse("토다리", 1000L, false)));
given(billActionDetailService.findBillActionDetails(anyString(), anyLong()))
.willReturn(appResponse);

Expand All @@ -58,6 +58,7 @@ void findBillActionDetailsTest() throws Exception {
.andExpect(jsonPath("$.members").isArray())
.andExpect(jsonPath("$.members[0].name").value("토다리"))
.andExpect(jsonPath("$.members[0].price").value(1000L))
.andExpect(jsonPath("$.members[0].isFixed").value(false))
.andDo(
document("findBillActionDetailsTest",
preprocessRequest(prettyPrint()),
Expand All @@ -73,7 +74,9 @@ void findBillActionDetailsTest() throws Exception {
fieldWithPath("members[0].name").type(JsonFieldType.STRING)
.description("참여자 이름"),
fieldWithPath("members[0].price").type(JsonFieldType.NUMBER)
.description("참여자 정산 금액")
.description("참여자 정산 금액"),
fieldWithPath("members[0].isFixed").type(JsonFieldType.BOOLEAN)
.description("참여자 정산 금액 수정 여부")
)
)
);
Expand All @@ -83,8 +86,8 @@ void findBillActionDetailsTest() throws Exception {
@Test
void updateBillActionDetailsTest() throws Exception {
List<BillActionDetailUpdateRequest> billActionDetailUpdateRequests = List.of(
new BillActionDetailUpdateRequest("소하", 10000L),
new BillActionDetailUpdateRequest("웨디", 20000L)
new BillActionDetailUpdateRequest("소하", 10000L, true),
new BillActionDetailUpdateRequest("웨디", 20000L, true)
);
BillActionDetailsUpdateRequest request = new BillActionDetailsUpdateRequest(
billActionDetailUpdateRequests);
Expand Down Expand Up @@ -114,7 +117,9 @@ void updateBillActionDetailsTest() throws Exception {
fieldWithPath("members[0].name").type(JsonFieldType.STRING)
.description("참여자 이름"),
fieldWithPath("members[0].price").type(JsonFieldType.NUMBER)
.description("참여자 정산 금액")
.description("참여자 정산 금액"),
fieldWithPath("members[0].isFixed").type(JsonFieldType.BOOLEAN)
.description("참여자 정산 금액 수정 여부")
)
)
);
Expand Down
Loading
Loading