Skip to content

Commit

Permalink
refactor: 참여자별 정산 현황 조회 및 액션 이력 조회 수정 (#377)
Browse files Browse the repository at this point in the history
* refactor: BillActionDetail 변경 사항을 반영하여 참여자별 정산 현황 조회하도록 수정

* refactor: 액션 이력 조회 시 지출 액션 고정 금액 설정 여부 필드 추가

* refactor: isFixed 필드 삭제

* refactor: 메서드 이름 변경

* fix: BillAction 변경 로직 수정
  • Loading branch information
khabh authored Aug 19, 2024
1 parent 55fb094 commit 64d9ac8
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import server.haengdong.application.response.MemberBillReportAppResponse;
import server.haengdong.domain.action.BillAction;
import server.haengdong.domain.action.BillActionRepository;
Expand All @@ -15,6 +16,7 @@
import server.haengdong.exception.HaengdongException;

@RequiredArgsConstructor
@Transactional(readOnly = true)
@Service
public class ActionService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public void updateBillAction(String token, Long actionId, BillActionUpdateAppReq

resetBillActionDetail(billAction, request.price());

BillAction updatedBillAction = billAction.update(request.title(), request.price());
billActionRepository.save(updatedBillAction);
billAction.update(request.title(), request.price());
}

private void resetBillActionDetail(BillAction billAction, Long updatePrice) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ public record ActionAppResponse(
String name,
Long price,
Long sequence,
boolean isFixed,
ActionType actionType
) {

public ActionAppResponse(Long actionId, String name, Long price, Long sequence, ActionType actionType) {
this(actionId, name, price, sequence, false, actionType);
}

public static ActionAppResponse of(BillAction billAction) {
return new ActionAppResponse(
billAction.getAction().getId(),
billAction.getTitle(),
billAction.getPrice(),
billAction.getSequence(),
ActionType.BILL
billAction.isFixed(),
ActionAppResponse.ActionType.BILL
);
}

Expand All @@ -30,7 +36,8 @@ public static ActionAppResponse of(MemberAction memberAction) {
memberAction.getMemberName(),
null,
memberAction.getSequence(),
ActionType.of(status)
false,
ActionAppResponse.ActionType.of(status)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -24,6 +27,7 @@ public class BillAction implements Comparable<BillAction> {
public static final int MAX_TITLE_LENGTH = 30;
public static final long MIN_PRICE = 1L;
public static final long MAX_PRICE = 10_000_000L;
private static final long DEFAULT_PRICE = 0L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -37,6 +41,9 @@ public class BillAction implements Comparable<BillAction> {

private Long price;

@OneToMany(mappedBy = "billAction", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private List<BillActionDetail> billActionDetails = new ArrayList<>();

public BillAction(Action action, String title, Long price) {
this(null, action, title, price);
}
Expand All @@ -63,8 +70,11 @@ private void validatePrice(Long price) {
}
}

public BillAction update(String title, Long price) {
return new BillAction(id, action, title, price);
public void update(String title, Long price) {
validateTitle(title);
validatePrice(price);
this.title = title;
this.price = price;
}

public Long getSequence() {
Expand All @@ -75,6 +85,30 @@ public Event getEvent() {
return action.getEvent();
}

public Long findPriceByMemberName(String memberName) {
return billActionDetails.stream()
.filter(billActionDetail -> billActionDetail.hasMemberName(memberName))
.map(BillActionDetail::getPrice)
.findFirst()
.orElse(DEFAULT_PRICE);
}

public boolean isFixed() {
return billActionDetails.stream()
.map(BillActionDetail::getPrice)
.distinct()
.count() != 1L;
}

public void addDetails(List<BillActionDetail> billActionDetails) {
billActionDetails.forEach(this::addDetail);
}

private void addDetail(BillActionDetail billActionDetail) {
this.billActionDetails.add(billActionDetail);
billActionDetail.setBillAction(this);
}

@Override
public int compareTo(BillAction o) {
return Long.compare(this.getSequence(), o.getSequence());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ public class BillActionDetail {

private Long price;

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

public void setBillAction(BillAction billAction) {
this.billAction = billAction;
}

public boolean hasMemberName(String memberName) {
return this.memberName.equals(memberName);
}

public BillActionDetail(BillAction billAction, String memberName, Long price) {
this.billAction = billAction;
this.memberName = memberName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ private static void addBillAction(
return;
}

Long pricePerMember = billAction.getPrice() / currentMembers.size();
for (String currentMember : currentMembers.getMembers()) {
Long price = memberBillReports.get(currentMember) + pricePerMember;
Long currentPrice = billAction.findPriceByMemberName(currentMember);
Long price = memberBillReports.get(currentMember) + currentPrice;
memberBillReports.put(currentMember, price);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,21 @@ public record ActionResponse(
Long actionId,
String name,
Long price,
Long sequence
Long sequence,
boolean isFixed
) {

public ActionResponse(Long actionId, String name, Long price, Long sequence) {
this(actionId, name, price, sequence, false);
}

public static ActionResponse of(ActionAppResponse actionAppResponse) {
return new ActionResponse(
actionAppResponse.actionId(),
actionAppResponse.name(),
actionAppResponse.price(),
actionAppResponse.sequence()
actionAppResponse.sequence(),
actionAppResponse.isFixed()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import server.haengdong.application.response.MemberBillReportAppResponse;
import server.haengdong.domain.action.Action;
import server.haengdong.domain.action.BillAction;
import server.haengdong.domain.action.BillActionDetail;
import server.haengdong.domain.action.BillActionRepository;
import server.haengdong.domain.action.MemberAction;
import server.haengdong.domain.action.MemberActionRepository;
Expand Down Expand Up @@ -49,9 +50,21 @@ void getMemberBillReports() {
);
List<BillAction> billActions = List.of(
new BillAction(new Action(savedEvent, 4L), "뽕족", 60_000L),
new BillAction(new Action(savedEvent, 6L), "인생맥주", 40_000L),
new BillAction(new Action(savedEvent, 7L), "인생네컷", 20_000L)
);
billActions.get(0).addDetails(
List.of(
new BillActionDetail("소하", 10_000L),
new BillActionDetail("감자", 40_000L),
new BillActionDetail("쿠키", 10_000L)
)
);
billActions.get(1).addDetails(
List.of(
new BillActionDetail("소하", 5_000L),
new BillActionDetail("쿠키", 15_000L)
)
);
memberActionRepository.saveAll(memberActions);
billActionRepository.saveAll(billActions);

Expand All @@ -61,9 +74,9 @@ void getMemberBillReports() {
.hasSize(3)
.extracting(MemberBillReportAppResponse::name, MemberBillReportAppResponse::price)
.containsExactlyInAnyOrder(
tuple("감자", 20_000L),
tuple("쿠키", 50_000L),
tuple("소하", 50_000L)
tuple("감자", 40_000L),
tuple("쿠키", 25_000L),
tuple("소하", 15_000L)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,12 @@ 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);
billAction.addDetails(List.of(billActionDetail1, billActionDetail2, billActionDetail3, billActionDetail4));
BillAction savedBillAction = billActionRepository.save(billAction);
BillActionDetail billActionDetail1 = new BillActionDetail(savedBillAction, "감자", 3000L);
BillActionDetail billActionDetail2 = new BillActionDetail(savedBillAction, "고구마", 2000L);
BillActionDetail billActionDetail3 = new BillActionDetail(savedBillAction, "당근", 3000L);
BillActionDetail billActionDetail4 = new BillActionDetail(savedBillAction, "양파", 2000L);

billActionDetailRepository.saveAll(
List.of(billActionDetail1, billActionDetail2, billActionDetail3, billActionDetail4));

Long actionId = savedBillAction.getAction().getId();
BillActionUpdateAppRequest request = new BillActionUpdateAppRequest("인생맥주", 20_000L);
Expand Down
Loading

0 comments on commit 64d9ac8

Please sign in to comment.