Skip to content

Commit

Permalink
Merge pull request #115 from UMC-TripPiece/91-map-color-and-edit
Browse files Browse the repository at this point in the history
[Feat] 기존 정보 기반 맵 수정/삭제 API 추가
  • Loading branch information
yyypearl authored Jan 15, 2025
2 parents 5ff4404 + b1b0fd7 commit f34b6e1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
38 changes: 23 additions & 15 deletions src/main/java/umc/TripPiece/service/MapService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,12 @@ public class MapService {
private final UserRepository userRepository;
private final CountryRepository countryRepository;

// 유저별 맵 리스트를 조회
public List<MapResponseDto> getMapsByUserId(Long userId) {
return mapRepository.findByUserId(userId).stream()
.map(MapConverter::toMapResponseDto)
.collect(Collectors.toList());
}

// 맵 생성 시 도시 정보 포함
@Transactional
public MapResponseDto createMapWithCity(MapRequestDto requestDto) {
City city = cityRepository.findById(requestDto.getCityId())
Expand All @@ -50,39 +48,53 @@ public MapResponseDto createMapWithCity(MapRequestDto requestDto) {
return MapConverter.toMapResponseDto(savedMap);
}

// 맵 색상 수정
@Transactional
public MapResponseDto updateMapColor(Long mapId, String newColor) {
Map map = mapRepository.findById(mapId)
.orElseThrow(() -> new IllegalArgumentException("Map not found with id: " + mapId));

map.setColor(newColor); // 요청된 hex 값을 설정
map.setColor(newColor);
Map updatedMap = mapRepository.save(map);
return MapConverter.toMapResponseDto(updatedMap);
}

@Transactional
public MapResponseDto updateMapColorWithInfo(Long userId, String countryCode, Long cityId, String newColor) {
Map map = mapRepository.findByUserIdAndCountryCodeAndCityId(userId, countryCode, cityId)
.orElseThrow(() -> new IllegalArgumentException("Map not found with provided info."));

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

// 맵 색상 삭제
@Transactional
public void deleteMapColor(Long mapId) {
Map map = mapRepository.findById(mapId)
.orElseThrow(() -> new IllegalArgumentException("Map not found with id: " + mapId));

map.setColor(null); // 색상 삭제
map.setColor(null);
mapRepository.save(map);
}

// 여러 색상 선택 업데이트
@Transactional
public void deleteMapWithInfo(Long userId, String countryCode, Long cityId) {
Map map = mapRepository.findByUserIdAndCountryCodeAndCityId(userId, countryCode, cityId)
.orElseThrow(() -> new IllegalArgumentException("Map not found with provided info."));

mapRepository.delete(map);
}

@Transactional
public MapResponseDto updateMultipleMapColors(Long mapId, List<String> colorStrings) {
Map map = mapRepository.findById(mapId)
.orElseThrow(() -> new IllegalArgumentException("Map not found with id: " + mapId));

map.setColors(colorStrings); // 다중 색상 설정
map.setColors(colorStrings);
Map updatedMap = mapRepository.save(map);
return MapConverter.toMapResponseDto(updatedMap);
}

// 유저별 방문한 나라와 도시 통계 반환
public MapStatsResponseDto getMapStatsByUserId(Long userId) {
long countryCount = mapRepository.countDistinctCountryCodeByUserId(userId);
long cityCount = mapRepository.countDistinctCityByUserId(userId);
Expand All @@ -103,7 +115,6 @@ public MapStatsResponseDto getMapStatsByUserId(Long userId) {
);
}

// 방문한 나라 누적 정보 반환
public List<String> getVisitedCountries(Long userId) {
return mapRepository.findDistinctCountryCodesByUserId(userId);
}
Expand All @@ -112,7 +123,6 @@ public long getVisitedCountryCount(Long userId) {
return mapRepository.countDistinctCountryCodeByUserId(userId);
}

// 방문한 나라 누적 정보와 프로필 통합 반환
public MapStatsResponseDto getVisitedCountriesWithProfile(Long userId) {
List<String> visitedCountries = getVisitedCountries(userId);
long visitedCountryCount = getVisitedCountryCount(userId);
Expand All @@ -124,13 +134,12 @@ public MapStatsResponseDto getVisitedCountriesWithProfile(Long userId) {
visitedCountryCount,
visitedCountries.size(),
visitedCountries,
Collections.emptyList(), // 도시 ID 리스트는 필요 시 추가
Collections.emptyList(),
user.getProfileImg(),
user.getNickname()
);
}

// 도시 및 국가 검색
public List<MapResponseDto.searchDto> searchCitiesCountry(String keyword) {
if (keyword == null || keyword.trim().isEmpty()) {
return new ArrayList<>();
Expand All @@ -155,15 +164,14 @@ public List<MapResponseDto.searchDto> searchCitiesCountry(String keyword) {
return results;
}

// 마커 반환
public List<MapResponseDto.getMarkerResponse> getMarkers(String token) {
Long userId = jwtUtil.getUserIdFromToken(token);
List<Map> maps = mapRepository.findByUserId(userId);

return maps.stream()
.map(map -> MapConverter.toMarkerResponseDto(
map,
"", // 마커 이미지 URL 추가 필요 시 수정
"",
map.getCity().getCountry().getName(),
map.getCity().getName()
))
Expand Down
20 changes: 19 additions & 1 deletion src/main/java/umc/TripPiece/web/controller/MapController.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,29 @@ public ApiResponse<MapResponseDto> updateMultipleMapColors(@PathVariable(name =
return ApiResponse.onSuccess(updatedMap);
}

@PutMapping("/color")
@Operation(summary = "맵 색상 수정 (기존 정보 기반)", description = "맵의 색상을 수정 (userId, countryCode, cityId 기반)")
public ApiResponse<MapResponseDto> updateMapColorWithInfo(@RequestHeader("Authorization") String token,
@RequestBody @Valid MapRequestDto requestDto) {
Long userId = jwtUtil.getUserIdFromToken(token.substring(7)); // Bearer 제거
MapResponseDto updatedMap = mapService.updateMapColorWithInfo(userId, requestDto.getCountryCode(), requestDto.getCityId(), requestDto.getColor());
return ApiResponse.onSuccess(updatedMap);
}

@DeleteMapping("/color")
@Operation(summary = "맵 삭제 (기존 정보 기반)", description = "맵을 삭제 (userId, countryCode, cityId 기반)")
public ApiResponse<Void> deleteMapWithInfo(@RequestHeader("Authorization") String token,
@RequestBody @Valid MapRequestDto requestDto) {
Long userId = jwtUtil.getUserIdFromToken(token.substring(7)); // Bearer 제거
mapService.deleteMapWithInfo(userId, requestDto.getCountryCode(), requestDto.getCityId());
return ApiResponse.onSuccess(null);
}

@GetMapping("/visited-countries")
@Operation(summary = "방문한 나라 누적 API", description = "사용자가 방문한 나라의 리스트와 카운트를 반환")
public ApiResponse<MapStatsResponseDto> getVisitedCountries(@RequestHeader("Authorization") String token) {
String tokenWithoutBearer = token.substring(7); // Bearer 제거
Long userId = jwtUtil.getUserIdFromToken(tokenWithoutBearer); // jwtUtil 사용
Long userId = jwtUtil.getUserIdFromToken(tokenWithoutBearer);
MapStatsResponseDto response = mapService.getVisitedCountriesWithProfile(userId);
return ApiResponse.onSuccess(response);
}
Expand Down

0 comments on commit f34b6e1

Please sign in to comment.