Skip to content

Commit

Permalink
refactor: 액션 이력 조회 리펙토링 (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arachneee authored Aug 4, 2024
1 parent 0739181 commit d381d9d
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import server.haengdong.presentation.request.EventSaveRequest;
import server.haengdong.presentation.response.EventDetailResponse;
import server.haengdong.presentation.response.EventResponse;
import server.haengdong.presentation.response.StepResponse;
import server.haengdong.presentation.response.StepsResponse;

@RequiredArgsConstructor
@RestController
Expand All @@ -35,9 +35,9 @@ public ResponseEntity<EventDetailResponse> findEvent(@PathVariable("eventId") St
}

@GetMapping("/api/events/{eventId}/actions")
public ResponseEntity<StepResponse> findActions(@PathVariable("eventId") String token) {
StepResponse stepResponse = StepResponse.of(eventService.findActions(token));
public ResponseEntity<StepsResponse> findActions(@PathVariable("eventId") String token) {
StepsResponse stepsResponse = StepsResponse.of(eventService.findActions(token));

return ResponseEntity.ok(stepResponse);
return ResponseEntity.ok(stepsResponse);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,57 +1,25 @@
package server.haengdong.presentation.response;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import server.haengdong.application.response.ActionAppResponse;

public record StepResponse(
List<ActionsResponse> steps
String type,
List<String> members,
List<ActionResponse> actions
) {

public static StepResponse of(List<ActionAppResponse> actions) {
if (actions.isEmpty()) {
return new StepResponse(List.of());
}
List<ActionsResponse> actionsResponse = new ArrayList<>();
Set<String> members = new HashSet<>();
ActionAppResponse firstAction = getFirstAction(actions);
List<ActionAppResponse> group = new ArrayList<>();
group.add(firstAction);
String currentActionType = firstAction.actionTypeName();
members.add(firstAction.name());

for (int i = 1; i < actions.size(); i++) {
ActionAppResponse action = actions.get(i);
String typeName = action.actionTypeName();
if (currentActionType.equals(typeName)) {
if (typeName.equals("IN")) {
members.add(action.name());
}
if (typeName.equals("OUT")) {
members.remove(action.name());
}
group.add(action);
continue;
}
actionsResponse.add(ActionsResponse.of(group, members));
currentActionType = typeName;
group.clear();
if (typeName.equals("IN")) {
members.add(action.name());
}
if (typeName.equals("OUT")) {
members.remove(action.name());
}
group.add(action);
}
actionsResponse.add(ActionsResponse.of(group, members));

return new StepResponse(actionsResponse);
public static StepResponse of(List<String> members, List<ActionAppResponse> actions) {
return new StepResponse(
actions.get(0).actionTypeName(),
new ArrayList<>(members),
toActionsResponse(actions)
);
}

private static ActionAppResponse getFirstAction(List<ActionAppResponse> actions) {
return actions.get(0);
private static List<ActionResponse> toActionsResponse(List<ActionAppResponse> actions) {
return actions.stream()
.map(ActionResponse::of)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package server.haengdong.presentation.response;

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

public record StepsResponse(List<StepResponse> steps) {

public static StepsResponse of(List<ActionAppResponse> actions) {
List<StepResponse> steps = new ArrayList<>();
List<String> currentMembers = new ArrayList<>();
List<List<ActionAppResponse>> groups = createGroups(actions);

for (List<ActionAppResponse> group : groups) {
changeCurrentMembers(group, currentMembers);
StepResponse stepResponse = StepResponse.of(currentMembers, group);
steps.add(stepResponse);
}
return new StepsResponse(steps);
}

private static List<List<ActionAppResponse>> createGroups(List<ActionAppResponse> actions) {
List<List<ActionAppResponse>> groups = new ArrayList<>();

for (ActionAppResponse action : actions) {
if (groups.isEmpty() || isActionTypeChange(action, groups)) {
groups.add(new ArrayList<>());
}
groups.get(groups.size() - 1).add(action);
}

return groups;
}

private static boolean isActionTypeChange(ActionAppResponse action, List<List<ActionAppResponse>> groups) {
List<ActionAppResponse> currentGroup = groups.get(groups.size() - 1);
return currentGroup.get(0).actionType() != action.actionType();
}

private static void changeCurrentMembers(List<ActionAppResponse> group, List<String> currentMembers) {
for (ActionAppResponse action : group) {
if (action.actionType() == ActionType.IN) {
currentMembers.add(action.name());
continue;
}
if (action.actionType() == ActionType.OUT) {
currentMembers.remove(action.name());
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package server.haengdong.presentation.response;


import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import server.haengdong.application.response.ActionAppResponse;
import server.haengdong.application.response.ActionAppResponse.ActionType;

class StepsResponseTest {

@DisplayName("이웃한 같은 타입의 액션들을 그룹화 하여 응답객체를 생성한다.")
@Test
void of() {
List<ActionAppResponse> actions = List.of(
new ActionAppResponse(1L, "망쵸", null, 1L, ActionType.IN),
new ActionAppResponse(2L, "백호", null, 2L, ActionType.IN),
new ActionAppResponse(3L, "감자탕", 10_000L, 3L, ActionType.BILL),
new ActionAppResponse(4L, "인생네컷", 10_000L, 4L, ActionType.BILL),
new ActionAppResponse(5L, "소하", null, 5L, ActionType.IN),
new ActionAppResponse(6L, "웨디", null, 6L, ActionType.IN),
new ActionAppResponse(7L, "망쵸", null, 7L, ActionType.OUT),
new ActionAppResponse(8L, "백호", null, 8L, ActionType.OUT),
new ActionAppResponse(9L, "노래방", 20_000L, 9L, ActionType.BILL)
);

StepsResponse stepsResponse = StepsResponse.of(actions);

StepsResponse expected = new StepsResponse(
List.of(
new StepResponse("IN", List.of("망쵸", "백호"), List.of(
new ActionResponse(1L, "망쵸", null, 1L),
new ActionResponse(2L, "백호", null, 2L)
)),
new StepResponse("BILL", List.of("망쵸", "백호"), List.of(
new ActionResponse(3L, "감자탕", 10_000L, 3L),
new ActionResponse(4L, "인생네컷", 10_000L, 4L)
)),
new StepResponse("IN", List.of("망쵸", "백호", "소하", "웨디"), List.of(
new ActionResponse(5L, "소하", null, 5L),
new ActionResponse(6L, "웨디", null, 6L)
)),
new StepResponse("OUT", List.of("소하", "웨디"), List.of(
new ActionResponse(7L, "망쵸", null, 7L),
new ActionResponse(8L, "백호", null, 8L)
)),
new StepResponse("BILL", List.of("소하", "웨디"), List.of(
new ActionResponse(9L, "노래방", 20_000L, 9L)
))
)
);
assertThat(stepsResponse).isEqualTo(expected);
}

@DisplayName("액션이 없으면 빈 스탭들이 만들어진다.")
@Test
void ofEmpty() {
StepsResponse stepsResponse = StepsResponse.of(List.of());
assertThat(stepsResponse.steps()).isEmpty();
}
}

0 comments on commit d381d9d

Please sign in to comment.