Skip to content

Commit

Permalink
Merge: 충돌나는 코드 머지
Browse files Browse the repository at this point in the history
  • Loading branch information
3Juhwan committed Aug 16, 2024
2 parents 8b218a5 + 45f147d commit 756757d
Show file tree
Hide file tree
Showing 12 changed files with 359 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,23 @@ public void updateBillAction(String token, Long actionId, BillActionUpdateAppReq

validateToken(token, billAction);

resetBillActionDetail(billAction, request.price());

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

private void resetBillActionDetail(BillAction billAction, Long updatePrice) {
if (billAction.getPrice() != updatePrice) {
List<BillActionDetail> billActionDetails = billActionDetailRepository.findByBillAction(billAction);
int memberCount = billActionDetails.size();
if (memberCount != 0) {
Long eachPrice = updatePrice / memberCount;
billActionDetails.forEach(billActionDetail -> billActionDetail.updatePrice(eachPrice));
}
}
}

private void validateToken(String token, BillAction billAction) {
Event event = billAction.getEvent();
if (event.isTokenMismatch(token)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
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.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;
Expand All @@ -25,6 +29,8 @@ public class MemberActionService {
private final MemberActionRepository memberActionRepository;
private final EventRepository eventRepository;
private final ActionRepository actionRepository;
private final BillActionDetailRepository billActionDetailRepository;
private final BillActionRepository billActionRepository;

@Transactional
public void saveMemberAction(String token, MemberActionsSaveAppRequest request) {
Expand Down Expand Up @@ -65,6 +71,9 @@ public void deleteMember(String token, String memberName) {
.orElseThrow(() -> new HaengdongException(HaengdongErrorCode.EVENT_NOT_FOUND));

memberActionRepository.deleteAllByEventAndMemberName(event, memberName);

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

@Transactional
Expand All @@ -77,6 +86,26 @@ 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());
billActions.forEach(billAction -> resetBillAction(event, billAction));
}

private void resetBillAction(Event event, BillAction billAction) {
List<MemberAction> memberActions = memberActionRepository.findByEventAndSequence(event,
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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public BillActionDetail(BillAction billAction, String memberName, Long price) {
this.memberName = memberName;
this.price = price;
}

public void updatePrice(Long price) {
this.price = price;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package server.haengdong.domain.action;

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 @@ -4,6 +4,7 @@
import java.util.Optional;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import server.haengdong.domain.event.Event;

Expand All @@ -16,4 +17,11 @@ public interface BillActionRepository extends JpaRepository<BillAction, Long> {
void deleteByAction_EventAndActionId(Event event, Long actionId);

Optional<BillAction> findByAction_Id(Long actionId);

@Query("""
select ba
from BillAction ba
where ba.action.event = :event and ba.action.sequence > :sequence
""")
List<BillAction> findByEventAndGreaterThanSequence(Event event, Long sequence);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,11 @@ public interface MemberActionRepository extends JpaRepository<MemberAction, Long
List<MemberAction> findAllByAction_EventAndMemberName(Event event, String memberName);

boolean existsByAction_EventAndMemberName(Event event, String updatedMemberName);

@Query("""
select ma
from MemberAction ma
where ma.action.event = :event and ma.action.sequence < :sequence
""")
List<MemberAction> findByEventAndSequence(Event event, Long sequence);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server.haengdong.presentation;

import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
Expand All @@ -15,10 +16,12 @@
import org.springframework.web.bind.annotation.RestController;
import server.haengdong.application.AuthService;
import server.haengdong.application.EventService;
import server.haengdong.application.response.ActionAppResponse;
import server.haengdong.infrastructure.auth.CookieProperties;
import server.haengdong.presentation.request.EventLoginRequest;
import server.haengdong.presentation.request.EventSaveRequest;
import server.haengdong.presentation.request.MemberNamesUpdateRequest;
import server.haengdong.presentation.response.ActionsResponse;
import server.haengdong.presentation.response.EventDetailResponse;
import server.haengdong.presentation.response.EventResponse;
import server.haengdong.presentation.response.MembersResponse;
Expand Down Expand Up @@ -60,6 +63,14 @@ public ResponseEntity<StepsResponse> findActions(@PathVariable("eventId") String
return ResponseEntity.ok(stepsResponse);
}

@GetMapping("/api/events/{eventId}/actions/v2")
public ResponseEntity<ActionsResponse> findActions2(@PathVariable("eventId") String token) {
List<ActionAppResponse> actions = eventService.findActions(token);
ActionsResponse actionsResponse = ActionsResponse.of(actions);

return ResponseEntity.ok(actionsResponse);
}

@GetMapping("/api/events/{eventId}/members")
public ResponseEntity<MembersResponse> findAllMembers(@PathVariable("eventId") String token) {
MembersResponse response = MembersResponse.of(eventService.findAllMembers(token));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package server.haengdong.presentation.response;

import server.haengdong.application.response.ActionAppResponse;

public record ActionResponse2(
Long actionId,
String name,
Long price,
Long sequence,
String type
) {

public static ActionResponse2 of(ActionAppResponse actionAppResponse) {
return new ActionResponse2(
actionAppResponse.actionId(),
actionAppResponse.name(),
actionAppResponse.price(),
actionAppResponse.sequence(),
actionAppResponse.actionType().name()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package server.haengdong.presentation.response;

import java.util.List;
import server.haengdong.application.response.ActionAppResponse;

public record ActionsResponse(
List<ActionResponse2> actions
) {
public static ActionsResponse of(List<ActionAppResponse> actions) {
return new ActionsResponse(actions.stream()
.map(ActionResponse2::of)
.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,40 @@ void updateBillAction1() {
.isInstanceOf(HaengdongException.class);
}

@DisplayName("지출 내역 금액을 변경하면 지출 디테일이 초기화 된다.")
@Test
void updateBillAction2() {
Event event = Fixture.EVENT1;
Event savedEvent = eventRepository.save(event);
Action action = Action.createFirst(savedEvent);
BillAction billAction = new BillAction(action, "뽕족", 10_000L);
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);

billActionService.updateBillAction(event.getToken(), actionId, request);

BillAction updatedBillAction = billActionRepository.findById(savedBillAction.getId()).get();
List<BillActionDetail> billActionDetails = billActionDetailRepository.findByBillAction(updatedBillAction);

assertThat(billActionDetails).hasSize(4)
.extracting("memberName", "price")
.containsExactlyInAnyOrder(
tuple("감자", 5000L),
tuple("고구마", 5000L),
tuple("당근", 5000L),
tuple("양파", 5000L)
);
}

@DisplayName("지출 내역을 삭제한다.")
@Test
void deleteBillAction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
import server.haengdong.application.request.MemberActionSaveAppRequest;
import server.haengdong.application.request.MemberActionsSaveAppRequest;
import server.haengdong.domain.action.Action;
import server.haengdong.domain.action.BillAction;
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;
Expand All @@ -33,6 +37,12 @@ class MemberActionServiceTest extends ServiceTestSupport {
@Autowired
private EventRepository eventRepository;

@Autowired
private BillActionRepository billActionRepository;

@Autowired
private BillActionDetailRepository billActionDetailRepository;

@DisplayName("현재 행사에 참여하고 있는 경우에 나갈 수 있다.")
@Test
void saveMemberActionTest() {
Expand Down Expand Up @@ -115,6 +125,44 @@ void deleteMember() {
);
}

@DisplayName("이벤트에 속한 멤버을 삭제하면 전체 지출 내역 디테일이 초기화된다.")
@Test
void deleteMember1() {
Event event = Fixture.EVENT1;
eventRepository.save(event);
MemberAction memberAction1 = createMemberAction(new Action(event, 1L), "토다리", IN, 1L);
Action targetAction = new Action(event, 2L);
MemberAction memberAction2 = createMemberAction(targetAction, "토다리", OUT, 2L);
MemberAction memberAction3 = createMemberAction(new Action(event, 3L), "쿠키", IN, 3L);
MemberAction memberAction4 = createMemberAction(new Action(event, 4L), "웨디", IN, 4L);
MemberAction memberAction5 = createMemberAction(new Action(event, 5L), "감자", IN, 5L);
memberActionRepository.saveAll(
List.of(memberAction1,
memberAction2,
memberAction3,
memberAction4,
memberAction5
)
);
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);
billActionDetailRepository.saveAll(List.of(billActionDetail1, billActionDetail2, billActionDetail3));

memberActionService.deleteMember(event.getToken(), "쿠키");

List<BillActionDetail> billActionDetails = billActionDetailRepository.findByBillAction(billAction);

assertThat(billActionDetails).hasSize(2)
.extracting("memberName", "price")
.containsExactlyInAnyOrder(
tuple("웨디", 50_000L),
tuple("감자", 50_000L)
);
}

@DisplayName("이벤트에 속한 멤버 액션을 삭제하면 이후에 기록된 해당 참여자의 모든 멤버 액션을 삭제한다.")
@Test
void deleteMemberAction() {
Expand Down Expand Up @@ -151,6 +199,45 @@ void deleteMemberAction() {
);
}

@DisplayName("이벤트에 속한 멤버 액션을 삭제하면 이후 지출 내역 디테일이 초기화된다.")
@Test
void deleteMemberAction1() {
Event event = Fixture.EVENT1;
eventRepository.save(event);
MemberAction memberAction1 = createMemberAction(new Action(event, 1L), "토다리", IN, 1L);
Action targetAction = new Action(event, 2L);
MemberAction memberAction2 = createMemberAction(targetAction, "토다리", OUT, 2L);
MemberAction memberAction3 = createMemberAction(new Action(event, 3L), "쿠키", IN, 3L);
MemberAction memberAction4 = createMemberAction(new Action(event, 4L), "웨디", IN, 4L);
MemberAction memberAction5 = createMemberAction(new Action(event, 5L), "감자", IN, 5L);
memberActionRepository.saveAll(
List.of(memberAction1,
memberAction2,
memberAction3,
memberAction4,
memberAction5
)
);
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);
billActionDetailRepository.saveAll(List.of(billActionDetail1, billActionDetail2, billActionDetail3));

memberActionService.deleteMemberAction(event.getToken(), targetAction.getId());
List<BillActionDetail> billActionDetails = billActionDetailRepository.findByBillAction(billAction);

assertThat(billActionDetails).hasSize(4)
.extracting("memberName", "price")
.containsExactlyInAnyOrder(
tuple("토다리", 25_000L),
tuple("쿠키", 25_000L),
tuple("웨디", 25_000L),
tuple("감자", 25_000L)
);
}

private MemberAction createMemberAction(
Action action,
String memberName,
Expand Down
Loading

0 comments on commit 756757d

Please sign in to comment.