Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
hyxklee committed Feb 5, 2025
2 parents 5d030f2 + 678bc53 commit b5d6194
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public interface MatchingAlgorithmService {
* 방을 찾는 메서드
* 이미 방에 들어가있는 멤버가 다시 요청했을 때 Optional.empty()를 반환하도록 로직을 구성해야함
* @param userId 방에 들어가려는 사용자 ID
* @param startLongitude 시작 지점 경도
* @param startLatitude 시작 지점 위도
* @param destinationLongitude 도착 지점 경도
* @param destinationLatitude 도착 지점 위도
* @param departure 출발지
* @param destination 도착지
// * @param startLongitude 시작 지점 경도
// * @param startLatitude 시작 지점 위도
// * @param destinationLongitude 도착 지점 경도
// * @param destinationLatitude 도착 지점 위도
* @param criteria 방 검색에 필요한 기타 조건 (태그 등)
* @return Optional<FindRoomResult> - 매칭 가능한 방 정보가 있으면 값이 있고, 없으면 empty
*/
Optional<FindRoomResult> findRoom(Long userId, double startLongitude, double startLatitude, double destinationLongitude, double destinationLatitude, List<Tags> criteria);
Optional<FindRoomResult> findRoom(Long userId, String departure, String destination, List<Tags> criteria);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ public class MatchingAlgorithmServiceImpl implements MatchingAlgorithmService {
@Value("${gachtaxi.matching.auto-matching-description}")
private String autoMatchingDescription;

private static final double SEARCH_RADIUS = 300.0;
// private static final double SEARCH_RADIUS = 300.0;

@Override
public Optional<FindRoomResult> findRoom(Long userId, double startLongitude, double startLatitude,
double destinationLongitude, double destinationLatitude,
public Optional<FindRoomResult> findRoom(Long userId, String departure, String destination,
List<Tags> criteria) {
/*
사용자 ID로 사용자 정보 조회(이미 방에 참여하고 있는지 중복체크)
Expand All @@ -43,16 +42,21 @@ public Optional<FindRoomResult> findRoom(Long userId, double startLongitude, dou
}
});

/*
위치 정보를 이용한 방 검색(300M 이내)ø
*/
List<MatchingRoom> matchingRooms = matchingRoomRepository.findRoomsByStartAndDestination(
startLongitude,
startLatitude,
destinationLongitude,
destinationLatitude,
SEARCH_RADIUS
);
/*
출발지와 도착지 기준으로 방 검색
*/
List<MatchingRoom> matchingRooms = matchingRoomRepository.findRoomsByDepartureAndDestination(departure, destination);

// /*
// 위치 정보를 이용한 방 검색(300M 이내)ø
// */
// List<MatchingRoom> matchingRooms = matchingRoomRepository.findRoomsByStartAndDestination(
// startLongitude,
// startLatitude,
// destinationLongitude,
// destinationLatitude,
// SEARCH_RADIUS
// );
/*
ACTIVE 상태인 방 && 블랙리스트가 없는 방만 필터링
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@

public record AutoMatchingPostRequest(
String startPoint,
String startName,
// String startName,
String destinationPoint,
// String destinationName,
String startName,
String destinationName,
List<String> criteria,
List<Integer> members,
Expand All @@ -18,4 +20,10 @@ public List<Tags> getCriteria() {
.map(Tags::valueOf)
.toList();
}
public String getDeparture() {
return startName;
}
public String getDestination() {
return destinationName;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.gachtaxi.domain.matching.common.dto.response;

import com.gachtaxi.domain.matching.common.entity.MatchingRoom;
import java.time.LocalDateTime;
import java.util.List;

public record MatchingRoomResponse(
Expand All @@ -10,7 +9,7 @@ public record MatchingRoomResponse(
String description,
String departure,
String destination,
LocalDateTime departureTime,
String departureTime,
int maxCapacity,
int currentMembers,
List<String> tags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
import jakarta.persistence.FetchType;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import java.time.LocalDateTime;
import java.util.List;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -68,7 +66,7 @@ public class MatchingRoom extends BaseEntity {

@Column(name = "departure_time")
@Getter
private LocalDateTime departureTime;
private String departureTime;

@Column(name = "departure")
@Getter
Expand Down Expand Up @@ -112,20 +110,22 @@ public boolean isFull(int size) {

public boolean isAutoConvertible(int currentMembers) { return currentMembers < this.capacity; }

public static MatchingRoom activeOf(MatchRoomCreatedEvent matchRoomCreatedEvent, Members members, Route route, ChattingRoom chattingRoom) {
public static MatchingRoom activeOf(MatchRoomCreatedEvent matchRoomCreatedEvent, Members members, ChattingRoom chattingRoom) {
return MatchingRoom.builder()
.capacity(matchRoomCreatedEvent.maxCapacity())
.roomMaster(members)
.title(matchRoomCreatedEvent.title())
.description(matchRoomCreatedEvent.description())
.route(route)
// .route(route)
.departure(matchRoomCreatedEvent.startName())
.destination(matchRoomCreatedEvent.destinationName())
.totalCharge(matchRoomCreatedEvent.expectedTotalCharge())
.matchingRoomStatus(MatchingRoomStatus.ACTIVE)
.chattingRoomId(chattingRoom.getId())
.build();
}

public static MatchingRoom manualOf(Members roomMaster, String departure, String destination, String description, int maxCapacity, int totalCharge, LocalDateTime departureTime, Long chattingRoomId) {
public static MatchingRoom manualOf(Members roomMaster, String departure, String destination, String description, int maxCapacity, int totalCharge, String departureTime, Long chattingRoomId) {
return MatchingRoom.builder()
.capacity(4)
.roomMaster(roomMaster)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.gachtaxi.domain.matching.common.entity.enums.MatchingRoomType;
import com.gachtaxi.domain.members.entity.Members;
import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -15,17 +14,25 @@

@Repository
public interface MatchingRoomRepository extends JpaRepository<MatchingRoom, Long> {
// @Query("SELECT r FROM MatchingRoom r " +
// "WHERE " +
// "FUNCTION('ST_Distance_Sphere', FUNCTION('POINT', :startLongitude, :startLatitude), FUNCTION('POINT', r.route.startLongitude, r.route.startLatitude)) <= :radius " +
// "AND FUNCTION('ST_Distance_Sphere', FUNCTION('POINT', :destinationLongitude, :destinationLatitude), FUNCTION('POINT', r.route.endLongitude, r.route.endLatitude)) <= :radius ")
// List<MatchingRoom> findRoomsByStartAndDestination(
// @Param("startLongitude") double startLongitude,
// @Param("startLatitude") double startLatitude,
// @Param("destinationLongitude") double destinationLongitude,
// @Param("destinationLatitude") double destinationLatitude,
// @Param("radius") double radius
// );
/**
* 출발지와 도착지 기준으로 매칭 방 찾기 (위도, 경도 제거)
*/
@Query("SELECT r FROM MatchingRoom r " +
"WHERE " +
"FUNCTION('ST_Distance_Sphere', FUNCTION('POINT', :startLongitude, :startLatitude), FUNCTION('POINT', r.route.startLongitude, r.route.startLatitude)) <= :radius " +
"AND FUNCTION('ST_Distance_Sphere', FUNCTION('POINT', :destinationLongitude, :destinationLatitude), FUNCTION('POINT', r.route.endLongitude, r.route.endLatitude)) <= :radius ")
List<MatchingRoom> findRoomsByStartAndDestination(
@Param("startLongitude") double startLongitude,
@Param("startLatitude") double startLatitude,
@Param("destinationLongitude") double destinationLongitude,
@Param("destinationLatitude") double destinationLatitude,
@Param("radius") double radius
);
"WHERE r.departure = :departure " +
"AND r.destination = :destination " +
"AND r.matchingRoomStatus = 'ACTIVE' ")
List<MatchingRoom> findRoomsByDepartureAndDestination(@Param("departure") String departure, @Param("destination") String destination);
@Query("SELECT r " +
"FROM MatchingRoom r JOIN r.memberMatchingRoomChargingInfo m " +
"WHERE m.members = :user "+
Expand All @@ -42,6 +49,9 @@ List<MatchingRoom> findRoomsByStartAndDestination(

Page<MatchingRoom> findByMatchingRoomTypeAndMatchingRoomStatus(MatchingRoomType type, MatchingRoomStatus status, Pageable pageable);

@Query("SELECT m.matchingRoom FROM MemberMatchingRoomChargingInfo m WHERE m.members = :user ORDER BY m.matchingRoom.id DESC")
@Query("SELECT m.matchingRoom FROM MemberMatchingRoomChargingInfo m " +
"WHERE m.members = :user " +
"AND m.matchingRoom.matchingRoomStatus = 'ACTIVE' " +
"ORDER BY m.matchingRoom.id DESC")
Page<MatchingRoom> findByMemberInMatchingRoom(@Param("user") Members user, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,28 @@ public AutoMatchingPostResponse handlerAutoRequestMatching(
AutoMatchingPostRequest autoMatchingPostRequest
) {
List<Tags> criteria = autoMatchingPostRequest.getCriteria();
// String[] startCoordinates = autoMatchingPostRequest.startPoint().split(",");
// double startLongitude = Double.parseDouble(startCoordinates[0]);
// double startLatitude = Double.parseDouble(startCoordinates[1]);
//
// String[] destinationCoordinates = autoMatchingPostRequest.destinationPoint().split(",");
// double destinationLongitude = Double.parseDouble(destinationCoordinates[0]);
// double destinationLatitude = Double.parseDouble(destinationCoordinates[1]);

String[] startCoordinates = autoMatchingPostRequest.startPoint().split(",");
double startLongitude = Double.parseDouble(startCoordinates[0]);
double startLatitude = Double.parseDouble(startCoordinates[1]);
// Optional<FindRoomResult> optionalRoom =
// this.matchingAlgorithmService.findRoom(memberId, startLongitude, startLatitude, destinationLongitude, destinationLatitude, criteria);

String[] destinationCoordinates = autoMatchingPostRequest.destinationPoint().split(",");
double destinationLongitude = Double.parseDouble(destinationCoordinates[0]);
double destinationLatitude = Double.parseDouble(destinationCoordinates[1]);

Optional<FindRoomResult> optionalRoom =
this.matchingAlgorithmService.findRoom(memberId, startLongitude, startLatitude, destinationLongitude, destinationLatitude, criteria);

Optional<FindRoomResult> optionalRoom =
this.matchingAlgorithmService.findRoom(
memberId,
autoMatchingPostRequest.getDeparture(),
autoMatchingPostRequest.getDestination(),
criteria
);
// Optional<FindRoomResult> optionalRoom =
// this.matchingAlgorithmService.findRoom(memberId, autoMatchingPostRequest.startPoint(), autoMatchingPostRequest.destinationPoint(), criteria);
optionalRoom
.ifPresentOrElse(
roomResult -> this.sendMatchMemberJoinedEvent(memberId, roomResult),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import com.gachtaxi.domain.members.service.BlacklistService;
import com.gachtaxi.domain.members.service.MemberService;
import jakarta.transaction.Transactional;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -69,17 +67,14 @@ public Long createManualMatchingRoom(Long userId, ManualMatchingRequest request)
.build();
chattingRoomRepository.save(chattingRoom);

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime departureTime = LocalDateTime.parse(request.getDeparture(), formatter);

MatchingRoom matchingRoom = MatchingRoom.manualOf(
roomMaster,
request.getDeparture(),
request.getDestination(),
request.description(),
4,
request.getTotalCharge(),
departureTime,
request.departureTime(),
chattingRoom.getId()
);

Expand Down Expand Up @@ -151,23 +146,6 @@ public void joinManualMatchingRoom(Long userId, Long roomId) {
*/
@Transactional
public void convertToAutoMatching(Long roomId) {
MatchingRoom matchingRoom = this.matchingRoomRepository.findById(roomId)
.orElseThrow(NoSuchMatchingRoomException::new);

if (!matchingRoom.isActive()) {
throw new NotActiveMatchingRoomException();
}

if (LocalDateTime.now().isAfter(matchingRoom.getDepartureTime().minusMinutes(10))) {

int currentMembers = this.memberMatchingRoomChargingInfoRepository
.countByMatchingRoomAndPaymentStatus(matchingRoom, PaymentStatus.NOT_PAYED);

if (matchingRoom.isAutoConvertible(currentMembers)) {
matchingRoom.convertToAutoMatching();
matchingRoomRepository.save(matchingRoom);
}
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public class MatchingRoomService {
public MatchRoomCreatedEvent createMatchingRoom(MatchRoomCreatedEvent matchRoomCreatedEvent) {
Members members = this.memberService.findById(matchRoomCreatedEvent.roomMasterId());

Route route = this.saveRoute(matchRoomCreatedEvent);
// Route route = this.saveRoute(matchRoomCreatedEvent);

ChattingRoom chattingRoom = this.chattingRoomService.create();

MatchingRoom matchingRoom = MatchingRoom.activeOf(matchRoomCreatedEvent, members, route, chattingRoom);
MatchingRoom matchingRoom = MatchingRoom.activeOf(matchRoomCreatedEvent, members, chattingRoom);

this.saveMatchingRoomTagInfo(matchingRoom, matchRoomCreatedEvent.criteria());
this.saveRoomMasterChargingInfo(matchingRoom, members);
Expand All @@ -69,25 +69,25 @@ public MatchRoomCreatedEvent createMatchingRoom(MatchRoomCreatedEvent matchRoomC
return MatchRoomCreatedEvent.of(matchRoomCreatedEvent, savedMatchingRoom.getId(), savedMatchingRoom.getChattingRoomId());
}

private Route saveRoute(MatchRoomCreatedEvent matchRoomCreatedEvent) {
String[] startCoordinates = matchRoomCreatedEvent.startPoint().split(",");
double startLatitude = Double.parseDouble(startCoordinates[0]);
double startLongitude = Double.parseDouble(startCoordinates[1]);

String[] endCoordinates = matchRoomCreatedEvent.destinationPoint().split(",");
double endLatitude = Double.parseDouble(endCoordinates[0]);
double endLongitude = Double.parseDouble(endCoordinates[1]);

Route route = Route.builder()
.startLongitude(startLongitude)
.startLatitude(startLatitude)
.startLocationName(matchRoomCreatedEvent.startName())
.endLongitude(endLongitude)
.endLatitude(endLatitude)
.endLocationName(matchRoomCreatedEvent.destinationName())
.build();
return this.routeRepository.save(route);
}
// private Route saveRoute(MatchRoomCreatedEvent matchRoomCreatedEvent) {
// String[] startCoordinates = matchRoomCreatedEvent.startPoint().split(",");
// double startLatitude = Double.parseDouble(startCoordinates[0]);
// double startLongitude = Double.parseDouble(startCoordinates[1]);
//
// String[] endCoordinates = matchRoomCreatedEvent.destinationPoint().split(",");
// double endLatitude = Double.parseDouble(endCoordinates[0]);
// double endLongitude = Double.parseDouble(endCoordinates[1]);
//
// Route route = Route.builder()
// .startLongitude(startLongitude)
// .startLatitude(startLatitude)
// .startLocationName(matchRoomCreatedEvent.startName())
// .endLongitude(endLongitude)
// .endLatitude(endLatitude)
// .endLocationName(matchRoomCreatedEvent.destinationName())
// .build();
// return this.routeRepository.save(route);
// }

private void saveMatchingRoomTagInfo(MatchingRoom matchingRoom, List<Tags> tags) {
tags.forEach(tag -> this.matchingRoomTagInfoRepository.save(MatchingRoomTagInfo.of(matchingRoom, tag)));
Expand Down
Loading

0 comments on commit b5d6194

Please sign in to comment.