맵 색상 코드 유연성 개선 및 지도 API 기능 확장

버질·2025년 1월 8일

✅ 1. HEX 색상 코드 지원 추가

코드 변경 내용:

Color 열거형에서 HEX 코드 처리 개선:

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 코드 유효성을 검증하고 유효하지 않은 코드에 대해 예외 처리.

✅ 2. 지도 관련 기능 개선

코드 변경 내용:

맵 생성 및 업데이트 로직 추가:

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

마커 반환 API 개선:

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

✅ 3. 컨트롤러 작업

코드 변경 내용:

MapController에 엔드포인트 추가:

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

✅ 4. DTO 구조 개선

MapRequestDto와 MapResponseDto에 다중 색상 필드 추가:

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; // 다중 색상 반환
}

✅ 5. 주요 개선 사항

HEX 색상 코드 지원으로 UX 강화.
지도 관련 기능의 유연성 증가:
색상 단일/다중 설정 가능.
유효하지 않은 값에 대해 명확한 예외 처리.
테스트를 통해 정상 동작 확인.

profile
iOS Developer · SwiftUI & UIKit '가끔 되고 가끔 안 되는' 문제를 뿌리부터 잡습니다.

0개의 댓글