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] 요구사항 변경에 따른 지출 내역 추가, 지출 액션 삭제 API 수정 #373

Merged
merged 4 commits into from
Aug 19, 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 @@ -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);
Comment on lines +59 to +61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
currentMembers.getMembers().stream()
.map(memberName -> new BillActionDetail(billAction, memberName, pricePerMember))
.forEach(billActionDetailRepository::save);
List<BillActionDetail> billActionDetails = currentMembers.getMembers().stream()
.map(memberName -> new BillActionDetail(billAction, memberName, pricePerMember))
.toList();
billActionDetailRepository.saveAll(billActionDetails);

r: 리스트로 추출한 다음에 saveAll 메서드를 사용하면 한 번의 쿼리로 전부 저장해서 더 좋을 것 같아요

}

@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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
billActionDetailRepository.deleteByBillAction_Action_EventAndBillAction_ActionId(event, actionId);
billActionDetailRepository.deleteAllByBillAction_Action_EventAndBillAction_ActionId(event, actionId);

r: BillActionDetail 여러 개 삭제하는 거니까 deleteAll을 사용하는 게 더 예측하기 쉬울 것 같아요.

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());
Comment on lines 10 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return new ActionsResponse(actions.stream()
.map(ActionResponse2::of)
.toList());
.map(ActionResponse2::of)
.toList());
return new ActionsResponse(actions.stream()
.map(ActionResponse2::of)
.toList())

또는

Suggested change
return new ActionsResponse(actions.stream()
.map(ActionResponse2::of)
.toList());
.map(ActionResponse2::of)
.toList());
return new ActionsResponse(
actions.stream()
.map(ActionResponse2::of)
.toList()
);

c: 인덴트가 뭔가 이상해요

}
}
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
Loading