public enum Color {
BLUE("6744FF"),
YELLOW("FFB40F"),
CYAN("25CEC1"),
RED("FD2D69");
private final String code;
private static final Map<String, Color> BY_CODE = new HashMap<>();
static {
for (Color c : values()) {
BY_CODE.put(c.code, c);
}
}
Color(String code) {
this.code = code;
}
public static Color fromHex(String hexCode) {
if (hexCode == null || hexCode.isBlank()) {
throw new IllegalArgumentException("Color code cannot be null or empty.");
}
Color color = BY_CODE.get(hexCode.toUpperCase());
if (color == null) {
throw new IllegalArgumentException("Invalid color code: " + hexCode);
}
return color;
}
}
fromHex 메서드로 HEX 코드 유효성을 검증하고 유효하지 않은 코드에 대해 예외 처리.
@Transactional
public MapResponseDto createMapWithCity(MapRequestDto requestDto) {
City city = cityRepository.findById(requestDto.getCityId())
.orElseThrow(() -> new IllegalArgumentException("City not found with id: " + requestDto.getCityId()));
Map map = MapConverter.toMap(requestDto, city);
Map savedMap = mapRepository.save(map);
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 값으로 색상 업데이트
return MapConverter.toMapResponseDto(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); // 다중 색상 설정
return MapConverter.toMapResponseDto(map);
}
@Transactional
public List<MapResponseDto.getMarkerResponse> getMarkers(String token) {
Long userId = jwtUtil.getUserIdFromToken(token);
List<Travel> travels = travelRepository.findByUserId(userId);
return travels.stream()
.map(travel -> {
City city = travel.getCity();
Country country = city.getCountry();
Map map = mapRepository.findByCountryCodeAndUserId(country.getCode(), userId);
return MapConverter.toMarkerResponseDto(map, travel.getThumbnail(), country.getName(), city.getName());
})
.collect(Collectors.toList());
}
@PutMapping("/color/{mapId}")
@Operation(summary = "맵 색상 수정 API", description = "맵의 색상을 수정")
public ApiResponse<MapResponseDto> updateMapColor(
@PathVariable(name = "mapId") Long mapId,
@RequestBody @Valid MapColorDto colorDto) {
MapResponseDto updatedMap = mapService.updateMapColor(mapId, colorDto.getColor());
return ApiResponse.onSuccess(updatedMap);
}
@PutMapping("/colors/{mapId}")
@Operation(summary = "맵 여러 색상 선택 API", description = "맵의 색상을 여러 개 선택")
public ApiResponse<MapResponseDto> updateMultipleMapColors(
@PathVariable(name = "mapId") Long mapId,
@RequestBody MapColorsDto colorsDto) {
MapResponseDto updatedMap = mapService.updateMultipleMapColors(mapId, colorsDto.getColors());
return ApiResponse.onSuccess(updatedMap);
}
public class MapRequestDto {
private Long userId;
@NotBlank
private String countryCode;
private String color; // 단일 색상 필드
private Long cityId;
}
public class MapResponseDto {
private Long visitId;
private Long userId;
private String countryCode;
private String color; // HEX 코드 반환
private List<String> colors; // 다중 색상 반환
}
HEX 색상 코드 지원으로 UX 강화.
지도 관련 기능의 유연성 증가:
색상 단일/다중 설정 가능.
유효하지 않은 값에 대해 명확한 예외 처리.
테스트를 통해 정상 동작 확인.