Skip to content

Commit

Permalink
feat: 요구사항 변경에 따른 지출 내역 추가, 지출 액션 삭제 API 수정 (#373)
Browse files Browse the repository at this point in the history
* feat: 지출 내역 추가 시, 상세 내역 생성 로직 추가

* feat: 지출 내역 삭제 시, 상세 내역 삭제 로직 추가

* fix: 멤버가 없는 상황에 대해 0으로 나누는 상황 방지
  • Loading branch information
3Juhwan authored Aug 19, 2024
1 parent 45f147d commit 55fb094
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
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 @@ -23,19 +26,25 @@
public class BillActionService {

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

@Transactional
public void saveAllBillAction(String eventToken, List<BillActionAppRequest> requests) {
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);
billActionRepository.save(billAction);
action = action.next();
if (currentMembers.isNotEmpty()) {
saveBillActionDetails(billAction, currentMembers);
}
}
}

Expand All @@ -45,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)
Expand Down Expand Up @@ -86,6 +102,7 @@ public void deleteBillAction(String token, Long actionId) {
Event event = eventRepository.findByToken(token)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.EVENT_NOT_FOUND));

billActionDetailRepository.deleteByBillAction_Action_EventAndBillAction_ActionId(event, actionId);
billActionRepository.deleteByAction_EventAndActionId(event, actionId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,17 @@ public void deleteMemberAction(String token, Long actionId) {
MemberAction memberAction = memberActionRepository.findByAction(action)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.MEMBER_ACTION_NOT_FOUND));

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

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

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

billActionDetailRepository.deleteAllByBillAction(billAction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import server.haengdong.domain.event.Event;

@Repository
public interface BillActionDetailRepository extends JpaRepository<BillActionDetail, Long> {

List<BillActionDetail> findByBillAction(BillAction billAction);

void deleteAllByBillAction(BillAction billAction);

void deleteByBillAction_Action_EventAndBillAction_ActionId(Event event, Long actionId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public boolean isEmpty() {
}

public boolean isNotEmpty() {
return !isEmpty();
return !members.isEmpty();
}

public int size() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public record ActionsResponse(
) {
public static ActionsResponse of(List<ActionAppResponse> actions) {
return new ActionsResponse(actions.stream()
.map(ActionResponse2::of)
.toList());
.map(ActionResponse2::of)
.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
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 @@ -35,11 +38,19 @@ class BillActionServiceTest extends ServiceTestSupport {
@Autowired
private BillActionDetailRepository billActionDetailRepository;

@Autowired
private MemberActionRepository memberActionRepository;

@DisplayName("지출 내역을 생성한다.")
@Test
void saveAllBillAction() {
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> requests = List.of(
new BillActionAppRequest("뽕족", 10_000L),
Expand All @@ -52,8 +63,39 @@ void saveAllBillAction() {

assertThat(actions).extracting(BillAction::getTitle, BillAction::getPrice, BillAction::getSequence)
.containsExactlyInAnyOrder(
tuple("뽕족", 10_000L, 1L),
tuple("인생맥주", 15_000L, 2L)
tuple("뽕족", 10_000L, 3L),
tuple("인생맥주", 15_000L, 4L)
);
}

@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)
);
}

Expand Down Expand Up @@ -125,7 +167,8 @@ void updateBillAction2() {
BillActionDetail billActionDetail3 = new BillActionDetail(savedBillAction, "당근", 3000L);
BillActionDetail billActionDetail4 = new BillActionDetail(savedBillAction, "양파", 2000L);

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

Long actionId = savedBillAction.getAction().getId();
BillActionUpdateAppRequest request = new BillActionUpdateAppRequest("인생맥주", 20_000L);
Expand Down Expand Up @@ -159,6 +202,26 @@ void deleteBillAction() {
assertThat(billActionRepository.findById(billAction.getId())).isEmpty();
}

@DisplayName("지출 내역을 삭제하면 지출 상세도 삭제된다.")
@Test
void deleteBillActionTest1() {
Event event = Fixture.EVENT1;
eventRepository.save(event);
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);
memberActionRepository.saveAll(List.of(memberAction1, memberAction2));
billActionRepository.save(billAction);
billActionDetailRepository.saveAll(List.of(billActionDetail1, billActionDetail2));
Long actionId = billAction.getAction().getId();

billActionService.deleteBillAction(event.getToken(), actionId);

assertThat(billActionDetailRepository.findAll()).isEmpty();
}

@DisplayName("지출 내역 삭제 시 행사가 존재하지 않으면 예외가 발생한다.")
@Test
void deleteBillAction1() {
Expand Down
Loading

0 comments on commit 55fb094

Please sign in to comment.