Skip to content

Commit

Permalink
refactor: 지출 내역 추가 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Arachneee committed Jul 19, 2024
1 parent 9c22e47 commit 24ef6e4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package server.haengdong.application;

import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -26,20 +25,18 @@ public class BillActionService {
public void saveAllBillAction(String eventToken, List<BillActionAppRequest> requests) {
Event event = eventRepository.findByToken(eventToken)
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 이벤트 토큰입니다."));
long lastSequence = getLastSequence(event);
Action action = createStartAction(event);

List<BillAction> 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));
}
}
10 changes: 10 additions & 0 deletions server/src/main/java/server/haengdong/domain/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
@Entity
public class Action {

private static final long FIRST_SEQUENCE = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand All @@ -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);
}
}
29 changes: 29 additions & 0 deletions server/src/test/java/server/haengdong/domain/ActionTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 24ef6e4

Please sign in to comment.