From 59ef17b63258c3024c54c898495039a4fe9bf0dc Mon Sep 17 00:00:00 2001 From: jinho7 <69239293+jinho7@users.noreply.github.com> Date: Thu, 18 Jul 2024 18:10:08 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feature:=20Weather-Service=EB=A1=9C?= =?UTF-8?q?=20=EB=B6=80=ED=84=B0=20=ED=95=B4=EB=8B=B9=20=EC=8B=9C=EA=B0=81?= =?UTF-8?q?=EC=9D=98=20=EC=B2=B4=EA=B0=90=20=EC=98=A8=EB=8F=84=20=EB=B0=9B?= =?UTF-8?q?=EC=95=84=EC=98=A4=EB=8A=94=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../userservice/config/SecurityConfig.java | 2 +- .../converter/SurveyConverter.java | 4 -- .../userservice/dto/request/SurveyReqDto.java | 4 +- .../userservice/entity/UserMedian.java | 3 +- .../jwt/filter/JwtAuthorizationFilter.java | 12 ----- .../global/jwt/filter/JwtLogoutFilter.java | 2 +- .../userservice/global/jwt/util/JwtUtil.java | 2 +- .../service/commandService/SurveyService.java | 10 +++-- .../service/commandService/UserService.java | 6 +-- .../{global => }/util/CalculateUtil.java | 2 +- .../{global => }/util/RedisUtil.java | 2 +- .../waither/userservice/util/RestClient.java | 45 +++++++++++++++++++ 12 files changed, 60 insertions(+), 34 deletions(-) rename user-service/src/main/java/com/waither/userservice/{global => }/util/CalculateUtil.java (85%) rename user-service/src/main/java/com/waither/userservice/{global => }/util/RedisUtil.java (94%) create mode 100644 user-service/src/main/java/com/waither/userservice/util/RestClient.java diff --git a/user-service/src/main/java/com/waither/userservice/config/SecurityConfig.java b/user-service/src/main/java/com/waither/userservice/config/SecurityConfig.java index 0eb68c03..03d6203d 100644 --- a/user-service/src/main/java/com/waither/userservice/config/SecurityConfig.java +++ b/user-service/src/main/java/com/waither/userservice/config/SecurityConfig.java @@ -3,7 +3,7 @@ import com.waither.userservice.global.jwt.filter.*; import com.waither.userservice.global.jwt.util.JwtUtil; -import com.waither.userservice.global.util.RedisUtil; +import com.waither.userservice.util.RedisUtil; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/user-service/src/main/java/com/waither/userservice/converter/SurveyConverter.java b/user-service/src/main/java/com/waither/userservice/converter/SurveyConverter.java index 2d221782..0ed3ab24 100644 --- a/user-service/src/main/java/com/waither/userservice/converter/SurveyConverter.java +++ b/user-service/src/main/java/com/waither/userservice/converter/SurveyConverter.java @@ -3,13 +3,9 @@ import com.waither.userservice.dto.request.SurveyReqDto; import com.waither.userservice.entity.*; import com.waither.userservice.entity.enums.Season; -import com.waither.userservice.global.exception.CustomException; -import com.waither.userservice.global.response.ErrorCode; import lombok.AccessLevel; import lombok.NoArgsConstructor; -import static com.waither.userservice.global.util.CalculateUtil.calculateMedian; - @NoArgsConstructor(access = AccessLevel.PRIVATE) public class SurveyConverter { diff --git a/user-service/src/main/java/com/waither/userservice/dto/request/SurveyReqDto.java b/user-service/src/main/java/com/waither/userservice/dto/request/SurveyReqDto.java index 016a8e1a..2499a552 100644 --- a/user-service/src/main/java/com/waither/userservice/dto/request/SurveyReqDto.java +++ b/user-service/src/main/java/com/waither/userservice/dto/request/SurveyReqDto.java @@ -4,9 +4,9 @@ public class SurveyReqDto { - // Todo : DTO에서 날짜/시간 관련 필드는 String 타입으로 선언하고, 서비스 계층에서 원하는 형식으로 파싱하는 것이 좋은가 고민중. public record SurveyRequestDto( - + Double latitude, + Double longitude, // level Integer ans, LocalDateTime time diff --git a/user-service/src/main/java/com/waither/userservice/entity/UserMedian.java b/user-service/src/main/java/com/waither/userservice/entity/UserMedian.java index 59965853..a44860ed 100644 --- a/user-service/src/main/java/com/waither/userservice/entity/UserMedian.java +++ b/user-service/src/main/java/com/waither/userservice/entity/UserMedian.java @@ -1,6 +1,5 @@ package com.waither.userservice.entity; -import com.waither.userservice.converter.SurveyConverter; import com.waither.userservice.entity.enums.Season; import com.waither.userservice.global.BaseEntity; import jakarta.persistence.*; @@ -8,7 +7,7 @@ import java.util.List; -import static com.waither.userservice.global.util.CalculateUtil.calculateMedian; +import static com.waither.userservice.util.CalculateUtil.calculateMedian; @Builder @Getter diff --git a/user-service/src/main/java/com/waither/userservice/global/jwt/filter/JwtAuthorizationFilter.java b/user-service/src/main/java/com/waither/userservice/global/jwt/filter/JwtAuthorizationFilter.java index 04636856..73f63c3a 100644 --- a/user-service/src/main/java/com/waither/userservice/global/jwt/filter/JwtAuthorizationFilter.java +++ b/user-service/src/main/java/com/waither/userservice/global/jwt/filter/JwtAuthorizationFilter.java @@ -1,17 +1,5 @@ package com.waither.userservice.global.jwt.filter; -import com.waither.userservice.global.jwt.util.JwtUtil; -import com.waither.userservice.global.util.RedisUtil; -import jakarta.servlet.FilterChain; -import jakarta.servlet.ServletException; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.web.filter.OncePerRequestFilter; - -import java.io.IOException; //@Slf4j //@RequiredArgsConstructor //public class JwtAuthorizationFilter extends OncePerRequestFilter { diff --git a/user-service/src/main/java/com/waither/userservice/global/jwt/filter/JwtLogoutFilter.java b/user-service/src/main/java/com/waither/userservice/global/jwt/filter/JwtLogoutFilter.java index 84c384d6..59a8b4eb 100644 --- a/user-service/src/main/java/com/waither/userservice/global/jwt/filter/JwtLogoutFilter.java +++ b/user-service/src/main/java/com/waither/userservice/global/jwt/filter/JwtLogoutFilter.java @@ -3,7 +3,7 @@ import com.waither.userservice.global.jwt.execption.SecurityErrorCode; import com.waither.userservice.global.jwt.execption.SecurityCustomException; import com.waither.userservice.global.jwt.util.JwtUtil; -import com.waither.userservice.global.util.RedisUtil; +import com.waither.userservice.util.RedisUtil; import io.jsonwebtoken.ExpiredJwtException; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; diff --git a/user-service/src/main/java/com/waither/userservice/global/jwt/util/JwtUtil.java b/user-service/src/main/java/com/waither/userservice/global/jwt/util/JwtUtil.java index bfce4009..96b0b39f 100644 --- a/user-service/src/main/java/com/waither/userservice/global/jwt/util/JwtUtil.java +++ b/user-service/src/main/java/com/waither/userservice/global/jwt/util/JwtUtil.java @@ -4,7 +4,7 @@ import com.waither.userservice.global.jwt.execption.SecurityCustomException; import com.waither.userservice.global.jwt.execption.SecurityErrorCode; import com.waither.userservice.global.jwt.userdetails.PrincipalDetails; -import com.waither.userservice.global.util.RedisUtil; +import com.waither.userservice.util.RedisUtil; import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.MalformedJwtException; diff --git a/user-service/src/main/java/com/waither/userservice/service/commandService/SurveyService.java b/user-service/src/main/java/com/waither/userservice/service/commandService/SurveyService.java index 356dddce..bd5753bf 100644 --- a/user-service/src/main/java/com/waither/userservice/service/commandService/SurveyService.java +++ b/user-service/src/main/java/com/waither/userservice/service/commandService/SurveyService.java @@ -12,6 +12,7 @@ import com.waither.userservice.repository.SurveyRepository; import com.waither.userservice.repository.UserDataRepository; import com.waither.userservice.repository.UserMedianRepository; +import com.waither.userservice.util.RestClient; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.security.core.parameters.P; @@ -34,9 +35,11 @@ public class SurveyService { private final KafkaService kafkaService; + private final RestClient restClient; + @Transactional public void createSurvey(User user, SurveyReqDto.SurveyRequestDto surveyRequestDto) { - Double temp = getTemp(surveyRequestDto.time()); + Double temp = getTemp(surveyRequestDto.latitude(), surveyRequestDto.longitude(), surveyRequestDto.time()); Survey survey = SurveyConverter.toSurvey(surveyRequestDto, temp, getCurrentSeason()); survey.setUser(user); user.addSurvey(survey); @@ -74,9 +77,8 @@ private void updateUserMedian(UserData userData, UserMedian userMedian) { userMedianRepository.save(userMedian); } - // Todo: 해당 시각의 체감 온도 받아오기 (Weather-Service로 부터) - public Double getTemp(LocalDateTime time) { - return 18.0; + public Double getTemp(double latitude, double longitude, LocalDateTime time) { + return restClient.getTemperature(latitude, longitude, time); } public static Season getCurrentSeason() { diff --git a/user-service/src/main/java/com/waither/userservice/service/commandService/UserService.java b/user-service/src/main/java/com/waither/userservice/service/commandService/UserService.java index 17439bb2..c0ab1810 100644 --- a/user-service/src/main/java/com/waither/userservice/service/commandService/UserService.java +++ b/user-service/src/main/java/com/waither/userservice/service/commandService/UserService.java @@ -4,13 +4,12 @@ import com.waither.userservice.dto.request.UserReqDto; import com.waither.userservice.dto.response.KakaoResDto; import com.waither.userservice.entity.*; -import com.waither.userservice.entity.enums.Season; import com.waither.userservice.global.exception.CustomException; import com.waither.userservice.global.jwt.dto.JwtDto; import com.waither.userservice.global.jwt.userdetails.PrincipalDetails; import com.waither.userservice.global.jwt.util.JwtUtil; import com.waither.userservice.global.response.ErrorCode; -import com.waither.userservice.global.util.RedisUtil; +import com.waither.userservice.util.RedisUtil; import com.waither.userservice.kafka.KafkaConverter; import com.waither.userservice.kafka.KafkaDto; import com.waither.userservice.kafka.KafkaService; @@ -26,11 +25,8 @@ import java.security.SecureRandom; import java.util.*; import java.util.concurrent.TimeUnit; -import java.util.function.Function; import java.util.stream.Collectors; -import static com.waither.userservice.service.commandService.SurveyService.getCurrentSeason; - @Slf4j @RequiredArgsConstructor @Transactional diff --git a/user-service/src/main/java/com/waither/userservice/global/util/CalculateUtil.java b/user-service/src/main/java/com/waither/userservice/util/CalculateUtil.java similarity index 85% rename from user-service/src/main/java/com/waither/userservice/global/util/CalculateUtil.java rename to user-service/src/main/java/com/waither/userservice/util/CalculateUtil.java index 601f14ab..11b5633d 100644 --- a/user-service/src/main/java/com/waither/userservice/global/util/CalculateUtil.java +++ b/user-service/src/main/java/com/waither/userservice/util/CalculateUtil.java @@ -1,4 +1,4 @@ -package com.waither.userservice.global.util; +package com.waither.userservice.util; import lombok.AccessLevel; import lombok.NoArgsConstructor; diff --git a/user-service/src/main/java/com/waither/userservice/global/util/RedisUtil.java b/user-service/src/main/java/com/waither/userservice/util/RedisUtil.java similarity index 94% rename from user-service/src/main/java/com/waither/userservice/global/util/RedisUtil.java rename to user-service/src/main/java/com/waither/userservice/util/RedisUtil.java index 72e2a33a..99fa82f9 100644 --- a/user-service/src/main/java/com/waither/userservice/global/util/RedisUtil.java +++ b/user-service/src/main/java/com/waither/userservice/util/RedisUtil.java @@ -1,4 +1,4 @@ -package com.waither.userservice.global.util; +package com.waither.userservice.util; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; diff --git a/user-service/src/main/java/com/waither/userservice/util/RestClient.java b/user-service/src/main/java/com/waither/userservice/util/RestClient.java new file mode 100644 index 00000000..4a1d58bd --- /dev/null +++ b/user-service/src/main/java/com/waither/userservice/util/RestClient.java @@ -0,0 +1,45 @@ +package com.waither.userservice.util; + +import com.waither.userservice.global.exception.CustomException; +import com.waither.userservice.global.response.ApiResponse; +import com.waither.userservice.global.response.ErrorCode; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.HttpStatusCode; +import org.springframework.stereotype.Component; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.core.publisher.Mono; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; + +@Component +public class RestClient { + public static final String WEATHER_SERVICE_URL = "http://localhost:8081"; + + private final WebClient webClient; + + public RestClient(WebClient.Builder webClientBuilder) { + this.webClient = webClientBuilder.baseUrl(WEATHER_SERVICE_URL).build(); + } + + public Double getTemperature(double latitude, double longitude, LocalDateTime time) { + ApiResponse response = webClient.get() + .uri(uriBuilder -> uriBuilder + .path("/weather/temperature") + .queryParam("latitude", latitude) + .queryParam("longitude", longitude) + .queryParam("baseTime", time.format(DateTimeFormatter.ISO_DATE_TIME)) + .build()) + .retrieve() + .onStatus(HttpStatusCode::isError, clientResponse -> + Mono.error(new CustomException(ErrorCode.INTERNAL_SERVER_ERROR_500))) + .bodyToMono(new ParameterizedTypeReference>() {}) + .block(); + + if (response != null && "COMMON200".equals(response.getCode())) { + return Double.parseDouble(response.getResult()); + } else { + throw new CustomException(ErrorCode.INTERNAL_SERVER_ERROR_500); + } + } +}