Skip to content

Commit

Permalink
Feat : STOMP 1차 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim-Dong-Jun99 committed Jan 10, 2024
1 parent bd3026b commit 08fb437
Show file tree
Hide file tree
Showing 18 changed files with 91 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .http/NaverMap.http
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ X-NCP-APIGW-API-KEY: {{naver-map-client-secret}}


### 자동차 경로 계산 api
GET https://naveropenapi.apigw.ntruss.com/map-direction-15/v1/driving?start=127.1058342,37.359708&goal=126.9284261,37.5258975
GET https://naveropenapi.apigw.ntruss.com/map-direction-15/v1/driving?start=128.5175868107,36.5376537450&goal=128.5163479914,36.5390674494
X-NCP-APIGW-API-KEY-ID: {{naver-map-client-id}}
X-NCP-APIGW-API-KEY: {{naver-map-client-secret}}
7 changes: 7 additions & 0 deletions .http/Odsay.http
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ Origin: https://weplanplans.site
GET https://api.odsay.com/v1/api/searchPubTransPathT?apiKey={{odsay-key}}&SX=127.1051573&SY=37.3718141&EX=227.124206&EY=37.495701
Origin: https://weplanplans.site

### 대중교통 길 찾기
GET https://api.odsay.com/v1/api/searchPubTransPathT?apiKey={{odsay-key}}&SX=128.5163479914&SY=36.5390674494&EX=128.5163479914&EY=36.5390674494
Origin: https://weplanplans.site

