Skip to content

Commit

Permalink
Feat : 요즘 떠오르는 도시 api 생성 #1
Browse files Browse the repository at this point in the history
  • Loading branch information
oyatplum committed Dec 23, 2024
1 parent b0bac82 commit afeed0b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/umc/TripPiece/converter/ExploreConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ public static ExploreResponseDto.ExploreListDto toExploreListDto(Travel travel)
.nickname(user.getNickname())
.build();
}

public static ExploreResponseDto.PopularCitiesDto toPopularCitiesDto(City city){
return ExploreResponseDto.PopularCitiesDto.builder()
.id(city.getId())
.city(city.getName())
.country(city.getCountry().getName())
.thumbnail(city.getCityImage())
.count(city.getLogCount())
.build();
}
}
4 changes: 4 additions & 0 deletions src/main/java/umc/TripPiece/repository/CityRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ public interface CityRepository extends JpaRepository<City, Long> {
// 유저가 방문한 도시 리스트 조회
@Query("SELECT DISTINCT c FROM City c JOIN Travel t ON t.city.id = c.id WHERE t.user.id = :userId")
List<City> findCitiesByUserId(Long userId);

//도시별 여행기 수 내림차순
@Query("SELECT c FROM City c ORDER BY c.logCount DESC ")
List<City> findAllByOrderByLogCountDesc();
}
1 change: 1 addition & 0 deletions src/main/java/umc/TripPiece/service/CityService.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ public List<CityResponseDto.searchDto> searchCity(String keyword){

return searched;
}

}
6 changes: 6 additions & 0 deletions src/main/java/umc/TripPiece/service/ExploreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,11 @@ public List<ExploreResponseDto.ExploreListDto> searchTravels(String query){
return travels.stream().distinct().map(ExploreConverter::toExploreListDto).toList();
}

@Transactional
public List<ExploreResponseDto.PopularCitiesDto> getCitiesByTravelCount() {
List<City> cities = cityRepository.findAllByOrderByLogCountDesc();
return cities.stream().map(ExploreConverter::toPopularCitiesDto).toList();
}


}
10 changes: 10 additions & 0 deletions src/main/java/umc/TripPiece/web/controller/ExploreController.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ public ApiResponse<List<ExploreResponseDto.ExploreListDto>> getSearchedTravelLis
}
return ApiResponse.onSuccess(travels);
}

@GetMapping("/popular")
@Operation(summary = "요즘 떠오르는 도시 API", description = "도시별 여행기순 내림차순")
public ApiResponse<List<ExploreResponseDto.PopularCitiesDto>> getPopularCities(){
List<ExploreResponseDto.PopularCitiesDto> cities = exploreService.getCitiesByTravelCount();
if(cities.isEmpty()){
return ApiResponse.onFailure("400", "생성된 여행기 없음.", null);
}
return ApiResponse.onSuccess(cities);
}
}

0 comments on commit afeed0b

Please sign in to comment.