Skip to content

Commit

Permalink
feat: 액션 이력 조회 반환 형식 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
kunsanglee committed Jul 25, 2024
1 parent 962ede0 commit 7d624cc
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,34 @@ public List<ActionAppResponse> findActions(String token) {
return getActionAppResponses(billActions, memberActions);
}

private static List<ActionAppResponse> getActionAppResponses(List<BillAction> billActions,
List<MemberAction> memberActions) {
private List<ActionAppResponse> getActionAppResponses(
List<BillAction> billActions,
List<MemberAction> memberActions
) {
int billActionIndex = 0;
int memberActionIndex = 0;
List<ActionAppResponse> actionAppResponses = new ArrayList<>();

List<ActionAppResponse> result = new ArrayList<>();
while (billActionIndex < billActions.size() && memberActionIndex < memberActions.size()) {
BillAction billAction = billActions.get(billActionIndex);
MemberAction memberAction = memberActions.get(memberActionIndex);
if (billAction.getSequence() < memberAction.getSequence()) {
result.add(ActionAppResponse.of(billAction));
actionAppResponses.add(ActionAppResponse.of(billAction));
billActionIndex++;
} else {
result.add(ActionAppResponse.of(memberAction));
actionAppResponses.add(ActionAppResponse.of(memberAction));
memberActionIndex++;
}
}
while (billActionIndex < billActions.size()) {
BillAction billAction = billActions.get(billActionIndex++);
result.add(ActionAppResponse.of(billAction));
actionAppResponses.add(ActionAppResponse.of(billAction));
}
while (memberActionIndex < memberActions.size()) {
MemberAction memberAction = memberActions.get(memberActionIndex++);
result.add(ActionAppResponse.of(memberAction));
actionAppResponses.add(ActionAppResponse.of(memberAction));
}

return result;
return actionAppResponses;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public record ActionAppResponse(
) {

public static ActionAppResponse of(BillAction billAction) {
return new ActionAppResponse(billAction.getAction().getId(),
return new ActionAppResponse(
billAction.getAction().getId(),
billAction.getTitle(),
billAction.getPrice(),
billAction.getSequence(),
Expand All @@ -24,7 +25,8 @@ public static ActionAppResponse of(BillAction billAction) {
public static ActionAppResponse of(MemberAction memberAction) {
MemberActionStatus status = memberAction.getStatus();

return new ActionAppResponse(memberAction.getAction().getId(),
return new ActionAppResponse(
memberAction.getAction().getId(),
memberAction.getMemberName(),
null,
memberAction.getSequence(),
Expand All @@ -36,14 +38,14 @@ public String actionTypeName() {
return actionType.name();
}

private enum ActionType {
public enum ActionType {
BILL,
IN,
OUT,
;

public static ActionType of(MemberActionStatus memberActionStatus) {
if (memberActionStatus.name().equals("IN")) {
private static ActionType of(MemberActionStatus memberActionStatus) {
if (MemberActionStatus.IN == memberActionStatus) {
return IN;
}
return OUT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import org.springframework.web.bind.annotation.RestController;
import server.haengdong.application.EventService;
import server.haengdong.presentation.request.EventSaveRequest;
import server.haengdong.presentation.response.ActionsResponse;
import server.haengdong.presentation.response.EventDetailResponse;
import server.haengdong.presentation.response.EventResponse;
import server.haengdong.presentation.response.StepResponse;

@RequiredArgsConstructor
@RestController
Expand All @@ -34,10 +34,10 @@ public ResponseEntity<EventDetailResponse> findEvent(@PathVariable("eventId") St
return ResponseEntity.ok(eventDetailResponse);
}

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

return ResponseEntity.ok(actionsResponse);
return ResponseEntity.ok(stepResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ public record ActionResponse(
Long actionId,
String name,
Long price,
Long sequence,
String type
Long sequence
) {

public static ActionResponse of(ActionAppResponse actionAppResponse) {
return new ActionResponse(actionAppResponse.actionId(),
return new ActionResponse(
actionAppResponse.actionId(),
actionAppResponse.name(),
actionAppResponse.price(),
actionAppResponse.sequence(),
actionAppResponse.actionTypeName()
actionAppResponse.sequence()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
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(List<ActionResponse> actions) {
public record ActionsResponse(
String type,
String stepName,
Set<String> members,
List<ActionResponse> actions
) {

public static ActionsResponse of(List<ActionAppResponse> actions) {
List<ActionResponse> actionResponses = actions.stream().map(ActionResponse::of).toList();
return new ActionsResponse(actionResponses);
public static ActionsResponse of(List<ActionAppResponse> actions, Set<String> members) {
List<ActionResponse> actionResponses = actions.stream()
.map(ActionResponse::of)
.toList();

String actionType = actions.get(0).actionTypeName();
return new ActionsResponse(
actionType,
null,
new HashSet<>(members),
actionResponses
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
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
) {

public static StepResponse of(List<ActionAppResponse> actions) {
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;
}
if (currentActionType.equals("BILL")) {
actionsResponse.add(ActionsResponse.of(group, members));
} else {
actionsResponse.add(ActionsResponse.of(group, Set.of()));
}
currentActionType = typeName;
group.clear();
if (typeName.equals("IN")) {
members.add(action.name());
}
if (typeName.equals("OUT")) {
members.remove(action.name());
}
group.add(action);
}

if (currentActionType.equals("BILL")) {
actionsResponse.add(ActionsResponse.of(group, members));
} else {
actionsResponse.add(ActionsResponse.of(group, null));
}

return new StepResponse(actionsResponse);
}

private static ActionAppResponse getFirstAction(List<ActionAppResponse> actions) {
return actions.get(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,6 @@ void findActionsTest() throws Exception {
.andExpect(jsonPath("$.actions[1].sequence").value(2L))
.andExpect(jsonPath("$.actions[1].type").value("BILL"));
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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> 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);
}
}

0 comments on commit 7d624cc

Please sign in to comment.