Skip to content

Commit

Permalink
refactor: action 도메인 삭제 (#542)
Browse files Browse the repository at this point in the history
* refactor: action 도메인 삭제

Co-authored-by: 3juhwan <[email protected]>
Co-authored-by: Arachneee <[email protected]>
Co-authored-by: kunsanglee <[email protected]>

* refactor: 엔티티 not null, unique 설정

Co-authored-by: 3juhwan <[email protected]>
Co-authored-by: Arachneee <[email protected]>
Co-authored-by: kunsanglee <[email protected]>

---------

Co-authored-by: 3juhwan <[email protected]>
Co-authored-by: Arachneee <[email protected]>
Co-authored-by: kunsanglee <[email protected]>
  • Loading branch information
4 people authored Sep 9, 2024
1 parent f75a7c9 commit 8cd6fad
Show file tree
Hide file tree
Showing 38 changed files with 482 additions and 601 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public class BillActionDetailService {

private final BillActionRepository billActionRepository;

public BillActionDetailsAppResponse findBillActionDetails(String token, Long actionId) {
BillAction billAction = billActionRepository.findByAction_Id(actionId)
public BillActionDetailsAppResponse findBillActionDetails(String token, Long billActionId) {
BillAction billAction = billActionRepository.findById(billActionId)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.BILL_ACTION_NOT_FOUND));
validateToken(token, billAction);

Expand All @@ -31,8 +31,8 @@ public BillActionDetailsAppResponse findBillActionDetails(String token, Long act
}

@Transactional
public void updateBillActionDetails(String token, Long actionId, BillActionDetailsUpdateAppRequest request) {
BillAction billAction = billActionRepository.findByAction_Id(actionId)
public void updateBillActionDetails(String token, Long billActionId, BillActionDetailsUpdateAppRequest request) {
BillAction billAction = billActionRepository.findById(billActionId)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.BILL_ACTION_NOT_FOUND));

List<BillActionDetailUpdateAppRequest> billActionDetailUpdateAppRequests = request.billActionDetailUpdateAppRequests();
Expand Down Expand Up @@ -60,8 +60,10 @@ private void validateToken(String token, BillAction billAction) {
}
}

private void validateTotalPrice(List<BillActionDetailUpdateAppRequest> billActionDetailUpdateAppRequests,
BillAction billAction) {
private void validateTotalPrice(
List<BillActionDetailUpdateAppRequest> billActionDetailUpdateAppRequests,
BillAction billAction
) {
Long requestsPriceSum = calculateUpdatePriceSum(billActionDetailUpdateAppRequests);
if (!billAction.isSamePrice(requestsPriceSum)) {
throw new HaengdongException(HaengdongErrorCode.BILL_ACTION_PRICE_NOT_MATCHED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
import org.springframework.transaction.annotation.Transactional;
import server.haengdong.application.request.BillActionAppRequest;
import server.haengdong.application.request.BillActionUpdateAppRequest;
import server.haengdong.domain.action.Action;
import server.haengdong.domain.action.ActionRepository;
import server.haengdong.domain.action.BillAction;
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.action.Sequence;
import server.haengdong.domain.event.Event;
import server.haengdong.domain.event.EventRepository;
import server.haengdong.exception.HaengdongErrorCode;
Expand All @@ -25,33 +24,35 @@ public class BillActionService {

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

@Transactional
public void saveAllBillAction(String eventToken, List<BillActionAppRequest> requests) {
Event event = getEvent(eventToken);
Action action = createStartAction(event);
Sequence sequence = createStartSequence(event);
List<MemberAction> findMemberActions = memberActionRepository.findAllByEvent(event);
CurrentMembers currentMembers = CurrentMembers.of(findMemberActions);

for (BillActionAppRequest request : requests) {
BillAction billAction = request.toBillAction(action, currentMembers);
BillAction billAction = request.toBillAction(event, sequence, currentMembers);
billActionRepository.save(billAction);
action = action.next();
sequence = sequence.next();
}
}

private Action createStartAction(Event event) {
return actionRepository.findLastByEvent(event)
.map(Action::next)
.orElse(Action.createFirst(event));
private Sequence createStartSequence(Event event) {
Sequence memberActionSequence = memberActionRepository.findLastByEvent(event)
.map(MemberAction::getSequence)
.orElseGet(Sequence::createFirst);
Sequence billActionSequence = billActionRepository.findLastByEvent(event)
.map(BillAction::getSequence)
.orElseGet(Sequence::createFirst);
return Sequence.getGreater(memberActionSequence, billActionSequence).next();
}

@Transactional
public void updateBillAction(String token, Long actionId, BillActionUpdateAppRequest request) {
BillAction billAction = billActionRepository.findByAction_Id(actionId)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.BILL_ACTION_NOT_FOUND));
public void updateBillAction(String token, Long billActionId, BillActionUpdateAppRequest request) {
BillAction billAction = getBillAction(billActionId);

validateToken(token, billAction);

Expand All @@ -66,15 +67,19 @@ private void validateToken(String token, BillAction billAction) {
}

@Transactional
public void deleteBillAction(String token, Long actionId) {
Event event = eventRepository.findByToken(token)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.EVENT_NOT_FOUND));

billActionRepository.deleteByAction_EventAndActionId(event, actionId);
public void deleteBillAction(String token, Long billActionId) {
BillAction billAction = getBillAction(billActionId);
validateToken(token, billAction);
billActionRepository.deleteById(billActionId);
}

private Event getEvent(String eventToken) {
return eventRepository.findByToken(eventToken)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.EVENT_NOT_FOUND));
}

private BillAction getBillAction(Long billActionId) {
return billActionRepository.findById(billActionId)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.BILL_ACTION_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
import server.haengdong.application.response.ActionAppResponse;
import server.haengdong.application.response.EventAppResponse;
import server.haengdong.application.response.EventDetailAppResponse;
import server.haengdong.application.response.MemberBillReportAppResponse;
import server.haengdong.application.response.MembersAppResponse;
import server.haengdong.domain.action.BillAction;
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.MemberBillReport;
import server.haengdong.domain.event.Event;
import server.haengdong.domain.event.EventRepository;
import server.haengdong.domain.event.EventTokenProvider;
Expand Down Expand Up @@ -57,7 +59,7 @@ public EventDetailAppResponse findEvent(String token) {
public List<ActionAppResponse> findActions(String token) {
Event event = getEvent(token);

List<BillAction> billActions = billActionRepository.findByAction_Event(event).stream()
List<BillAction> billActions = billActionRepository.findByEvent(event).stream()
.sorted(Comparator.comparing(BillAction::getSequence)).toList();
List<MemberAction> memberActions = memberActionRepository.findAllByEvent(event).stream()
.sorted(Comparator.comparing(MemberAction::getSequence)).toList();
Expand All @@ -76,7 +78,7 @@ private List<ActionAppResponse> getActionAppResponses(
while (billActionIndex < billActions.size() && memberActionIndex < memberActions.size()) {
BillAction billAction = billActions.get(billActionIndex);
MemberAction memberAction = memberActions.get(memberActionIndex);
if (billAction.getSequence() < memberAction.getSequence()) {
if (billAction.getSequence().getValue() < memberAction.getSequence().getValue()) {
actionAppResponses.add(ActionAppResponse.of(billAction));
billActionIndex++;
} else {
Expand Down Expand Up @@ -126,7 +128,7 @@ private void validateBeforeNames(List<MemberNameUpdateAppRequest> members, Event
}

private void validateBeforeMemberNameExist(Event event, String beforeName) {
boolean isMemberNameExist = memberActionRepository.existsByAction_EventAndMemberName(event, beforeName);
boolean isMemberNameExist = memberActionRepository.existsByEventAndMemberName(event, beforeName);
if (!isMemberNameExist) {
throw new HaengdongException(HaengdongErrorCode.MEMBER_NOT_EXIST);
}
Expand All @@ -143,16 +145,16 @@ private void validateAfterNames(List<MemberNameUpdateAppRequest> members, Event
}

private void validateAfterMemberNameNotExist(Event event, String afterName) {
boolean isMemberNameExist = memberActionRepository.existsByAction_EventAndMemberName(event, afterName);
boolean isMemberNameExist = memberActionRepository.existsByEventAndMemberName(event, afterName);
if (isMemberNameExist) {
throw new HaengdongException(HaengdongErrorCode.MEMBER_NAME_DUPLICATE);
}
}

private void updateMemberName(Event event, String beforeName, String afterName) {
memberActionRepository.findAllByAction_EventAndMemberName(event, beforeName)
memberActionRepository.findAllByEventAndMemberName(event, beforeName)
.forEach(memberAction -> memberAction.updateMemberName(afterName));
billActionDetailRepository.findAllByBillAction_Action_EventAndMemberName(event, beforeName)
billActionDetailRepository.findAllByBillAction_EventAndMemberName(event, beforeName)
.forEach(billActionDetail -> billActionDetail.updateMemberName(afterName));
}

Expand All @@ -167,4 +169,17 @@ private Event getEvent(String token) {
return eventRepository.findByToken(token)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.EVENT_NOT_FOUND));
}

public List<MemberBillReportAppResponse> getMemberBillReports(String token) {
Event event = eventRepository.findByToken(token)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.EVENT_NOT_FOUND));
List<BillAction> billActions = billActionRepository.findByEvent(event);
List<MemberAction> memberActions = memberActionRepository.findAllByEvent(event);

MemberBillReport memberBillReport = MemberBillReport.createByActions(billActions, memberActions);

return memberBillReport.getReports().entrySet().stream()
.map(entry -> new MemberBillReportAppResponse(entry.getKey(), entry.getValue()))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,33 @@
import org.springframework.stereotype.Component;
import server.haengdong.application.request.MemberActionSaveAppRequest;
import server.haengdong.application.request.MemberActionsSaveAppRequest;
import server.haengdong.domain.action.Action;
import server.haengdong.domain.action.CurrentMembers;
import server.haengdong.domain.action.MemberAction;
import server.haengdong.domain.action.MemberActionStatus;
import server.haengdong.domain.action.MemberGroupIdProvider;
import server.haengdong.domain.action.Sequence;
import server.haengdong.domain.event.Event;
import server.haengdong.exception.HaengdongErrorCode;
import server.haengdong.exception.HaengdongException;

@RequiredArgsConstructor
@Component
public class MemberActionFactory {

private final MemberGroupIdProvider memberGroupIdProvider;

public List<MemberAction> createMemberActions(
Event event,
MemberActionsSaveAppRequest request,
CurrentMembers currentMembers,
Action action
Sequence sequence
) {
validateMemberNames(request);
validateActions(request, currentMembers);

Long memberGroupId = memberGroupIdProvider.createGroupId();
List<MemberAction> createdMemberActions = new ArrayList<>();
List<MemberActionSaveAppRequest> actions = request.actions();
for (MemberActionSaveAppRequest appRequest : actions) {
MemberAction memberAction = appRequest.toMemberAction(action, memberGroupId);
MemberAction memberAction = appRequest.toMemberAction(event, sequence);
createdMemberActions.add(memberAction);
action = action.next();
sequence = sequence.next();
}

return createdMemberActions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
import org.springframework.transaction.annotation.Transactional;
import server.haengdong.application.request.MemberActionsSaveAppRequest;
import server.haengdong.application.response.CurrentMemberAppResponse;
import server.haengdong.domain.action.Action;
import server.haengdong.domain.action.ActionRepository;
import server.haengdong.domain.action.BillAction;
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.action.Sequence;
import server.haengdong.domain.event.Event;
import server.haengdong.domain.event.EventRepository;
import server.haengdong.exception.HaengdongErrorCode;
Expand All @@ -26,7 +25,6 @@ public class MemberActionService {
private final MemberActionFactory memberActionFactory;
private final MemberActionRepository memberActionRepository;
private final EventRepository eventRepository;
private final ActionRepository actionRepository;
private final BillActionRepository billActionRepository;

@Transactional
Expand All @@ -35,15 +33,20 @@ public void saveMemberAction(String token, MemberActionsSaveAppRequest request)

List<MemberAction> findMemberActions = memberActionRepository.findAllByEvent(event);
CurrentMembers currentMembers = CurrentMembers.of(findMemberActions);
Action action = createStartAction(event);
List<MemberAction> memberActions = memberActionFactory.createMemberActions(request, currentMembers, action);
Sequence sequence = createStartSequence(event);
List<MemberAction> memberActions = memberActionFactory.createMemberActions(event, request, currentMembers,
sequence);
memberActionRepository.saveAll(memberActions);
}

private Action createStartAction(Event event) {
return actionRepository.findLastByEvent(event)
.map(Action::next)
.orElse(Action.createFirst(event));
private Sequence createStartSequence(Event event) {
Sequence memberActionSequence = memberActionRepository.findLastByEvent(event)
.map(MemberAction::getSequence)
.orElseGet(Sequence::createFirst);
Sequence billActionSequence = billActionRepository.findLastByEvent(event)
.map(BillAction::getSequence)
.orElseGet(Sequence::createFirst);
return Sequence.getGreater(memberActionSequence, billActionSequence).next();
}

public List<CurrentMemberAppResponse> getCurrentMembers(String token) {
Expand All @@ -69,30 +72,27 @@ public void deleteMember(String token, String memberName) {

memberActionRepository.deleteAllByEventAndMemberName(event, memberName);

List<BillAction> billActions = billActionRepository.findByAction_Event(event);
List<BillAction> billActions = billActionRepository.findByEvent(event);
billActions.forEach(billAction -> resetBillAction(event, billAction));
}

@Transactional
public void deleteMemberAction(String token, Long actionId) {
public void deleteMemberAction(String token, Long id) {
Event event = eventRepository.findByToken(token)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.EVENT_NOT_FOUND));
Action action = actionRepository.findByIdAndEvent(actionId, event)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.ACTION_NOT_FOUND));
MemberAction memberAction = memberActionRepository.findByAction(action)
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.MEMBER_ACTION_NOT_FOUND));
MemberAction memberAction = memberActionRepository.findByIdAndEvent(id, event);

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

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

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

billAction.resetBillActionDetails(currentMembers);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package server.haengdong.application.request;

import server.haengdong.domain.action.Action;
import server.haengdong.domain.action.BillAction;
import server.haengdong.domain.action.CurrentMembers;
import server.haengdong.domain.action.Sequence;
import server.haengdong.domain.event.Event;

public record BillActionAppRequest(
String title,
Long price
) {

public BillAction toBillAction(Action action, CurrentMembers currentMembers) {
return BillAction.create(action, title, price, currentMembers);
public BillAction toBillAction(Event event, Sequence sequence, CurrentMembers currentMembers) {
return BillAction.create(event, sequence, title, price, currentMembers);
}
}
Loading

0 comments on commit 8cd6fad

Please sign in to comment.