Skip to content

Commit

Permalink
Merge pull request #89 from UMC-TripPiece/feature/1
Browse files Browse the repository at this point in the history
[feature] 요즘 떠오르는 도시 api 구현
  • Loading branch information
yyypearl authored Dec 28, 2024
2 parents 5de90a3 + afeed0b commit a74fd7a
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 7 deletions.
Binary file modified .DS_Store
Binary file not shown.
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();
}
}
5 changes: 4 additions & 1 deletion src/main/java/umc/TripPiece/domain/City.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ public class City {

private String name;
private String comment;
private String cityImage;

@Setter
private Long logCount;
@Column(name = "log_count", nullable = false, columnDefinition = "BIGINT DEFAULT 0")
private Long logCount = 0L;

@OneToMany(mappedBy = "city")
private List<Travel> travels = new ArrayList<>();
Expand Down
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;
}

}
11 changes: 7 additions & 4 deletions src/main/java/umc/TripPiece/service/ExploreService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ public class ExploreService {
public List<ExploreResponseDto.ExploreListDto> searchTravels(String query){
List<City> cities = cityRepository.findByNameContainingIgnoreCase(query);
List<Country> countries = countryRepository.findByNameContainingIgnoreCase(query);

Set<Long> cityIds = new HashSet<>();

cities.forEach(city -> cityIds.add(city.getId()));

countries.forEach(country -> {
List<City> citiesInCountry = cityRepository.findByCountryId(country.getId());
citiesInCountry.forEach(city -> cityIds.add(city.getId()));
});

List<Travel> travels = travelRepository.findByCityIdInAndTravelOpenTrue(new ArrayList<>(cityIds));

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();
}


}
5 changes: 4 additions & 1 deletion src/main/java/umc/TripPiece/service/TravelService.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ public TravelResponseDto.Create createTravel(TravelRequestDto.Create request, Mu
User user = userRepository.findById(userId).orElseThrow(() -> new IllegalArgumentException("user not found"));

City city = cityRepository.findByNameContainingIgnoreCase(request.getCityName()).stream().findFirst().orElseThrow(() -> new IllegalArgumentException("city not found"));
city.setLogCount(city.getLogCount() + 1);

if (request.getStartDate().isAfter(request.getEndDate())) {
throw new IllegalArgumentException("Start date cannot be after end date.");
Expand Down Expand Up @@ -240,6 +239,10 @@ public TravelResponseDto.Create createTravel(TravelRequestDto.Create request, Mu
public TravelResponseDto.TripSummaryDto endTravel(Long travelId) {
Travel travel = travelRepository.findById(travelId).orElseThrow(() -> new IllegalArgumentException("travel not found"));
travel.setStatus(TravelStatus.COMPLETED);

City city = travel.getCity();
city.setLogCount(city.getLogCount() + 1);

List<TripPiece> tripPieces = tripPieceRepository.findByTravelId(travelId);
initPicturesThumbnail(travel);

Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,20 @@ public static class ExploreListDto {
private String countryName;
private TravelStatus status;
private String countryImage;

private Long userId;
private String profileImg;
private String nickname;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class PopularCitiesDto {
private Long id;
private String city;
private String country;
private String thumbnail;
private Long count;
}
}

0 comments on commit a74fd7a

Please sign in to comment.