Skip to content

Commit

Permalink
Merge pull request #77 from Team-Going/feature/73
Browse files Browse the repository at this point in the history
[feat] 여행 친구 전체 조회 API 구현
  • Loading branch information
SunwoongH authored Jan 11, 2024
2 parents d46fa5b + ae53436 commit 95c4ef2
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
7 changes: 7 additions & 0 deletions doorip-api/src/main/java/org/doorip/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ public abstract class Constants {
public static final String COMPLETE = "complete";
public static final String OUR = "our";
public static final String MY = "my";
public static final String STYLE_A = "style_a";
public static final String STYLE_B = "style_b";
public static final String STYLE_C = "style_c";
public static final String STYLE_D = "style_d";
public static final String STYLE_E = "style_e";
public static final int MIN_STYLE_RATE = 0;
public static final int MAX_STYLE_RATE = 4;
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,11 @@ public ResponseEntity<ApiResponse<?>> getOurTodoDetail(@UserId final Long userId
final OurTodoResponse response = tripDetailService.getOurTodoDetail(userId, tripId);
return ApiResponseUtil.success(SuccessMessage.OK, response);
}

@GetMapping("/{tripId}/participants")
public ResponseEntity<ApiResponse<?>> getParticipants(@UserId final Long userId,
@PathVariable final Long tripId) {
final TripParticipantGetResponse response = tripDetailService.getParticipants(userId, tripId);
return ApiResponseUtil.success(SuccessMessage.OK, response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.doorip.trip.dto.response;

import lombok.AccessLevel;
import lombok.Builder;
import org.doorip.trip.domain.Participant;

import java.util.List;

@Builder(access = AccessLevel.PRIVATE)
public record TripParticipantGetResponse(
List<TripParticipantResponse> participants,
List<TripStyleResponse> styles
) {
public static TripParticipantGetResponse of(List<Participant> participants, List<TripStyleResponse> styles) {
return TripParticipantGetResponse.builder()
.participants(participants.stream()
.map(TripParticipantResponse::of)
.toList())
.styles(styles)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.doorip.trip.dto.response;

import lombok.AccessLevel;
import lombok.Builder;

@Builder(access = AccessLevel.PRIVATE)
public record TripStyleResponse(
int rate,
boolean isLeft
) {
public static TripStyleResponse of(int rate, boolean isLeft) {
return TripStyleResponse.builder()
.rate(rate)
.isLeft(isLeft)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.doorip.trip.domain.Trip;
import org.doorip.trip.dto.response.MyTodoResponse;
import org.doorip.trip.dto.response.OurTodoResponse;
import org.doorip.trip.dto.response.TripParticipantGetResponse;
import org.doorip.trip.dto.response.TripStyleResponse;
import org.doorip.trip.repository.TodoRepository;
import org.doorip.trip.repository.TripRepository;
import org.doorip.user.domain.User;
Expand All @@ -16,10 +18,10 @@

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.Objects;
import java.util.*;

import static java.lang.Math.round;
import static org.doorip.common.Constants.*;

@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand All @@ -44,6 +46,21 @@ public OurTodoResponse getOurTodoDetail(Long userId, Long tripId) {
return OurTodoResponse.of(findTrip, progressRate, isComplete, participants);
}

public TripParticipantGetResponse getParticipants(Long userId, Long tripId) {
Trip findTrip = getTrip(tripId);
List<Participant> participants = findTrip.getParticipants();
Participant ownerParticipant = getOwnerParticipant(userId, participants);
sortParticipants(participants, ownerParticipant);
List<TripStyleResponse> response = calculateAndGetPropensityAverageRates(participants);
return TripParticipantGetResponse.of(participants, response);
}

private Map<String, Integer> createDefaultPropensity() {
return new HashMap<>(Map.of(STYLE_A, MIN_STYLE_RATE, STYLE_B, MIN_STYLE_RATE,
STYLE_C, MIN_STYLE_RATE, STYLE_D, MIN_STYLE_RATE, STYLE_E, MIN_STYLE_RATE)) {
};
}

private int getIncompleteTodoCount(Long tripId) {
return todoRepository.countTodoByTripIdAndProgress(tripId, Progress.INCOMPLETE);
}
Expand Down Expand Up @@ -76,6 +93,15 @@ private int calculateTodoProgressRate(Long tripId) {
return round(((float) completeTodoCount / totalTodoCount) * 100);
}

private List<TripStyleResponse> calculateAndGetPropensityAverageRates(List<Participant> participants) {
int participantCount = participants.size();
Map<String, Integer> propensity = getDefaultPropensity(participants);
List<String> keys = sortPropensityKeys(propensity);
List<TripStyleResponse> response = new ArrayList<>();
calculateAndSetPropensityAverageRate(keys, propensity, participantCount, response);
return response;
}

private Trip getTrip(Long tripId) {
return tripRepository.findById(tripId)
.orElseThrow(() -> new EntityNotFoundException(ErrorMessage.TRIP_NOT_FOUND));
Expand All @@ -84,4 +110,32 @@ private Trip getTrip(Long tripId) {
private boolean isOwnerParticipant(Long userId, User user) {
return Objects.equals(user.getId(), userId);
}

private Map<String, Integer> getDefaultPropensity(List<Participant> participants) {
Map<String, Integer> propensity = createDefaultPropensity();
participants.forEach(participant -> setDefaultPropensity(participant, propensity));
return propensity;
}

private List<String> sortPropensityKeys(Map<String, Integer> propensity) {
List<String> keys = new ArrayList<>(propensity.keySet());
Collections.sort(keys);
return keys;
}

private void calculateAndSetPropensityAverageRate(List<String> keys, Map<String, Integer> propensity, int participantCount, List<TripStyleResponse> response) {
keys.forEach(key -> {
int rate = round((float) propensity.get(key) / participantCount);
boolean isLeft = rate <= MAX_STYLE_RATE - rate;
response.add(TripStyleResponse.of(rate, isLeft));
});
}

private void setDefaultPropensity(Participant participant, Map<String, Integer> propensity) {
propensity.put(STYLE_A, propensity.get(STYLE_A) + participant.getStyleA());
propensity.put(STYLE_B, propensity.get(STYLE_B) + participant.getStyleB());
propensity.put(STYLE_C, propensity.get(STYLE_C) + participant.getStyleC());
propensity.put(STYLE_D, propensity.get(STYLE_D) + participant.getStyleD());
propensity.put(STYLE_E, propensity.get(STYLE_E) + participant.getStyleE());
}
}

0 comments on commit 95c4ef2

Please sign in to comment.