From d381d9dfad70ff374d806ae59e800b2bb5c07607 Mon Sep 17 00:00:00 2001 From: Arachne <66822642+Arachneee@users.noreply.github.com> Date: Sun, 4 Aug 2024 14:16:01 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EC=95=A1=EC=85=98=20=EC=9D=B4?= =?UTF-8?q?=EB=A0=A5=20=EC=A1=B0=ED=9A=8C=20=EB=A6=AC=ED=8E=99=ED=86=A0?= =?UTF-8?q?=EB=A7=81=20(#141)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/EventController.java | 8 +-- .../response/ActionsResponse.java | 28 --------- .../presentation/response/StepResponse.java | 58 ++++------------- .../presentation/response/StepsResponse.java | 52 +++++++++++++++ .../response/StepResponseTest.java | 57 ----------------- .../response/StepsResponseTest.java | 63 +++++++++++++++++++ 6 files changed, 132 insertions(+), 134 deletions(-) delete mode 100644 server/src/main/java/server/haengdong/presentation/response/ActionsResponse.java create mode 100644 server/src/main/java/server/haengdong/presentation/response/StepsResponse.java delete mode 100644 server/src/test/java/server/haengdong/presentation/response/StepResponseTest.java create mode 100644 server/src/test/java/server/haengdong/presentation/response/StepsResponseTest.java diff --git a/server/src/main/java/server/haengdong/presentation/EventController.java b/server/src/main/java/server/haengdong/presentation/EventController.java index 9fbe098ce..816e7ed26 100644 --- a/server/src/main/java/server/haengdong/presentation/EventController.java +++ b/server/src/main/java/server/haengdong/presentation/EventController.java @@ -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 @@ -35,9 +35,9 @@ public ResponseEntity findEvent(@PathVariable("eventId") St } @GetMapping("/api/events/{eventId}/actions") - public ResponseEntity findActions(@PathVariable("eventId") String token) { - StepResponse stepResponse = StepResponse.of(eventService.findActions(token)); + public ResponseEntity findActions(@PathVariable("eventId") String token) { + StepsResponse stepsResponse = StepsResponse.of(eventService.findActions(token)); - return ResponseEntity.ok(stepResponse); + return ResponseEntity.ok(stepsResponse); } } diff --git a/server/src/main/java/server/haengdong/presentation/response/ActionsResponse.java b/server/src/main/java/server/haengdong/presentation/response/ActionsResponse.java deleted file mode 100644 index 188c48dc9..000000000 --- a/server/src/main/java/server/haengdong/presentation/response/ActionsResponse.java +++ /dev/null @@ -1,28 +0,0 @@ -package server.haengdong.presentation.response; - -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import server.haengdong.application.response.ActionAppResponse; - -public record ActionsResponse( - String type, - String stepName, - Set members, - List actions -) { - - public static ActionsResponse of(List actions, Set members) { - List actionResponses = actions.stream() - .map(ActionResponse::of) - .toList(); - - String actionType = actions.get(0).actionTypeName(); - return new ActionsResponse( - actionType, - null, - new HashSet<>(members), - actionResponses - ); - } -} diff --git a/server/src/main/java/server/haengdong/presentation/response/StepResponse.java b/server/src/main/java/server/haengdong/presentation/response/StepResponse.java index a8f3877ef..c3e854f4e 100644 --- a/server/src/main/java/server/haengdong/presentation/response/StepResponse.java +++ b/server/src/main/java/server/haengdong/presentation/response/StepResponse.java @@ -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 steps + String type, + List members, + List actions ) { - - public static StepResponse of(List actions) { - if (actions.isEmpty()) { - return new StepResponse(List.of()); - } - List actionsResponse = new ArrayList<>(); - Set members = new HashSet<>(); - ActionAppResponse firstAction = getFirstAction(actions); - List 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 members, List actions) { + return new StepResponse( + actions.get(0).actionTypeName(), + new ArrayList<>(members), + toActionsResponse(actions) + ); } - private static ActionAppResponse getFirstAction(List actions) { - return actions.get(0); + private static List toActionsResponse(List actions) { + return actions.stream() + .map(ActionResponse::of) + .toList(); } } diff --git a/server/src/main/java/server/haengdong/presentation/response/StepsResponse.java b/server/src/main/java/server/haengdong/presentation/response/StepsResponse.java new file mode 100644 index 000000000..ac147387d --- /dev/null +++ b/server/src/main/java/server/haengdong/presentation/response/StepsResponse.java @@ -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 steps) { + + public static StepsResponse of(List actions) { + List steps = new ArrayList<>(); + List currentMembers = new ArrayList<>(); + List> groups = createGroups(actions); + + for (List group : groups) { + changeCurrentMembers(group, currentMembers); + StepResponse stepResponse = StepResponse.of(currentMembers, group); + steps.add(stepResponse); + } + return new StepsResponse(steps); + } + + private static List> createGroups(List actions) { + List> 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> groups) { + List currentGroup = groups.get(groups.size() - 1); + return currentGroup.get(0).actionType() != action.actionType(); + } + + private static void changeCurrentMembers(List group, List currentMembers) { + for (ActionAppResponse action : group) { + if (action.actionType() == ActionType.IN) { + currentMembers.add(action.name()); + continue; + } + if (action.actionType() == ActionType.OUT) { + currentMembers.remove(action.name()); + } + } + } +} diff --git a/server/src/test/java/server/haengdong/presentation/response/StepResponseTest.java b/server/src/test/java/server/haengdong/presentation/response/StepResponseTest.java deleted file mode 100644 index aac78691c..000000000 --- a/server/src/test/java/server/haengdong/presentation/response/StepResponseTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package server.haengdong.presentation.response; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import java.util.ArrayList; -import java.util.List; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import server.haengdong.application.response.ActionAppResponse; -import server.haengdong.application.response.ActionAppResponse.ActionType; - -@SpringBootTest -class StepResponseTest { - - @Autowired - private ObjectMapper objectMapper; - - @DisplayName("") - @Test - void test() throws JsonProcessingException { - List actionAppResponse = new ArrayList<>(); - - // IN actions - ActionAppResponse actionAppResponse1 = new ActionAppResponse(3L, "망쵸", null, 3L, ActionType.IN); - actionAppResponse.add(actionAppResponse1); - ActionAppResponse actionAppResponse2 = new ActionAppResponse(4L, "백호", null, 4L, ActionType.IN); - actionAppResponse.add(actionAppResponse2); - - // BILL step 1 - ActionAppResponse actionAppResponse3 = new ActionAppResponse(1L, "감자탕", 10000L, 1L, ActionType.BILL); - actionAppResponse.add(actionAppResponse3); - ActionAppResponse actionAppResponse4 = new ActionAppResponse(2L, "인생네컷", 10000L, 2L, ActionType.BILL); - actionAppResponse.add(actionAppResponse4); - - // IN actions - ActionAppResponse actionAppResponse5 = new ActionAppResponse(5L, "소하", null, 5L, ActionType.IN); - actionAppResponse.add(actionAppResponse5); - ActionAppResponse actionAppResponse6 = new ActionAppResponse(6L, "웨디", null, 6L, ActionType.IN); - actionAppResponse.add(actionAppResponse6); - - // OUT actions - ActionAppResponse actionAppResponse7 = new ActionAppResponse(7L, "망쵸", null, 7L, ActionType.OUT); - actionAppResponse.add(actionAppResponse7); - ActionAppResponse actionAppResponse8 = new ActionAppResponse(8L, "백호", null, 8L, ActionType.OUT); - actionAppResponse.add(actionAppResponse8); - - // BILL step 2 - ActionAppResponse actionAppResponse9 = new ActionAppResponse(9L, "노래방", 20000L, 10L, ActionType.BILL); - actionAppResponse.add(actionAppResponse9); - - // StepResponse creation - StepResponse stepResponse = StepResponse.of(actionAppResponse); - System.out.println("stepResponse = " + stepResponse); - } -} diff --git a/server/src/test/java/server/haengdong/presentation/response/StepsResponseTest.java b/server/src/test/java/server/haengdong/presentation/response/StepsResponseTest.java new file mode 100644 index 000000000..f8fdb6e7d --- /dev/null +++ b/server/src/test/java/server/haengdong/presentation/response/StepsResponseTest.java @@ -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 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(); + } +}