코드 설명:
getVisitedCountries(Long userId)
사용자가 방문한 국가 코드의 리스트를 반환합니다.
MapRepository를 통해 중복되지 않은 국가 코드(countryCode)를 조회합니다.
public List<String> getVisitedCountries(Long userId) {
return mapRepository.findDistinctCountryCodesByUserId(userId);
}
getVisitedCountryCount(Long userId)
사용자가 방문한 국가의 개수를 반환합니다.
MapRepository에서 COUNT DISTINCT 쿼리를 통해 데이터베이스 레벨에서 중복을 제거하고 개수를 반환합니다.
public long getVisitedCountryCount(Long userId) {
return mapRepository.countDistinctCountryCodeByUserId(userId);
}
새로운 API를 추가하여 방문한 국가 정보를 제공합니다.
/maps/{userId}/visited-countries 경로로 접근하며, countryCodes(국가 코드 리스트)와 countryCount(방문한 국가 수)를 응답으로 반환합니다.
API 구현
@GetMapping("/{userId}/visited-countries")
@Operation(summary = "방문한 나라 누적 API", description = "사용자가 방문한 나라의 리스트와 카운트를 반환")
public ApiResponse<MapStatsResponseDto> getVisitedCountries(@PathVariable(name = "userId") Long userId) {
List<String> countryCodes = mapService.getVisitedCountries(userId);
long countryCount = mapService.getVisitedCountryCount(userId);
MapStatsResponseDto response = MapStatsResponseDto.builder()
.countryCodes(countryCodes)
.countryCount(countryCount)
.build();
return ApiResponse.onSuccess(response);
}
사용자가 방문한 국가 코드와 개수를 프론트엔드에서 요청할 수 있는 API를 제공.
프론트엔드에서 국가 누적 데이터를 활용하여 방문 기록을 시각화하거나 통계 자료를 표시 가능.
코드 설명:
기존 단일 색상 관리
Map 엔티티의 색상 컬럼(color)을 String으로 변경하여 HEX 값을 유연하게 저장할 수 있도록 수정하였습니다.
@Column(name = "color")
private String color; // 단일 색상 처리
다중 색상 관리 추가
colors 필드를 추가하여 다중 색상 처리가 가능하도록 구현했습니다.
@ElementCollection을 활용해 별도의 테이블(map_colors)에 여러 색상 데이터를 저장합니다.
@ElementCollection
@CollectionTable(name = "map_colors", joinColumns = @JoinColumn(name = "map_id"))
@Column(name = "colors")
private List<String> colors = new ArrayList<>(); // 다중 색상 처리
단일 색상 수정
updateMapColor 메서드를 통해 요청된 색상(HEX 코드)을 color 필드에 업데이트합니다.
@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);
return MapConverter.toMapResponseDto(mapRepository.save(map));
}
다중 색상 업데이트
여러 색상을 한 번에 업데이트할 수 있도록 updateMultipleMapColors 메서드를 추가했습니다.
프론트엔드에서 색상 리스트를 요청하면 이를 colors 필드에 저장합니다.
@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);
return MapConverter.toMapResponseDto(mapRepository.save(map));
}
API 응답 예시:
{
"isSuccess": true,
"code": "200",
"message": "요청 성공",
"result": {
"visitId": 1,
"userId": 123,
"countryCode": "KR",
"color": "#FF5733",
"colors": ["#FF5733", "#33FF57", "#3357FF"]
}
}
단일 색상과 다중 색상을 모두 관리할 수 있도록 처리 범위를 확장.
요청된 HEX 값 기반으로 지도 색상을 유연하게 처리 가능.
색상 데이터를 통한 사용자 경험 향상을 도모.
MapStatsResponseDto: 방문 국가 정보를 추가하기 위해 countryCodes와 countryCount 필드를 확장.
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MapStatsResponseDto {
private long countryCount;
private long cityCount;
private List<String> countryCodes;
private List<Long> cityIds;
private String profileImg;
private String nickname;
}
MapResponseDto: 색상 데이터를 유연하게 다루기 위해 color를 String으로 변경, colors 필드 추가.
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MapResponseDto {
private Long visitId;
private Long userId;
private String countryCode;
private String color; // 단일 색상
private List<String> colors; // 다중 색상
}