Skip to content

Commit

Permalink
Merge pull request #128 from yourweather/127-hotfix-memo-delete-not-r…
Browse files Browse the repository at this point in the history
…eflection-monthly

feat: 메모 삭제시 웨더의 최고 기온 update 진행
  • Loading branch information
HI-JIN2 authored Aug 23, 2023
2 parents 9ae05e8 + ae56767 commit f6fed99
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public ResponseDto<MemoUpdateResponseDto> update(@PathVariable Long memoId,
@DeleteMapping("/delete/{memoId}")
public ResponseDto<Void> delete(@PathVariable Long memoId) {
Long weatherId = memoService.delete(memoId);
weatherService.update(weatherId);
String comment = weatherService.checkMemoAndDelete(weatherId);
return ResponseDto.success("메모 삭제 완료" + comment);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/umc/yourweather/domain/entity/Weather.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public Weather(LocalDate date, User user, Status lastStatus, int lastTemperature
}

public void update(Status lastStatus, int lastTemperature) {
if (lastTemperature >= this.lastTemperature) {
this.lastStatus = lastStatus;
this.lastTemperature = lastTemperature;
}
// if (lastTemperature >= this.lastTemperature) {
this.lastStatus = lastStatus;
this.lastTemperature = lastTemperature;
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import com.umc.yourweather.domain.entity.User;
import com.umc.yourweather.domain.entity.Weather;
import com.umc.yourweather.domain.enums.Status;
import com.umc.yourweather.response.MemoItemResponseDto;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
Expand All @@ -28,4 +26,6 @@ List<Memo> findSpecificMemoList(User user, Status status, LocalDateTime startDat
// List<MemoItemResponseDto> findByDateAndUser(User user, LocalDate localDate);

List<Memo> findByUserAndWeatherId(User user, Weather weather);

List<Memo> findByWeatherId(Weather weather);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public interface WeatherRepository {
void delete(Weather weather);

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

Optional<Weather> findByDateAtHighestTemperature(LocalDate localDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public List<Memo> findByUserAndWeatherId(User user, Weather weather) {
return memoJpaRepository.findByUserAndWeatherId(user, weather);
}

@Override
public List<Memo> findByWeatherId(Weather weather) {
return memoJpaRepository.findByWeatherId(weather);
}

// 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
Expand Up @@ -57,4 +57,10 @@ public List<WeatherItemResponseDto> findByMonthAndUser(User user, LocalDate star
public List<Weather> findWeatherByDateBetweenAndUser(LocalDate oneWeekAgo, LocalDate current, User user) {
return weatherJpaRepository.findWeatherByDateBetweenAndUser(oneWeekAgo, current, user);
}

@Override
public Optional<Weather> findByDateAtHighestTemperature(LocalDate localDate) {
return weatherJpaRepository.findByDateAtHighestTemperature(localDate);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,13 @@ List<Memo> findSpecificMemoList(
List<Memo> findByUserAndWeatherId(
@Param("user") User user,
@Param("weather") Weather weatherId);

@Query("SELECT m FROM Memo m "
+ "JOIN FETCH m.weather w "
+ "JOIN FETCH w.user u "
+ "WHERE "
+ "m.weather = :weather "
+ "ORDER BY m.temperature asc")
List<Memo> findByWeatherId(
@Param("weather") Weather weatherId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@ List<WeatherItemResponseDto> findByMonthAndUser(
@Param("endDate") LocalDate endDate);

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

@Query("SELECT w FROM Weather w "
+ "JOIN FETCH w.user u "
+ "WHERE "
+ "u = :user AND "
+ "w.date = :localDate "
+ "ORDER BY w.date asc")
Optional<Weather> findByDateAtHighestTemperature(
@Param("localDate") LocalDate localDate);
}
16 changes: 11 additions & 5 deletions src/main/java/com/umc/yourweather/service/MemoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import com.umc.yourweather.request.MemoUpdateRequestDto;
import com.umc.yourweather.response.*;
import jakarta.persistence.EntityNotFoundException;

import java.util.Optional;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -58,7 +60,11 @@ public Optional<MemoResponseDto> write(MemoRequestDto memoRequestDto, CustomUser
}

//처음에 last에 대한 정보가 생기고 바로 update를 호출하는게 조금 마음에 걸리긴하지만.. 일단 패스~
weather.update(memoRequestDto.getStatus(), memoRequestDto.getTemperature());

//최고온도 update 함수의 조건을 여기서 넣어야함. update 함수를 재활용하기 위해서
if (memoRequestDto.getTemperature() >= weather.getLastTemperature()) {
weather.update(memoRequestDto.getStatus(), memoRequestDto.getTemperature());
}

// MemoRequestDto에 넘어온 정보를 토대로 Memo 객체 생성
Memo memo = Memo.builder()
Expand All @@ -72,9 +78,9 @@ public Optional<MemoResponseDto> write(MemoRequestDto memoRequestDto, CustomUser
memoRepository.save(memo);
return Optional.of(
MemoResponseDto.builder()
.memo(memo)
.weatherId(weather.getId())
.build());
.memo(memo)
.weatherId(weather.getId())
.build());
}

@Transactional
Expand All @@ -95,9 +101,9 @@ public MemoUpdateResponseDto update(Long memoId, MemoUpdateRequestDto requestDto
public Long delete(Long memoId) {
Memo memo = memoRepository.findById(memoId)
.orElseThrow(() -> new MemoNotFoundException("해당 메모가 없습니다. id =" + memoId));
memoRepository.delete(memo);

Long weatherId = memo.getWeather().getId();
memoRepository.delete(memo);

return weatherId;
}
Expand Down
39 changes: 24 additions & 15 deletions src/main/java/com/umc/yourweather/service/WeatherService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import com.umc.yourweather.exception.WeatherNotFoundException;
import com.umc.yourweather.response.*;
import com.umc.yourweather.repository.WeatherRepository;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -25,8 +27,7 @@ public class WeatherService {


@Transactional
public MissedInputResponseDto getMissedInputs(
CustomUserDetails userDetails) {
public MissedInputResponseDto getMissedInputs(CustomUserDetails userDetails) {

// 응답 변수 추가
MissedInputResponseDto missedInputResponseDto = new MissedInputResponseDto();
Expand All @@ -45,8 +46,7 @@ public MissedInputResponseDto getMissedInputs(
dateIterator = dateIterator.plusDays(1);
}

List<Weather> weathers = weatherRepository.findWeatherByDateBetweenAndUser(oneWeekAgo,
current,userDetails.getUser());
List<Weather> weathers = weatherRepository.findWeatherByDateBetweenAndUser(oneWeekAgo, current, userDetails.getUser());

for (Weather weather : weathers) {
LocalDate localDate = weather.getDate();
Expand All @@ -64,20 +64,14 @@ public HomeResponseDto home(CustomUserDetails userDetails) {
LocalDate localDate = LocalDate.now();
MemoManager memoManager = new MemoManager();

Weather weather = weatherRepository.findByDateAndUser(localDate, user)
.orElseThrow(() -> new WeatherNotFoundException("오늘 날짜에 해당하는 날씨 객체가 존재하지 않습니다."));
Weather weather = weatherRepository.findByDateAndUser(localDate, user).orElseThrow(() -> new WeatherNotFoundException("오늘 날짜에 해당하는 날씨 객체가 존재하지 않습니다."));

List<Memo> memoList = weather.getMemos();
memoManager.isMemoListEmpty(memoList);
String imageName = memoManager.getImageName(memoList);

Memo lastMemo = memoList.get(memoList.size() - 1);
return HomeResponseDto.builder()
.nickname(user.getNickname())
.status(lastMemo.getStatus())
.temperature(lastMemo.getTemperature())
.imageName(imageName)
.build();
return HomeResponseDto.builder().nickname(user.getNickname()).status(lastMemo.getStatus()).temperature(lastMemo.getTemperature()).imageName(imageName).build();

}

Expand All @@ -94,9 +88,8 @@ public WeatherResponseDto delete(LocalDate localDate, CustomUserDetails userDeta

@Transactional
public String checkMemoAndDelete(Long weatherId) {
Weather weather = weatherRepository.findById(weatherId)
.orElseThrow(
() -> new WeatherNotFoundException("해당 아이디로 조회되는 Weather 엔티티가 존재하지 않습니다."));
Weather weather = weatherRepository.findById(weatherId).orElseThrow(()
-> new WeatherNotFoundException("해당 아이디로 조회되는 Weather 엔티티가 존재하지 않습니다."));

List<Memo> memoList = weather.getMemos();
if (memoList.size() > 0) {
Expand All @@ -108,6 +101,22 @@ public String checkMemoAndDelete(Long weatherId) {
return " (해당 날짜의 Memo가 전부 삭제되어 해당 날짜의 Weather 또한 같이 삭제되었습니다.)";
}

public void update(Long weatherId){
Weather weather = weatherRepository.findById(weatherId).orElseThrow(()
-> new WeatherNotFoundException("해당 아이디로 조회되는 Weather 엔티티가 존재하지 않습니다."));
List<Memo> memoList = weather.getMemos();

Memo memoWithHighestTemperature = memoList.get(0); // Initialize with the first memo

for (Memo tmpMemo : memoList) {
if (tmpMemo.getTemperature() > memoWithHighestTemperature.getTemperature()) {
memoWithHighestTemperature = tmpMemo; // Update if a memo with higher temperature is found
}
}

weather.update(memoWithHighestTemperature.getStatus(),memoWithHighestTemperature.getTemperature());
}

@Transactional(readOnly = true)
public WeatherMonthlyResponseDto getMonthlyList(int year, int month, CustomUserDetails userDetails) {

Expand Down

0 comments on commit f6fed99

Please sign in to comment.