diff --git a/server/src/main/java/server/haengdong/application/BillActionService.java b/server/src/main/java/server/haengdong/application/BillActionService.java index 6c69e383f..0c62c133f 100644 --- a/server/src/main/java/server/haengdong/application/BillActionService.java +++ b/server/src/main/java/server/haengdong/application/BillActionService.java @@ -1,6 +1,5 @@ package server.haengdong.application; -import java.util.ArrayList; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -26,20 +25,18 @@ public class BillActionService { public void saveAllBillAction(String eventToken, List requests) { Event event = eventRepository.findByToken(eventToken) .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 이벤트 토큰입니다.")); - long lastSequence = getLastSequence(event); + Action action = createStartAction(event); - List billActions = new ArrayList<>(); for (BillActionAppRequest request : requests) { - Action action = new Action(event, ++lastSequence); BillAction billAction = request.toBillAction(action); - billActions.add(billAction); + billActionRepository.save(billAction); + action = action.next(); } - billActionRepository.saveAll(billActions); } - private long getLastSequence(Event event) { + private Action createStartAction(Event event) { return actionRepository.findLastByEvent(event) - .map(Action::getSequence) - .orElse(0L); + .map(Action::next) + .orElse(Action.createFirst(event)); } } diff --git a/server/src/main/java/server/haengdong/domain/Action.java b/server/src/main/java/server/haengdong/domain/Action.java index 7a47b1de5..be294dac3 100644 --- a/server/src/main/java/server/haengdong/domain/Action.java +++ b/server/src/main/java/server/haengdong/domain/Action.java @@ -15,6 +15,8 @@ @Entity public class Action { + private static final long FIRST_SEQUENCE = 1L; + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @@ -28,4 +30,12 @@ public Action(Event event, Long sequence) { this.event = event; this.sequence = sequence; } + + public static Action createFirst(Event event) { + return new Action(event, FIRST_SEQUENCE); + } + + public Action next() { + return new Action(event, sequence + 1); + } } diff --git a/server/src/test/java/server/haengdong/domain/ActionTest.java b/server/src/test/java/server/haengdong/domain/ActionTest.java new file mode 100644 index 000000000..a699fb04a --- /dev/null +++ b/server/src/test/java/server/haengdong/domain/ActionTest.java @@ -0,0 +1,29 @@ +package server.haengdong.domain; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class ActionTest { + + @DisplayName("액션의 초기 순서번호는 1이다.") + @Test + void createFirst() { + Event event = new Event("name", "token"); + Action action = Action.createFirst(event); + + assertThat(action.getSequence()).isOne(); + } + + @DisplayName("현재 액션의 다음 액션의 순서는 1만큼 증가한다.") + @Test + void next() { + Event event = new Event("name", "token"); + Action action = new Action(event, 2L); + + Action nextAction = action.next(); + + assertThat(nextAction.getSequence()).isEqualTo(3L); + } +}