Skip to content

Commit

Permalink
✨ feature: Weather-Service로 부터 해당 시각의 체감 온도 받아오는 메서드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
jinho7 committed Jul 18, 2024
1 parent d20f14d commit 59ef17b
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

public class SurveyReqDto {

// Todo : DTO에서 날짜/시간 관련 필드는 String 타입으로 선언하고, 서비스 계층에서 원하는 형식으로 파싱하는 것이 좋은가 고민중.
public record SurveyRequestDto(

Double latitude,
Double longitude,
// level
Integer ans,
LocalDateTime time
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
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.*;
import lombok.*;

import java.util.List;

import static com.waither.userservice.global.util.CalculateUtil.calculateMedian;
import static com.waither.userservice.util.CalculateUtil.calculateMedian;

@Builder
@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.waither.userservice.global.util;
package com.waither.userservice.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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<ApiResponse<String>>() {})
.block();

if (response != null && "COMMON200".equals(response.getCode())) {
return Double.parseDouble(response.getResult());
} else {
throw new CustomException(ErrorCode.INTERNAL_SERVER_ERROR_500);
}
}
}

0 comments on commit 59ef17b

Please sign in to comment.