Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] 메모 수정/삭제 시, 캘린더에 반영 안되는 것 수정 #130

Merged
merged 7 commits into from
Oct 10, 2023
19 changes: 19 additions & 0 deletions src/main/java/com/umc/yourweather/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
15 changes: 1 addition & 14 deletions src/main/java/com/umc/yourweather/domain/entity/Weather.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -37,17 +33,8 @@ public class Weather {
List<Memo> 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;
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ List<Memo> findSpecificMemoList(User user, Status status, LocalDateTime startDat
List<Memo> findByUserAndWeatherId(User user, Weather weather);

List<Memo> findByWeatherId(Weather weather);

Memo findFirstByWeatherIdOrderByTemperatureDesc(Long weatherId);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,7 +17,7 @@ public interface WeatherRepository {

Optional<Weather> findByDate(LocalDate localDate);

List<WeatherItemResponseDto> findByMonthAndUser(User user, LocalDate startDate, LocalDate endDate);
List<Weather> findByMonthAndUser(User user, LocalDate startDate, LocalDate endDate);

Weather save(Weather weather);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public List<Memo> findByWeatherId(Weather weather) {
return memoJpaRepository.findByWeatherId(weather);
}

@Override
public Memo findFirstByWeatherIdOrderByTemperatureDesc(Long weatherId) {
return memoJpaRepository.findFirstByWeatherIdOrderByTemperatureDesc(weatherId);
}

// public List<MemoItemResponseDto> findByDateAndUser(User user, LocalDate localDate) {
// return memoJpaRepository.findByDateAndUser(user,localDate);
// }
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,7 +41,7 @@ public Optional<Weather> findByDate(LocalDate localDate) {
}

@Override
public List<WeatherItemResponseDto> findByMonthAndUser(User user, LocalDate startDate, LocalDate endDate) {
public List<Weather> findByMonthAndUser(User user, LocalDate startDate, LocalDate endDate) {
return weatherJpaRepository.findByMonthAndUser(user, startDate, endDate);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ List<Memo> findByUserAndWeatherId(
+ "ORDER BY m.temperature asc")
List<Memo> findByWeatherId(
@Param("weather") Weather weatherId);

Memo findFirstByWeatherIdOrderByTemperatureDesc(Long weatherId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +26,7 @@ public interface WeatherJpaRepository extends JpaRepository<Weather, Long> {
+ "w.date >= :startDate AND "
+ "w.date <= :endDate "
+ "ORDER BY w.date asc")
List<WeatherItemResponseDto> findByMonthAndUser(
List<Weather> findByMonthAndUser(
@Param("user") User user,
@Param("startDate") LocalDate startDate,
@Param("endDate") LocalDate endDate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
10 changes: 0 additions & 10 deletions src/main/java/com/umc/yourweather/service/MemoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,11 @@ public Optional<MemoResponseDto> 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);
Expand All @@ -59,13 +56,6 @@ public Optional<MemoResponseDto> 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)
Expand Down
25 changes: 19 additions & 6 deletions src/main/java/com/umc/yourweather/service/WeatherService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,6 +25,7 @@
public class WeatherService {

private final WeatherRepository weatherRepository;
private final MemoRepository memoRepository;


@Transactional
Expand Down Expand Up @@ -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)
Expand All @@ -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<WeatherItemResponseDto> weatherList = weatherRepository.findByMonthAndUser(userDetails.getUser(), startDate, lastDate); // User 파라미터를 추가해야 함
//.orElseThrow(() -> new WeatherNotFoundException("해당 아이디로 조회되는 날씨 객체가 존재하지 않습니다."));
List<WeatherItemResponseDto> weatherItemResponseDtoList = new ArrayList<>();
List<Weather> 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);
}
}