Skip to content

Commit

Permalink
[refactor] 도시 기본 정보 조회시 인증 객체로부터 도시 보관 여부 조회하고 응답하도록 서비스 로직 및 API 수정
Browse files Browse the repository at this point in the history
related to: #144
  • Loading branch information
jo0oy committed Feb 3, 2024
1 parent 3137d41 commit c6a4f94
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.haejwo.tripcometrue.domain.city.dto.response.ExchangeRateResponseDto;
import com.haejwo.tripcometrue.domain.city.dto.response.WeatherResponseDto;
import com.haejwo.tripcometrue.domain.city.service.CityInfoReadService;
import com.haejwo.tripcometrue.global.springsecurity.PrincipalDetails;
import com.haejwo.tripcometrue.global.util.ResponseDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -21,13 +23,14 @@ public class CityInfoReadController {

@GetMapping("/{cityId}")
public ResponseEntity<ResponseDTO<CityInfoResponseDto>> getCityInfo(
@PathVariable("cityId") Long cityId
) {
@PathVariable("cityId") Long cityId,
@AuthenticationPrincipal PrincipalDetails principalDetails
) {

return ResponseEntity
.status(HttpStatus.OK)
.body(
ResponseDTO.okWithData(cityInfoReadService.getCityInfo(cityId))
ResponseDTO.okWithData(cityInfoReadService.getCityInfo(cityId, principalDetails))
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import com.haejwo.tripcometrue.domain.city.dto.response.ExchangeRateResponseDto;
import com.haejwo.tripcometrue.domain.city.dto.response.WeatherResponseDto;
import com.haejwo.tripcometrue.domain.city.entity.City;
import com.haejwo.tripcometrue.domain.store.repository.CityStoreRepository;
import com.haejwo.tripcometrue.global.enums.CurrencyUnit;
import com.haejwo.tripcometrue.domain.city.entity.Weather;
import com.haejwo.tripcometrue.domain.city.exception.CityNotFoundException;
import com.haejwo.tripcometrue.domain.city.repository.CityRepository;
import com.haejwo.tripcometrue.domain.city.repository.WeatherRepository;
import com.haejwo.tripcometrue.global.springsecurity.PrincipalDetails;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
Expand All @@ -19,22 +21,30 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RequiredArgsConstructor
@Service
public class CityInfoReadService {

private final CityRepository cityRepository;
private final WeatherRepository weatherRepository;
private final CityStoreRepository cityStoreRepository;
private final RedisTemplate<String, Object> redisTemplate;

private static final int WEATHER_MONTH_GAP = 3;

@Transactional(readOnly = true)
public CityInfoResponseDto getCityInfo(Long cityId) {
public CityInfoResponseDto getCityInfo(Long cityId, PrincipalDetails principalDetails) {

return CityInfoResponseDto.fromEntity(
cityRepository.findById(cityId)
.orElseThrow(CityNotFoundException::new)
.orElseThrow(CityNotFoundException::new),
Objects.nonNull(principalDetails)
&& Objects.nonNull(principalDetails.getMember())
&& cityStoreRepository
.findByMemberIdAndCityId(principalDetails.getMember().getId(), cityId)
.isPresent()
);
}

Expand Down Expand Up @@ -80,7 +90,7 @@ public List<WeatherResponseDto> getWeatherInfo(Long cityId) {
private List<Weather> sortWeatherInfos(List<Weather> weathers) {
int lastIdx = weathers.size() - 1;

if(weathers.get(lastIdx).getMonth() - weathers.get(0).getMonth() > WEATHER_MONTH_GAP) {
if (weathers.get(lastIdx).getMonth() - weathers.get(0).getMonth() > WEATHER_MONTH_GAP) {
weathers.add(0, weathers.remove(lastIdx));

for (int i = lastIdx; i >= 0; i--) {
Expand Down

0 comments on commit c6a4f94

Please sign in to comment.