Skip to content

Commit

Permalink
✨ feat: 메인 화면 Api 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
seheonnn authored May 13, 2024
2 parents ebb4399 + 72cd602 commit 0259c13
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
package com.waither.weatherservice.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.waither.weatherservice.dto.request.GetWeatherRequest;
import com.waither.weatherservice.dto.response.MainWeatherResponse;
import com.waither.weatherservice.response.ApiResponse;
import com.waither.weatherservice.service.WeatherService;

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/weather")
public class WeatherController {

private final WeatherService weatherService;

@PostMapping("/main")
public ApiResponse<MainWeatherResponse> getMainWeather(@RequestBody GetWeatherRequest getWeatherRequest) {
return ApiResponse.onSuccess(
weatherService.getMainWeather(getWeatherRequest.latitude(), getWeatherRequest.longitude()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/weather")
@RequestMapping("/api/v1/weather-test")
public class WeatherTestController {

private final WeatherService weatherService;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.waither.weatherservice.dto.request;

public record GetWeatherRequest(
double latitude,
double longitude
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.waither.weatherservice.dto.response;

import java.util.List;

import lombok.Builder;

@Builder
public record MainWeatherResponse(

String pop,
String tempMin,
String tempMax,
String humidity,
String windVector,
String windDegree,
// 예상 기온
List<String> expectedTemp,

// 예상 강수량
List<String> expectedRain,

// 예상 강수 형태
List<String> expectedPty,

// 예상 하늘 상태
List<String> expectedSky
) {

public static MainWeatherResponse from(
String pop,
String tempMin,
String tempMax,
String humidity,
String windVector,
String windDegree,
// 예상 기온
List<String> expectedTemp,

// 예상 강수량
List<String> expectedRain,

// 예상 강수 형태
List<String> expectedPty,

// 예상 하늘 상태
List<String> expectedSky
) {
return MainWeatherResponse.builder()
.pop(pop)
.tempMin(tempMin)
.tempMax(tempMax)
.humidity(humidity)
.windVector(windVector)
.windDegree(windDegree)
.expectedTemp(expectedTemp)
.expectedRain(expectedRain)
.expectedPty(expectedPty)
.expectedSky(expectedSky)
.build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@RedisHash(value = "DailyWeather", timeToLive = 28800L) // 유효시간: 8시간
public class DailyWeather {

@Id
private String key;
private String id;

// 강수확률 (%)
private String pop;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@RedisHash(value = "DisasterMessage", timeToLive = 86400L) // 유효시간: 24시간
public class DisasterMessage {

@Id
private String key;
private String id;
private String message;

public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@RedisHash(value = "ExpectedWeather", timeToLive = 21600L) // 유효시간: 6시간
public class ExpectedWeather {

@Id
private String key;
private String id;

// 예상 기온
private List<String> expectedTemp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
public enum WeatherErrorCode implements BaseErrorCode {

WEATHER_EXAMPLE_ERROR(HttpStatus.BAD_REQUEST, "WEAT4000", "날씨 에러입니다."),
WEATHER_OPENAPI_ERROR(HttpStatus.BAD_REQUEST, "WEAT4001", "OpenApi 관련 오류입니다.");
WEATHER_OPENAPI_ERROR(HttpStatus.BAD_REQUEST, "WEAT4010", "OpenApi 관련 오류입니다."),
WEATHER_MAIN_ERROR(HttpStatus.BAD_REQUEST, "WEAT4020", "잘못된 위도, 경도입니다."); // 레디스에 캐싱 데이터가 없는 경우

private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@

import java.io.IOException;
import java.net.URISyntaxException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.waither.weatherservice.dto.response.MainWeatherResponse;
import com.waither.weatherservice.entity.DailyWeather;
import com.waither.weatherservice.entity.DisasterMessage;
import com.waither.weatherservice.entity.ExpectedWeather;
import com.waither.weatherservice.exception.WeatherExceptionHandler;
import com.waither.weatherservice.gps.GpsTransfer;
import com.waither.weatherservice.gps.LatXLngY;
import com.waither.weatherservice.kafka.Producer;
import com.waither.weatherservice.openapi.ForeCastOpenApiResponse;
import com.waither.weatherservice.openapi.MsgOpenApiResponse;
import com.waither.weatherservice.openapi.OpenApiUtil;
import com.waither.weatherservice.redis.DailyWeatherRepository;
import com.waither.weatherservice.redis.DisasterMessageRepository;
import com.waither.weatherservice.redis.ExpectedWeatherRepository;
import com.waither.weatherservice.response.WeatherErrorCode;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -33,6 +40,7 @@ public class WeatherService {
private final ExpectedWeatherRepository expectedWeatherRepository;
private final DisasterMessageRepository disasterMessageRepository;
private final Producer producer;
private final GpsTransfer gpsTransfer;

public void createExpectedWeather(
int nx,
Expand All @@ -54,7 +62,7 @@ public void createExpectedWeather(
String key = item.getNx() + "_" + item.getNy() + "_" + item.getFcstDate() + "_" + item.getFcstTime();

ExpectedWeather expectedWeather = ExpectedWeather.builder()
.key(key)
.id(key)
.expectedTemp(expectedTempList)
.expectedRain(expectedRainList)
.expectedPty(expectedPtyList)
Expand Down Expand Up @@ -85,7 +93,7 @@ public void createDailyWeather(int nx,
String key = item.getNx() + "_" + item.getNy() + "_" + item.getFcstDate() + "_" + item.getFcstTime();

DailyWeather dailyWeather = DailyWeather.builder()
.key(key)
.id(key)
.pop(pop)
.tempMin(tmn)
.tempMax(tmx)
Expand All @@ -99,8 +107,8 @@ public void createDailyWeather(int nx,
// 바람 세기 Kafka 전송
producer.produceMessage(wsd);

// DailyWeather save = dailyWeatherRepository.save(dailyWeather);
log.info("[*] 하루 온도 : {}", dailyWeather);
DailyWeather save = dailyWeatherRepository.save(dailyWeather);
log.info("[*] 하루 온도 : {}", save);

}

Expand All @@ -113,7 +121,7 @@ public void createDisasterMsg(String location) throws URISyntaxException, IOExce

String key = location + "_" + createDate;
DisasterMessage disasterMessage = DisasterMessage.builder()
.key(key)
.id(key)
.message(msg)
.build();

Expand All @@ -128,4 +136,44 @@ public void createAirKorea(String searchTime) throws URISyntaxException {
public void convertLocation(double latitude, double longitude) throws URISyntaxException, JsonProcessingException {
openApiUtil.callAccuweatherLocationApi(latitude, longitude);
}

public MainWeatherResponse getMainWeather(double latitude, double longitude) {
LatXLngY latXLngY = gpsTransfer.convertGpsToGrid(latitude, longitude);

LocalDateTime now = LocalDateTime.now();
String key = (int)latXLngY.x() + "_" + (int)latXLngY.y() + "_" + convertLocalDateTimeToString(now);

// 테스트 키 : "55_127_20240508_1500"

DailyWeather dailyWeather = dailyWeatherRepository.findById(key)
.orElseThrow(() -> new WeatherExceptionHandler(WeatherErrorCode.WEATHER_MAIN_ERROR));

ExpectedWeather expectedWeather = expectedWeatherRepository.findById(key)
.orElseThrow(() -> new WeatherExceptionHandler(WeatherErrorCode.WEATHER_MAIN_ERROR));

MainWeatherResponse weatherMainResponse = MainWeatherResponse.from(
dailyWeather.getPop(), dailyWeather.getTempMin(),
dailyWeather.getTempMax(), dailyWeather.getHumidity(),
dailyWeather.getWindVector(), dailyWeather.getWindDegree(),
expectedWeather.getExpectedTemp(),
expectedWeather.getExpectedRain(), expectedWeather.getExpectedPty(),
expectedWeather.getExpectedSky()
);
log.info("{}", weatherMainResponse);

return weatherMainResponse;
}

public String convertLocalDateTimeToString(LocalDateTime time) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDateTime = time.format(formatter);

String[] lst = formattedDateTime.split(" ");
String baseDate = lst[0].replace("-", "");

String[] temp = lst[1].split(":");
String baseTime = temp[0] + "00";

return baseDate + "_" + baseTime;
}
}

0 comments on commit 0259c13

Please sign in to comment.