Skip to content

Commit

Permalink
refactor: BillActionDetail isFixed ์ถ”๊ฐ€
Browse files Browse the repository at this point in the history
  • Loading branch information
kunsanglee committed Aug 20, 2024
1 parent 07f4bf3 commit 830eaa6
Show file tree
Hide file tree
Showing 17 changed files with 121 additions and 72 deletions.
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

0 comments on commit 830eaa6

Please sign in to comment.