Skip to content

Commit

Permalink
Feat: 날씨 정보에 오늘 Main날씨 상태 추가 (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdgns8439 authored May 20, 2024
1 parent 77f78f7 commit 64071c1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.tripy.domain.wheather;

import com.example.tripy.domain.wheather.dto.WheatherResponseDto.WhetherResponseInfo;
import com.example.tripy.domain.wheather.dto.WeatherResponseDto.WhetherResponseInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.example.tripy.domain.wheather;

import jakarta.persistence.Tuple;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;
import java.util.Objects;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

Expand All @@ -24,12 +24,12 @@ public interface WeatherRepository extends JpaRepository<Weather, Long> {
List<Object[]> findAllByCityNameAndByDateTime(String cityName);


@Query("SELECT w.temp " +
@Query("SELECT w.temp as temp,w.weatherMain as weatherMain " +
"FROM Weather w " +
"WHERE w.cityName = :cityName " +
"AND w.weatherDate = :currentDate " +
"AND w.weatherTime <= :currentTime " +
"ORDER BY w.weatherDate DESC " +
"LIMIT 1")
double findClosestTemperature(String cityName, LocalDate currentDate, LocalTime currentTime);
Tuple findClosestTemperature(String cityName, LocalDate currentDate, LocalTime currentTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@

import com.example.tripy.domain.city.City;
import com.example.tripy.domain.city.CityRepository;
import com.example.tripy.domain.wheather.dto.WheatherResponseDto.WhetherResponseInfo;
import com.example.tripy.domain.wheather.dto.WheatherResponseDto.WhetherResponseSimpleInfo;
import com.example.tripy.domain.wheather.dto.WeatherResponseDto.WhetherResponseInfo;
import com.example.tripy.domain.wheather.dto.WeatherResponseDto.WhetherResponseSimpleInfo;
import com.example.tripy.global.common.response.code.status.ErrorStatus;
import com.example.tripy.global.common.response.exception.GeneralException;
import jakarta.persistence.Tuple;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.swing.plaf.IconUIResource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -41,9 +38,11 @@ public WhetherResponseInfo getWeatherList(Long cityId) {
LocalDate nowDate = LocalDate.now();
LocalTime nowTime = LocalTime.now();

double temp = weatherRepository.findClosestTemperature(
Tuple curInfo = weatherRepository.findClosestTemperature(
city.getName(), nowDate, nowTime);
Float temp = (Float) curInfo.get("temp");
String weatherMain = curInfo.get("weatherMain").toString();

return WhetherResponseInfo.toDto(temp, whetherResponseSimpleInfoList);
return WhetherResponseInfo.toDto(temp, weatherMain, whetherResponseSimpleInfoList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
import lombok.NoArgsConstructor;


public class WheatherResponseDto {
public class WeatherResponseDto {
private static final int CITY_NAME = 0;
private static final int TEMP_MAX = 1;
private static final int TEMP_MIN = 2;
private static final int WEATHER_DATE = 3;


@Getter
Expand All @@ -26,10 +30,10 @@ public static class WhetherResponseSimpleInfo{

public static WhetherResponseSimpleInfo toDto(Object[] object) {
return WhetherResponseSimpleInfo.builder()
.cityName(object[0].toString())
.tempMax(Double.valueOf(object[1].toString()))
.tempMin(Double.valueOf(object[2].toString()))
.weatherDate(object[3].toString())
.cityName(object[CITY_NAME].toString())
.tempMax(Double.valueOf(object[TEMP_MAX].toString()))
.tempMin(Double.valueOf(object[TEMP_MIN].toString()))
.weatherDate(object[WEATHER_DATE].toString())
.build();
}
}
Expand All @@ -40,11 +44,12 @@ public static WhetherResponseSimpleInfo toDto(Object[] object) {
@Builder
public static class WhetherResponseInfo {
Number currentTemp;
// String weatherMain;
String weatherMain;
List<WhetherResponseSimpleInfo> whetherResponseSimpleInfoList;
public static WhetherResponseInfo toDto(Double temp, List<WhetherResponseSimpleInfo> whetherResponseSimpleInfoList) {
public static WhetherResponseInfo toDto(Float temp, String weatherMain, List<WhetherResponseSimpleInfo> whetherResponseSimpleInfoList) {
return WhetherResponseInfo.builder()
.currentTemp(temp)
.weatherMain(weatherMain)
.whetherResponseSimpleInfoList(whetherResponseSimpleInfoList)
.build();
}
Expand Down

0 comments on commit 64071c1

Please sign in to comment.