### 대중교통 길 찾기
GET https://api.odsay.com/v1/api/searchPubTransPathT?apiKey={{odsay-key}}&SX=128.5163479914&SY=36.5390674494&EX=128.3383767680&EY=38.0731872541
Origin: https://weplanplans.site
8 changes: 8 additions & 0 deletions .json/AddTripItems.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"visitDate": "2024-01-04",
"newTripItems": [
{
"tourItemId": 72
}
]
}
3 changes: 3 additions & 0 deletions .json/GetPathAndItems.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"visitDate": "2024-01-03"
}
3 changes: 3 additions & 0 deletions .json/TripItemPriceUpdateMsg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"price": 50000
}
3 changes: 3 additions & 0 deletions .json/TripItemTransportationUpdateMsg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"transportation": "CAR"
}
3 changes: 3 additions & 0 deletions .json/TripItemVistDateUpdateMsg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"visitDate": "2024-01-04"
}
21 changes: 21 additions & 0 deletions .json/UpdateTripItemOrder.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"visitDate": "2024-01-03",
"tripItemOrder": [
{
"tripItemId": 1,
"seqNum": 3
},
{
"tripItemId": 3,
"seqNum": 4
},
{
"tripItemId": 4,
"seqNum": 1
},
{
"tripItemId": 6,
"seqNum": 2
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public record TripPathInfoMsg(

) {
public record PathInfo(
Long price,
Integer price,
Double totalDistance,
Long totalTime
){}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public class Trip extends BaseTimeEntity {
@ColumnDefault("0")
private Long tripItemPriceSum;
@ColumnDefault("0")
private Long transportationPriceSum;
private Integer transportationPriceSum;
@Convert(converter = MapConverter.class)
@Column(columnDefinition = "JSON")
private Map<String, Long> tripPathPriceMap;
private Map<String, Integer> tripPathPriceMap;

@OneToMany(mappedBy = "trip", fetch = LAZY, cascade = REMOVE)
private final List<TripMember> tripMembers = new ArrayList<>();
Expand Down Expand Up @@ -80,7 +80,7 @@ public TripInfoMsg toTripInfo() {
this.getArea(), this.getSubarea(), this.getBudget());
}

public void updateTransportationPriceSum(Long oldVisitDateTransportationPriceSum, Long newVisitDateTransportationPriceSum) {
public void updateTransportationPriceSum(Integer oldVisitDateTransportationPriceSum, Integer newVisitDateTransportationPriceSum) {
this.transportationPriceSum -= oldVisitDateTransportationPriceSum;
this.transportationPriceSum += newVisitDateTransportationPriceSum;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package org.tenten.tentenstomp.domain.trip.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.tenten.tentenstomp.domain.trip.entity.Trip;

import java.util.Optional;

public interface TripRepository extends JpaRepository<Trip, Long> {
@Query("SELECT t FROM Trip t JOIN FETCH t.tripItems WHERE t.id = :tripId")
Optional<Trip> findTripByTripId(@Param("tripId") Long tripId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import org.tenten.tentenstomp.global.component.dto.response.TripPathCalculationResult;
import org.tenten.tentenstomp.global.exception.GlobalException;
import org.tenten.tentenstomp.global.messaging.kafka.producer.KafkaProducer;
import org.tenten.tentenstomp.global.messaging.redis.publisher.RedisPublisher;
import org.tenten.tentenstomp.global.util.RedisChannelUtil;

import java.time.LocalDate;
import java.util.ArrayList;
Expand All @@ -27,7 +25,6 @@

import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.tenten.tentenstomp.global.common.constant.TopicConstant.*;
import static org.tenten.tentenstomp.global.common.constant.TopicConstant.BUDGET;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -61,7 +58,7 @@ public void updateTripItemVisitDate(String tripItemId, TripItemVisitDateUpdateMs
List<TripItem> newDateTripItems = tripItemRepository.findTripItemByTripIdAndVisitDate(tripItem.getTrip().getId(), newDate);

Long oldSeqNum = tripItem.getSeqNum();
Long newSeqNum = (long) newDateTripItems.size();
Long newSeqNum = (long) newDateTripItems.size()+1;
List<TripItem> newPastDateTripItems = new ArrayList<>();
for (TripItem pastDateTripItem : pastDateTripItems) {
if (pastDateTripItem.getId().equals(tripItem.getId())) {
Expand All @@ -80,9 +77,9 @@ public void updateTripItemVisitDate(String tripItemId, TripItemVisitDateUpdateMs
TripPathCalculationResult pastDateTripPath = pathComponent.getTripPath(TripPlace.fromTripItems(newPastDateTripItems));
TripPathCalculationResult newDateTripPath = pathComponent.getTripPath(TripPlace.fromTripItems(newDateTripItems));

Map<String, Long> tripPathPriceMap = trip.getTripPathPriceMap();
trip.updateTransportationPriceSum(tripPathPriceMap.getOrDefault(pastDate.toString(), 0L), pastDateTripPath.pathPriceSum());
trip.updateTransportationPriceSum(tripPathPriceMap.getOrDefault(newDate.toString(), 0L), newDateTripPath.pathPriceSum());
Map<String, Integer> tripPathPriceMap = trip.getTripPathPriceMap();
trip.updateTransportationPriceSum(tripPathPriceMap.getOrDefault(pastDate.toString(), 0), pastDateTripPath.pathPriceSum());
trip.updateTransportationPriceSum(tripPathPriceMap.getOrDefault(newDate.toString(), 0), newDateTripPath.pathPriceSum());
tripPathPriceMap.put(pastDate.toString(), pastDateTripPath.pathPriceSum());
tripPathPriceMap.put(newDate.toString(), newDateTripPath.pathPriceSum());
tripRepository.save(trip);
Expand Down Expand Up @@ -118,8 +115,8 @@ public void deleteTripItem(String tripItemId) {

tripItemRepository.delete(tripItem);
TripPathCalculationResult tripPath = pathComponent.getTripPath(TripPlace.fromTripItems(newTripItems));
Map<String, Long> tripPathPriceMap = trip.getTripPathPriceMap();
trip.updateTransportationPriceSum(tripPathPriceMap.getOrDefault(visitDate.toString(), 0L), tripPath.pathPriceSum());
Map<String, Integer> tripPathPriceMap = trip.getTripPathPriceMap();
trip.updateTransportationPriceSum(tripPathPriceMap.getOrDefault(visitDate.toString(), 0), tripPath.pathPriceSum());
tripPathPriceMap.put(visitDate.toString(), tripPath.pathPriceSum());
tripRepository.save(trip);

Expand Down Expand Up @@ -147,8 +144,8 @@ public void updateTripItemTransportation(String tripItemId, TripItemTransportati
tripItemRepository.save(tripItem);

TripPathCalculationResult tripPath = pathComponent.getTripPath(TripPlace.fromTripItems(tripItems));
Map<String, Long> tripPathPriceMap = trip.getTripPathPriceMap();
trip.updateTransportationPriceSum(tripPathPriceMap.getOrDefault(visitDate.toString(), 0L), tripPath.pathPriceSum());
Map<String, Integer> tripPathPriceMap = trip.getTripPathPriceMap();
trip.updateTransportationPriceSum(tripPathPriceMap.getOrDefault(visitDate.toString(), 0), tripPath.pathPriceSum());
tripPathPriceMap.put(visitDate.toString(), tripPath.pathPriceSum());
tripRepository.save(trip);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import org.tenten.tentenstomp.global.component.PathComponent;
import org.tenten.tentenstomp.global.component.dto.request.TripPlace;
import org.tenten.tentenstomp.global.component.dto.response.TripPathCalculationResult;
import org.tenten.tentenstomp.global.exception.GlobalException;
import org.tenten.tentenstomp.global.messaging.kafka.producer.KafkaProducer;

import java.time.LocalDate;
import java.util.*;

import static org.springframework.http.HttpStatus.NOT_FOUND;
import static org.tenten.tentenstomp.global.common.constant.TopicConstant.*;

@Service
Expand Down Expand Up @@ -112,8 +114,8 @@ public void addTripItem(String tripId, TripItemAddMsg tripItemAddMsg) {

private void updateBudgetAndItemsAndPath(Trip trip, List<TripItem> tripItems, String visitDate) {
TripPathCalculationResult tripPath = pathComponent.getTripPath(TripPlace.fromTripItems(tripItems));
Map<String, Long> tripPathPriceMap = trip.getTripPathPriceMap();
trip.updateTransportationPriceSum(tripPathPriceMap.getOrDefault(visitDate, 0L), tripPath.pathPriceSum());
Map<String, Integer> tripPathPriceMap = trip.getTripPathPriceMap();
trip.updateTransportationPriceSum(tripPathPriceMap.getOrDefault(visitDate, 0), tripPath.pathPriceSum());
tripPathPriceMap.put(visitDate, tripPath.pathPriceSum());
tripRepository.save(trip);

Expand All @@ -126,7 +128,7 @@ private void updateBudgetAndItemsAndPath(Trip trip, List<TripItem> tripItems, St

@Transactional
public void updateTripItemOrder(String tripId, TripItemOrderUpdateMsg orderUpdateMsg) {
Trip trip = tripRepository.getReferenceById(Long.parseLong(tripId));
Trip trip = tripRepository.findTripByTripId(Long.parseLong(tripId)).orElseThrow(() -> new GlobalException("해당 아이디로 존재하는 여정이 없습니다 " + tripId, NOT_FOUND));
Map<Long, Long> itemOrderMap = new HashMap<>();
for (OrderInfo orderInfo : orderUpdateMsg.tripItemOrder()) {
itemOrderMap.put(orderInfo.tripItemId(), orderInfo.seqNum());
Expand Down Expand Up @@ -180,7 +182,7 @@ private void sendToKafkaAndSave(Object... dataArgs) {
private TripMemberMsg getTripMemberMsg(String tripId) {
Object cached = redisCache.get(MEMBER, tripId);
if (cached != null) {
return (TripMemberMsg) cached;
return objectMapper.convertValue(cached, TripMemberMsg.class);
}
HashMap<Long, TripMemberInfoMsg> connectedMemberMap = tripConnectedMemberMap.getOrDefault(tripId, new HashMap<>());
TripMemberMsg tripMemberMsg = new TripMemberMsg(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import org.tenten.tentenstomp.domain.trip.dto.response.TripPathInfoMsg;
import org.tenten.tentenstomp.global.exception.GlobalException;

import java.util.List;
import java.util.Map;

import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpStatus.CONFLICT;
import static org.tenten.tentenstomp.domain.trip.dto.response.TripPathInfoMsg.*;
import static org.tenten.tentenstomp.domain.trip.dto.response.TripPathInfoMsg.PathInfo;
import static org.tenten.tentenstomp.global.common.constant.NaverMapConstant.NAVER_MAP_API_KEY_HEADER;
import static org.tenten.tentenstomp.global.common.constant.NaverMapConstant.NAVER_MAP_CLIENT_ID_HEADER;

Expand Down Expand Up @@ -69,7 +70,7 @@ public PathInfo calculatePathInfo(String fromLongitude,
Integer fuelPrice = (Integer) summary.get("fuelPrice");
Integer distance = (Integer) summary.get("distance");
Integer duration = (Integer) summary.get("duration");
return new PathInfo((long) duration / 60_000 , (double) distance, (long)tollFare + fuelPrice);
return new PathInfo(tollFare + fuelPrice , (double) distance, (long) duration / 60_000);
} else {
throw new GlobalException("자동차 경로를 조회할 수 없습니다.", CONFLICT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import org.tenten.tentenstomp.domain.trip.dto.response.TripPathInfoMsg;
import org.tenten.tentenstomp.domain.trip.dto.response.TripPathInfoMsg.PathInfo;
import org.tenten.tentenstomp.global.exception.GlobalException;

Expand Down Expand Up @@ -56,10 +55,10 @@ public PathInfo calculatePathInfo(String fromLongitude,
List<Map<String, Object>> pathList = (List<Map<String, Object>>) resultMap.get("path");
Map<String, Object> path = pathList.get(0);
Map<String, Object> pathInfo = (Map<String, Object>) path.get("info");
Integer payment = (Integer) pathInfo.get("payment");
Double distance = (Double) pathInfo.get("totalDistance");
Integer payment = (Integer) pathInfo.get("totalPayment");
Double distance = Double.valueOf(Integer.toString((Integer) pathInfo.get("totalDistance")));
Integer totalTime = (Integer) pathInfo.get("totalTime");
return new PathInfo((long) totalTime, distance, (long) payment);
return new PathInfo( payment, distance, (long) totalTime);
} else {
throw new GlobalException("대중교통 경로를 조회할 수 없습니다.", CONFLICT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.tenten.tentenstomp.domain.trip.dto.response.TripPathInfoMsg;
import org.tenten.tentenstomp.domain.trip.dto.response.TripPathInfoMsg.PathInfo;
import org.tenten.tentenstomp.global.common.annotation.GetExecutionTime;
import org.tenten.tentenstomp.global.component.dto.request.TripPlace;
import org.tenten.tentenstomp.global.component.dto.request.PathCalculateRequest;
import org.tenten.tentenstomp.global.component.dto.request.TripPlace;
import org.tenten.tentenstomp.global.component.dto.response.TripPathCalculationResult;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import static org.tenten.tentenstomp.global.common.enums.Transportation.CAR;

Expand All @@ -23,7 +21,7 @@
public class PathComponent {
private final OdsayComponent odsayComponent;
private final NaverMapComponent naverMapComponent;
@Async
// @Async
public TripPathInfoMsg calculatePath(TripPlace fromPlace, TripPlace toPlace) {
long startTime = System.currentTimeMillis();
PathInfo pathInfo;
Expand All @@ -38,12 +36,14 @@ public TripPathInfoMsg calculatePath(TripPlace fromPlace, TripPlace toPlace) {
@GetExecutionTime
public TripPathCalculationResult getTripPath(List<TripPlace> tripPlaceList) {
List<PathCalculateRequest> pathCalculateRequests = toPathCalculateRequest(tripPlaceList);
long priceSum = 0L;
int priceSum = 0;
List<TripPathInfoMsg> pathInfoMsgs = new ArrayList<>();
for (PathCalculateRequest calculateRequest : pathCalculateRequests) {
TripPathInfoMsg tripPathInfoMsg = calculatePath(calculateRequest.from(), calculateRequest.to());
pathInfoMsgs.add(tripPathInfoMsg);
priceSum += tripPathInfoMsg.pathInfo().price();
if (tripPathInfoMsg.pathInfo() != null) {
priceSum += tripPathInfoMsg.pathInfo().price();
}
}
return new TripPathCalculationResult(priceSum, pathInfoMsgs);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.List;

public record TripPathCalculationResult(
Long pathPriceSum,
Integer pathPriceSum,
List<TripPathInfoMsg> tripPathInfoMsgs
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.Map;

@Converter
public class MapConverter implements AttributeConverter<Map, String> {
public class MapConverter implements AttributeConverter<Map<String, Integer>, String> {
protected final ObjectMapper objectMapper;

public MapConverter() {
Expand All @@ -28,11 +28,11 @@ public String convertToDatabaseColumn(Map map) {
} }

@Override
public Map<String, Long> convertToEntityAttribute(String s) {
public Map<String, Integer> convertToEntityAttribute(String s) {

if (StringUtils.hasText(s)) {
try {
return objectMapper.readValue(s, Map.class); // 5
return (Map<String, Integer>) objectMapper.readValue(s, Map.class); // 5
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 08fb437

Please sign in to comment.