Skip to content

Commit

Permalink
Merge pull request #129 from UMC-TripPiece/91-map-color-and-edit
Browse files Browse the repository at this point in the history
fix: 국가별 최신 색상 정렬 오류 수정 및 코드 최적화
  • Loading branch information
StoneCAU authored Jan 30, 2025
2 parents 256158d + f3d8514 commit d0b29dd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/main/java/umc/TripPiece/repository/MapRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public interface MapRepository extends JpaRepository<Map, Long> {

// 유저 ID로 맵을 조회하는 메소드 (저장된 시간 순서로 정렬 - 최신 데이터가 리스트 마지막에 위치)
@Query("SELECT m FROM Map m WHERE m.userId = :userId ORDER BY m.countryCode ASC, m.createdAt ASC")
@Query("SELECT m FROM Map m WHERE m.userId = :userId ORDER BY m.countryCode ASC, m.createdAt DESC")
List<Map> findByUserIdOrderedByCreatedAt(Long userId);

// 유저가 방문한 나라 수를 조회하는 메소드
Expand All @@ -37,7 +37,7 @@ public interface MapRepository extends JpaRepository<Map, Long> {
@Query("SELECT m FROM Map m WHERE m.userId = :userId AND m.countryCode = :countryCode AND m.city.id = :cityId")
List<Map> findAllByUserIdAndCountryCodeAndCityId(Long userId, String countryCode, Long cityId);

// 특정 국가에서 유저가 색칠한 도시를 정렬된 순서로 조회 (최신이 마지막에 위치)
@Query("SELECT m FROM Map m WHERE m.countryCode = :countryCode AND m.userId = :userId ORDER BY m.createdAt ASC")
// 특정 국가에서 유저가 색칠한 도시를 정렬된 순서로 조회 (최신 데이터가 리스트 마지막에 위치)
@Query("SELECT m FROM Map m WHERE m.countryCode = :countryCode AND m.userId = :userId ORDER BY m.createdAt DESC")
List<Map> findByCountryCodeAndUserIdOrderedByCreatedAt(String countryCode, Long userId);
}
30 changes: 7 additions & 23 deletions src/main/java/umc/TripPiece/service/MapService.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ public MapResponseDto updateMapColor(Long mapId, String newColor) {
.orElseThrow(() -> new IllegalArgumentException("Map not found with id: " + mapId));

map.setColor(newColor);
Map updatedMap = mapRepository.save(map);
return MapConverter.toMapResponseDto(updatedMap);
return MapConverter.toMapResponseDto(mapRepository.save(map));
}

@Transactional
Expand All @@ -76,8 +75,7 @@ public MapResponseDto updateMapColorWithInfo(String countryCode, Long cityId, St
.orElseThrow(() -> new IllegalArgumentException("Map not found with provided info."));

map.setColor(newColor);
Map updatedMap = mapRepository.save(map);
return MapConverter.toMapResponseDto(updatedMap);
return MapConverter.toMapResponseDto(mapRepository.save(map));
}

@Transactional
Expand Down Expand Up @@ -107,8 +105,7 @@ public MapResponseDto updateMultipleMapColors(Long mapId, List<String> colorStri
.orElseThrow(() -> new IllegalArgumentException("Map not found with id: " + mapId));

map.setColors(colorStrings);
Map updatedMap = mapRepository.save(map);
return MapConverter.toMapResponseDto(updatedMap);
return MapConverter.toMapResponseDto(mapRepository.save(map));
}

public List<MapResponseDto> getMapsByUserId(Long userId) {
Expand Down Expand Up @@ -174,28 +171,15 @@ public List<MapResponseDto.searchDto> searchCitiesCountry(String keyword) {
List<City> cities = cityRepository.findByNameIgnoreCase(keyword);
List<Country> countries = countryRepository.findByNameIgnoreCase(keyword);

List<MapResponseDto.searchDto> results = new ArrayList<>();

if (!cities.isEmpty()) {
results.addAll(cities.stream().map(MapConverter::toSearchDto).collect(Collectors.toList()));
}

if (!countries.isEmpty()) {
for (Country country : countries) {
List<City> citiesInCountry = cityRepository.findByCountryId(country.getId());
results.addAll(citiesInCountry.stream().map(MapConverter::toSearchDto).collect(Collectors.toList()));
}
}

return results;
return cities.stream()
.map(MapConverter::toSearchDto)
.collect(Collectors.toList());
}

public List<MapResponseDto.getMarkerResponse> getUserMarkers() {
Long userId = SecurityUtils.getCurrentUserId();

List<Map> maps = mapRepository.findByUserIdOrderedByCreatedAt(userId);

return maps.stream()
return mapRepository.findByUserIdOrderedByCreatedAt(userId).stream()
.map(map -> MapConverter.toMarkerResponseDto(
map,
"",
Expand Down

0 comments on commit d0b29dd

Please sign in to comment.