-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
132 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 0 additions & 28 deletions
28
server/src/main/java/server/haengdong/presentation/response/ActionsResponse.java
This file was deleted.
Oops, something went wrong.
58 changes: 13 additions & 45 deletions
58
server/src/main/java/server/haengdong/presentation/response/StepResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
server/src/main/java/server/haengdong/presentation/response/StepsResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} | ||
} |
57 changes: 0 additions & 57 deletions
57
server/src/test/java/server/haengdong/presentation/response/StepResponseTest.java
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
server/src/test/java/server/haengdong/presentation/response/StepsResponseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |