From ab5b4ea41c8b539f7dd6810209f289bc35d07602 Mon Sep 17 00:00:00 2001 From: Yujin Date: Tue, 10 Oct 2023 21:24:15 +0900 Subject: [PATCH 1/7] =?UTF-8?q?feat:=20dto=EB=A7=90=EA=B3=A0=20=EC=97=94?= =?UTF-8?q?=ED=8B=B0=ED=8B=B0=EB=A5=BC=20=EB=B0=98=ED=99=98=ED=95=98?= =?UTF-8?q?=EB=8F=84=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/umc/yourweather/repository/WeatherRepository.java | 4 +--- .../yourweather/repository/impl/WeatherRepositoryImpl.java | 4 +--- .../umc/yourweather/repository/jpa/WeatherJpaRepository.java | 3 +-- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/umc/yourweather/repository/WeatherRepository.java b/src/main/java/com/umc/yourweather/repository/WeatherRepository.java index aecce4c..5b151c8 100644 --- a/src/main/java/com/umc/yourweather/repository/WeatherRepository.java +++ b/src/main/java/com/umc/yourweather/repository/WeatherRepository.java @@ -1,9 +1,7 @@ package com.umc.yourweather.repository; -import com.umc.yourweather.auth.CustomUserDetails; import com.umc.yourweather.domain.entity.User; import com.umc.yourweather.domain.entity.Weather; -import com.umc.yourweather.response.WeatherItemResponseDto; import java.time.LocalDate; import java.util.List; @@ -19,7 +17,7 @@ public interface WeatherRepository { Optional findByDate(LocalDate localDate); - List findByMonthAndUser(User user, LocalDate startDate, LocalDate endDate); + List findByMonthAndUser(User user, LocalDate startDate, LocalDate endDate); Weather save(Weather weather); diff --git a/src/main/java/com/umc/yourweather/repository/impl/WeatherRepositoryImpl.java b/src/main/java/com/umc/yourweather/repository/impl/WeatherRepositoryImpl.java index c7fe69e..de7941f 100644 --- a/src/main/java/com/umc/yourweather/repository/impl/WeatherRepositoryImpl.java +++ b/src/main/java/com/umc/yourweather/repository/impl/WeatherRepositoryImpl.java @@ -1,11 +1,9 @@ package com.umc.yourweather.repository.impl; -import com.umc.yourweather.auth.CustomUserDetails; import com.umc.yourweather.domain.entity.User; import com.umc.yourweather.domain.entity.Weather; import com.umc.yourweather.repository.WeatherRepository; import com.umc.yourweather.repository.jpa.WeatherJpaRepository; -import com.umc.yourweather.response.WeatherItemResponseDto; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Repository; @@ -43,7 +41,7 @@ public Optional findByDate(LocalDate localDate) { } @Override - public List findByMonthAndUser(User user, LocalDate startDate, LocalDate endDate) { + public List findByMonthAndUser(User user, LocalDate startDate, LocalDate endDate) { return weatherJpaRepository.findByMonthAndUser(user, startDate, endDate); } diff --git a/src/main/java/com/umc/yourweather/repository/jpa/WeatherJpaRepository.java b/src/main/java/com/umc/yourweather/repository/jpa/WeatherJpaRepository.java index 9c5b210..bab24f4 100644 --- a/src/main/java/com/umc/yourweather/repository/jpa/WeatherJpaRepository.java +++ b/src/main/java/com/umc/yourweather/repository/jpa/WeatherJpaRepository.java @@ -2,7 +2,6 @@ import com.umc.yourweather.domain.entity.User; import com.umc.yourweather.domain.entity.Weather; -import com.umc.yourweather.response.WeatherItemResponseDto; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -27,7 +26,7 @@ public interface WeatherJpaRepository extends JpaRepository { + "w.date >= :startDate AND " + "w.date <= :endDate " + "ORDER BY w.date asc") - List findByMonthAndUser( + List findByMonthAndUser( @Param("user") User user, @Param("startDate") LocalDate startDate, @Param("endDate") LocalDate endDate); From 5607995fdb4f6b1925b289f75194ce3b2ae368e3 Mon Sep 17 00:00:00 2001 From: Yujin Date: Tue, 10 Oct 2023 21:25:08 +0900 Subject: [PATCH 2/7] =?UTF-8?q?feat:=20jpa=20=EB=A9=94=EC=86=8C=EB=93=9C?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/umc/yourweather/repository/MemoRepository.java | 2 ++ .../umc/yourweather/repository/impl/MemoRepositoryImpl.java | 5 +++++ .../umc/yourweather/repository/jpa/MemoJpaRepository.java | 2 ++ 3 files changed, 9 insertions(+) diff --git a/src/main/java/com/umc/yourweather/repository/MemoRepository.java b/src/main/java/com/umc/yourweather/repository/MemoRepository.java index 402e5d8..a00e691 100644 --- a/src/main/java/com/umc/yourweather/repository/MemoRepository.java +++ b/src/main/java/com/umc/yourweather/repository/MemoRepository.java @@ -28,4 +28,6 @@ List findSpecificMemoList(User user, Status status, LocalDateTime startDat List findByUserAndWeatherId(User user, Weather weather); List findByWeatherId(Weather weather); + + Memo findFirstByWeatherIdOrderByTemperatureDesc(Long weatherId); } diff --git a/src/main/java/com/umc/yourweather/repository/impl/MemoRepositoryImpl.java b/src/main/java/com/umc/yourweather/repository/impl/MemoRepositoryImpl.java index cb0f6a8..3a308cb 100644 --- a/src/main/java/com/umc/yourweather/repository/impl/MemoRepositoryImpl.java +++ b/src/main/java/com/umc/yourweather/repository/impl/MemoRepositoryImpl.java @@ -66,6 +66,11 @@ public List findByWeatherId(Weather weather) { return memoJpaRepository.findByWeatherId(weather); } + @Override + public Memo findFirstByWeatherIdOrderByTemperatureDesc(Long weatherId) { + return memoJpaRepository.findFirstByWeatherIdOrderByTemperatureDesc(weatherId); + } + // public List findByDateAndUser(User user, LocalDate localDate) { // return memoJpaRepository.findByDateAndUser(user,localDate); // } diff --git a/src/main/java/com/umc/yourweather/repository/jpa/MemoJpaRepository.java b/src/main/java/com/umc/yourweather/repository/jpa/MemoJpaRepository.java index 6157d6a..cc28b70 100644 --- a/src/main/java/com/umc/yourweather/repository/jpa/MemoJpaRepository.java +++ b/src/main/java/com/umc/yourweather/repository/jpa/MemoJpaRepository.java @@ -61,4 +61,6 @@ List findByUserAndWeatherId( + "ORDER BY m.temperature asc") List findByWeatherId( @Param("weather") Weather weatherId); + + Memo findFirstByWeatherIdOrderByTemperatureDesc(Long weatherId); } From 8beab6eda263d6eed5fb1ed88e11fdb1773f42d2 Mon Sep 17 00:00:00 2001 From: Yujin Date: Tue, 10 Oct 2023 21:25:43 +0900 Subject: [PATCH 3/7] =?UTF-8?q?feat:=20Weather=20=EC=97=94=ED=8B=B0?= =?UTF-8?q?=ED=8B=B0=EC=97=90=20=EB=8C=80=ED=91=9C=20=EC=98=A8=EB=8F=84,?= =?UTF-8?q?=EC=83=81=ED=83=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../umc/yourweather/domain/entity/Weather.java | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/main/java/com/umc/yourweather/domain/entity/Weather.java b/src/main/java/com/umc/yourweather/domain/entity/Weather.java index 2073f80..58182b2 100644 --- a/src/main/java/com/umc/yourweather/domain/entity/Weather.java +++ b/src/main/java/com/umc/yourweather/domain/entity/Weather.java @@ -24,10 +24,6 @@ public class Weather { private Long id; private LocalDate date; - // 대표날씨 필드, 그 날씨의 온도 - @Enumerated(EnumType.STRING) - private Status lastStatus; - private int lastTemperature; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") @@ -37,17 +33,8 @@ public class Weather { List memos = new ArrayList<>(); @Builder - public Weather(LocalDate date, User user, Status lastStatus, int lastTemperature) { + public Weather(LocalDate date, User user) { this.date = date; this.user = user; - this.lastStatus = lastStatus; - this.lastTemperature = lastTemperature; - } - - public void update(Status lastStatus, int lastTemperature) { -// if (lastTemperature >= this.lastTemperature) { - this.lastStatus = lastStatus; - this.lastTemperature = lastTemperature; -// } } } \ No newline at end of file From 4674a0d540ebe255d5e41ab240d589913aec676c Mon Sep 17 00:00:00 2001 From: Yujin Date: Tue, 10 Oct 2023 21:26:16 +0900 Subject: [PATCH 4/7] =?UTF-8?q?feat:=20DTO=EC=97=90=EB=8A=94=20=EB=8C=80?= =?UTF-8?q?=ED=91=9C=20=EC=98=A8=EB=8F=84,=20=EC=83=81=ED=83=9C=20?= =?UTF-8?q?=EC=9E=88=EC=96=B4=EC=95=BC=ED=95=A8.=20=EA=B7=B8=EB=A6=AC?= =?UTF-8?q?=EA=B3=A0=20=EB=84=A4=EC=9D=B4=EB=B0=8D=20last=EC=97=90?= =?UTF-8?q?=EC=84=9C=20highest=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yourweather/response/WeatherItemResponseDto.java | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/umc/yourweather/response/WeatherItemResponseDto.java b/src/main/java/com/umc/yourweather/response/WeatherItemResponseDto.java index 3b6024d..0dda83b 100644 --- a/src/main/java/com/umc/yourweather/response/WeatherItemResponseDto.java +++ b/src/main/java/com/umc/yourweather/response/WeatherItemResponseDto.java @@ -14,15 +14,8 @@ public class WeatherItemResponseDto { private Long weatherId; //웨더 아이디 - private LocalDate date; - private Status lastStatus; - private int lastTemperature; - public WeatherItemResponseDto(Weather weather) { - this.weatherId = weather.getId(); - this.date = weather.getDate(); - this.lastStatus = weather.getLastStatus(); - this.lastTemperature = weather.getLastTemperature(); - } + private Status highestStatus; + private int highestTemperature; } From 1170a09ded17d7b81c7a823d82f5b734ed15845b Mon Sep 17 00:00:00 2001 From: Yujin Date: Tue, 10 Oct 2023 21:26:48 +0900 Subject: [PATCH 5/7] =?UTF-8?q?feat:=20=EB=A9=94=EB=AA=A8=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1=EC=8B=9C=20=EB=8C=80=ED=91=9C=EA=B0=92=20=EA=B0=B1?= =?UTF-8?q?=EC=8B=A0=20=EB=A1=9C=EC=A7=81=20=EB=B9=BC=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/umc/yourweather/service/MemoService.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/main/java/com/umc/yourweather/service/MemoService.java b/src/main/java/com/umc/yourweather/service/MemoService.java index d9276b0..0924143 100644 --- a/src/main/java/com/umc/yourweather/service/MemoService.java +++ b/src/main/java/com/umc/yourweather/service/MemoService.java @@ -41,14 +41,11 @@ public Optional write(MemoRequestDto memoRequestDto, CustomUser User user = userDetails.getUser(); // weather 찾아보고 만약 없으면 새로 등록해줌. - // 새로 등록할 때에 최신 날씨, 최신 온도도 같이 필드에 추가 Weather weather = weatherRepository.findByDateAndUser(date, user) .orElseGet(() -> { Weather newWeather = Weather.builder() .user(user) .date(date) - .lastStatus(memoRequestDto.getStatus()) - .lastTemperature(memoRequestDto.getTemperature()) .build(); return weatherRepository.save(newWeather); @@ -59,13 +56,6 @@ public Optional write(MemoRequestDto memoRequestDto, CustomUser return Optional.empty(); } - //처음에 last에 대한 정보가 생기고 바로 update를 호출하는게 조금 마음에 걸리긴하지만.. 일단 패스~ - - //최고온도 update 함수의 조건을 여기서 넣어야함. update 함수를 재활용하기 위해서 - if (memoRequestDto.getTemperature() >= weather.getLastTemperature()) { - weather.update(memoRequestDto.getStatus(), memoRequestDto.getTemperature()); - } - // MemoRequestDto에 넘어온 정보를 토대로 Memo 객체 생성 Memo memo = Memo.builder() .weather(weather) From ad86ef9da118de98ed6773cf00eb21eab6ade6ee Mon Sep 17 00:00:00 2001 From: Yujin Date: Tue, 10 Oct 2023 21:27:31 +0900 Subject: [PATCH 6/7] =?UTF-8?q?feat:=20findFirstByWeatherIdOrderByTemperat?= =?UTF-8?q?ureDesc=EB=A1=9C=20=EC=B0=BE=EC=9D=80=20highest=20=EB=A9=94?= =?UTF-8?q?=EB=AA=A8=EB=A5=BC=20=EA=B8=B0=EB=B0=98=EC=9C=BC=EB=A1=9C=20DTO?= =?UTF-8?q?=EC=97=90=20=EB=84=A3=EA=B8=B0!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yourweather/service/WeatherService.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/umc/yourweather/service/WeatherService.java b/src/main/java/com/umc/yourweather/service/WeatherService.java index 8b19c2c..20d2109 100644 --- a/src/main/java/com/umc/yourweather/service/WeatherService.java +++ b/src/main/java/com/umc/yourweather/service/WeatherService.java @@ -6,6 +6,7 @@ import com.umc.yourweather.domain.entity.User; import com.umc.yourweather.domain.entity.Weather; import com.umc.yourweather.exception.WeatherNotFoundException; +import com.umc.yourweather.repository.MemoRepository; import com.umc.yourweather.response.*; import com.umc.yourweather.repository.WeatherRepository; @@ -24,6 +25,7 @@ public class WeatherService { private final WeatherRepository weatherRepository; + private final MemoRepository memoRepository; @Transactional @@ -113,8 +115,6 @@ public void update(Long weatherId){ memoWithHighestTemperature = tmpMemo; // Update if a memo with higher temperature is found } } - - weather.update(memoWithHighestTemperature.getStatus(),memoWithHighestTemperature.getTemperature()); } @Transactional(readOnly = true) @@ -123,10 +123,23 @@ public WeatherMonthlyResponseDto getMonthlyList(int year, int month, CustomUserD LocalDate startDate = LocalDate.of(year, month, 1); LocalDate lastDate = startDate.withDayOfMonth(startDate.lengthOfMonth()); - List weatherList = weatherRepository.findByMonthAndUser(userDetails.getUser(), startDate, lastDate); // User 파라미터를 추가해야 함 - //.orElseThrow(() -> new WeatherNotFoundException("해당 아이디로 조회되는 날씨 객체가 존재하지 않습니다.")); + List weatherItemResponseDtoList = new ArrayList<>(); + List weatherList = weatherRepository.findByMonthAndUser(userDetails.getUser(), startDate, lastDate); - WeatherMonthlyResponseDto result = new WeatherMonthlyResponseDto(weatherList); - return result; + for(Weather weather : weatherList){ + + Memo highestMemo = memoRepository.findFirstByWeatherIdOrderByTemperatureDesc(weather.getId()); + WeatherItemResponseDto weatherItemResponseDto = WeatherItemResponseDto.builder() + .weatherId(weather.getId()) + .date(weather.getDate()) + .highestStatus(highestMemo.getStatus()) + .highestTemperature(highestMemo.getTemperature()) + .build(); + + weatherItemResponseDtoList.add(weatherItemResponseDto); + } + + + return new WeatherMonthlyResponseDto(weatherItemResponseDtoList); } } \ No newline at end of file From 120da239bdec5eb4d54d136611f76183c61b211d Mon Sep 17 00:00:00 2001 From: Yujin Date: Tue, 10 Oct 2023 22:15:15 +0900 Subject: [PATCH 7/7] =?UTF-8?q?fix:=20cors=20=EC=84=A4=EC=A0=95=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yourweather/config/SecurityConfig.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/java/com/umc/yourweather/config/SecurityConfig.java b/src/main/java/com/umc/yourweather/config/SecurityConfig.java index 8229fad..be98abd 100644 --- a/src/main/java/com/umc/yourweather/config/SecurityConfig.java +++ b/src/main/java/com/umc/yourweather/config/SecurityConfig.java @@ -27,6 +27,11 @@ import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.authentication.logout.LogoutFilter; +import org.springframework.web.cors.CorsConfiguration; +import org.springframework.web.cors.CorsConfigurationSource; +import org.springframework.web.cors.UrlBasedCorsConfigurationSource; + +import java.util.List; @Configuration @EnableWebSecurity @@ -72,9 +77,23 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http.addFilterAfter(customLoginFilter(), CustomOAuthLoginFilter.class); http.addFilterBefore(jwtAuthenticationFilter(), CustomLoginFilter.class); + http.cors(httpSecurityCorsConfigurer -> corsConfigurationSource()); + return http.build(); } + @Bean + CorsConfigurationSource corsConfigurationSource() { + CorsConfiguration configuration = new CorsConfiguration(); + configuration.setAllowedOrigins(List.of("*")); + configuration.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS")); + configuration.setAllowedHeaders(List.of("*")); + configuration.setExposedHeaders(List.of("*")); + UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); + source.registerCorsConfiguration("/**", configuration); + return source; + } + @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder();