diff --git a/backend/src/main/java/com/twtw/backend/domain/group/entity/Group.java b/backend/src/main/java/com/twtw/backend/domain/group/entity/Group.java index f85a013d..05d61367 100644 --- a/backend/src/main/java/com/twtw/backend/domain/group/entity/Group.java +++ b/backend/src/main/java/com/twtw/backend/domain/group/entity/Group.java @@ -125,4 +125,10 @@ public boolean hasMember(final Member member) { public void removeMember(final GroupMember groupMember) { this.groupMembers.remove(groupMember); } + + public String[] getMemberIds() { + return this.groupMembers.stream() + .map(groupMember -> groupMember.getMember().getId().toString()) + .toArray(String[]::new); + } } diff --git a/backend/src/main/java/com/twtw/backend/domain/location/service/GeoService.java b/backend/src/main/java/com/twtw/backend/domain/location/service/GeoService.java index 69043098..a1f3c5f4 100644 --- a/backend/src/main/java/com/twtw/backend/domain/location/service/GeoService.java +++ b/backend/src/main/java/com/twtw/backend/domain/location/service/GeoService.java @@ -5,21 +5,20 @@ import com.twtw.backend.domain.location.dto.request.LocationRequest; import com.twtw.backend.domain.location.dto.response.AverageCoordinate; import com.twtw.backend.domain.member.entity.Member; - import lombok.RequiredArgsConstructor; - +import org.springframework.data.geo.Distance; import org.springframework.data.geo.Metrics; import org.springframework.data.geo.Point; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; -import java.util.stream.Collectors; +import java.util.List; @Service @RequiredArgsConstructor public class GeoService { - private static final int CURRENT_LOCATION_INDEX = 0; + private static final Distance DEFAULT_DISTANCE = new Distance(0, Metrics.KILOMETERS); private final RedisTemplate redisTemplate; public AverageCoordinate saveLocation( @@ -34,37 +33,35 @@ public AverageCoordinate saveLocation( new Point(locationRequest.getLongitude(), locationRequest.getLatitude()), memberId); - return calculateAverage(collectMemberDistances(planId), planId, memberId); + return calculateAverage(collectMemberDistances(planId, group), planId, memberId); } - private MemberDistances collectMemberDistances(final String planId) { - return redisTemplate.opsForSet().members(planId).stream() - .map( - member -> - redisTemplate - .opsForGeo() - .position(planId, member) - .get(CURRENT_LOCATION_INDEX)) - .collect(Collectors.collectingAndThen(Collectors.toList(), MemberDistances::new)); + private MemberDistances collectMemberDistances(final String groupId, final Group group) { + final List points = redisTemplate.opsForGeo().position(groupId, group.getMemberIds()); + return new MemberDistances(points); } private AverageCoordinate calculateAverage( - final MemberDistances memberDistances, final String planId, final String memberId) { + final MemberDistances memberDistances, final String groupId, final String memberId) { final double averageLongitude = memberDistances.averageLongitude(); final double averageLatitude = memberDistances.averageLatitude(); - redisTemplate.opsForGeo().add(planId, new Point(averageLongitude, averageLatitude), planId); + redisTemplate.opsForGeo().add(groupId, new Point(averageLongitude, averageLatitude), groupId); - final Double distance = distance(planId, memberId); + final Distance distance = distance(groupId, memberId); - return new AverageCoordinate(averageLongitude, averageLatitude, distance); + return new AverageCoordinate(averageLongitude, averageLatitude, distance.getValue()); } - private double distance(final String planId, final String memberId) { - return redisTemplate + private Distance distance(final String groupId, final String memberId) { + final Distance distance = redisTemplate .opsForGeo() - .distance(planId, memberId, planId, Metrics.KILOMETERS) - .getValue(); + .distance(groupId, memberId, groupId, Metrics.KILOMETERS); + + if (distance == null) { + return DEFAULT_DISTANCE; + } + return distance; } }