Skip to content

Commit

Permalink
Merge pull request #20 from Capstone-Walking/fix/get-{trafficId}
Browse files Browse the repository at this point in the history
Fix/get {traffic id}
  • Loading branch information
nove1080 authored Jun 4, 2024
2 parents ac3f502 + cb774bd commit e15936a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.walking.api.converter;

import com.walking.api.service.dto.PredictedData;
import com.walking.api.web.dto.response.detail.FavoriteTrafficDetail;
import com.walking.api.web.dto.response.detail.PointDetail;
import com.walking.api.web.dto.response.detail.TrafficDetail;
import com.walking.data.entity.traffic.TrafficEntity;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public final class TrafficDetailConverter {
Expand All @@ -17,9 +19,18 @@ private TrafficDetailConverter() {}
* @param predictedData 사이클 정보 와 현재 색상 및 잔여시간을 예측한 데이터
* @return 예측 값을 바탕으로 만든 TrafficDetail
*/
public static TrafficDetail execute(PredictedData predictedData) {
public static TrafficDetail execute(
PredictedData predictedData, Optional<FavoriteTrafficDetail> favoriteTrafficDetail) {

TrafficEntity trafficEntity = predictedData.getTraffic();
boolean isFavorite = false;
String viewName = trafficEntity.getName();

if (favoriteTrafficDetail.isPresent()
&& favoriteTrafficDetail.get().getId().equals(trafficEntity.getId())) {
isFavorite = true;
viewName = favoriteTrafficDetail.get().getName();
}

return TrafficDetail.builder()
.id(trafficEntity.getId())
Expand All @@ -30,8 +41,8 @@ public static TrafficDetail execute(PredictedData predictedData) {
.redCycle(predictedData.getRedCycle().orElse(null))
.greenCycle(predictedData.getGreenCycle().orElse(null))
.detail(TrafficDetailInfoConverter.execute(trafficEntity))
.isFavorite(false)
.viewName(trafficEntity.getName())
.isFavorite(isFavorite)
.viewName(viewName)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,30 @@
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class ReadTrafficService {

private final TrafficRepository trafficRepository;

@Transactional(readOnly = true)
public TrafficEntity executeById(Long trafficId) {
return trafficRepository.findById(trafficId).orElseThrow();
}

@Transactional(readOnly = true)
public List<TrafficEntity> executeByIds(List<Long> trafficIds) {
return trafficRepository.findByIds(trafficIds);
}

@Transactional(readOnly = true)
public List<TrafficEntity> executeByLocationAndDistance(Float lat, Float lng, Integer distance) {
return trafficRepository.findByLocationAndDistance(lat, lng, distance);
}

@Transactional(readOnly = true)
public List<TrafficEntity> executeWithinBounds(
Float blLng, Float blLat, Float trLng, Float trLat) {
return trafficRepository.findTrafficWithinBounds(blLng, blLat, trLng, trLat);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import com.walking.api.domain.traffic.usecase.DeleteFavoriteTrafficUseCase;
import com.walking.api.domain.traffic.usecase.UpdateFavoriteTrafficUseCase;
import com.walking.api.security.authentication.token.TokenUserDetails;
import com.walking.api.security.authentication.token.TokenUserDetailsService;
import com.walking.api.security.filter.token.AccessTokenResolver;
import com.walking.api.service.TrafficIntegrationPredictService;
import com.walking.api.service.dto.PredictedData;
import com.walking.api.service.dto.request.IntegrationPredictRequestDto;
Expand All @@ -34,13 +36,17 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -60,6 +66,8 @@ public class TrafficController {

private final TrafficIntegrationPredictService integrationPredictService;
private final ReadTrafficService readTrafficService;
private final TokenUserDetailsService tokenUserDetailsService;

private final AddFavoriteTrafficUseCase addFavoriteTrafficUseCase;
private final BrowseFavoriteTrafficsUseCase browseFavoriteTrafficsUseCase;
private final DeleteFavoriteTrafficUseCase deleteFavoriteTrafficUseCase;
Expand Down Expand Up @@ -112,22 +120,38 @@ public ApiResponse<ApiResponse.SuccessBody<SearchTrafficsResponse>> searchTraffi
return ApiResponseGenerator.success(response, HttpStatus.OK, MessageCode.SUCCESS);
}

// todo: HttpServletRequest 파라미터로 설정하고 여기서 header에 있는 Authentication 을 가지고 토큰을 가져오도록
// 직접 header에서 token을 가져오는 이유 -> spring security를 사용하게되면 로그인하지 않은 사용자의 경우 토큰이 전달되지 않으므로
// filter에서 걸러져서 요청이 안들어옴
// Security package 안에 토큰 필터 -> request를 어떻게 가져오는지 보고 그냥 가져다 쓰면 된다.
@GetMapping("/{trafficId}")
public ApiResponse<ApiResponse.SuccessBody<BrowseTrafficsResponse>> browseTraffic(
@PathVariable Long trafficId) {
// todo implement
HttpServletRequest request, @PathVariable Long trafficId) {
log.info("Traffic browse trafficId: {}", trafficId);
IntegrationPredictResponseDto integrationPredictedResponse =
integrationPredictService.execute(
IntegrationPredictRequestDto.builder().trafficIds(List.of(trafficId)).build());

Map<Long, PredictedData> predictedDataMap = integrationPredictedResponse.getPredictedDataMap();
PredictedData predictedData = predictedDataMap.get(trafficId);
TrafficDetail trafficDetail = TrafficDetailConverter.execute(predictedData);

// 인증 토큰이 헤더에 들어있는지
String authorization = request.getHeader("Authorization");
Optional<FavoriteTrafficDetail> favoriteTrafficDetail = Optional.empty();
if (Objects.nonNull(authorization)) {

String token = AccessTokenResolver.resolve(authorization);
UserDetails userDetails = tokenUserDetailsService.loadUserByUsername(token);
Long memberId = Long.valueOf(userDetails.getUsername());

BrowseFavoriteTrafficsResponse favoriteTraffics =
browseFavoriteTrafficsUseCase.execute(
BrowseFavoriteTrafficsUseCaseRequest.builder().memberId(memberId).build());

favoriteTrafficDetail =
favoriteTraffics.getTraffics().stream()
.filter(traffic -> traffic.getId().equals(trafficId))
.findFirst();
}

TrafficDetail trafficDetail =
TrafficDetailConverter.execute(predictedData, favoriteTrafficDetail);
BrowseTrafficsResponse response =
BrowseTrafficsResponse.builder().traffic(trafficDetail).build();
return ApiResponseGenerator.success(response, HttpStatus.OK, MessageCode.SUCCESS);
Expand Down

0 comments on commit e15936a

Please sign in to comment.