From d4dd7e097fed5f076341b0f4f64c36868366491d Mon Sep 17 00:00:00 2001 From: changha Date: Tue, 30 Apr 2024 17:30:58 +0900 Subject: [PATCH] =?UTF-8?q?bugfix:=20enum=20index=EB=A7=9E=EC=B6=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/application/service/PostService.java | 7 +++++ .../service/UpdatePostRequest.java | 11 ++++--- .../sanEseo/post/domain/Category.java | 30 ++++++++++++------- .../service/PostDistrictService.java | 2 ++ .../sanEseo/public_api/Coordinate.java | 15 ++++++++++ .../sanEseo/public_api/CoordinateService.java | 5 ++++ .../public_api/CoordinatesConverter.java | 8 ++--- 7 files changed, 60 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/seoultech/sanEseo/post/application/service/PostService.java b/src/main/java/com/seoultech/sanEseo/post/application/service/PostService.java index 3c5d90c..9ac640e 100644 --- a/src/main/java/com/seoultech/sanEseo/post/application/service/PostService.java +++ b/src/main/java/com/seoultech/sanEseo/post/application/service/PostService.java @@ -6,6 +6,7 @@ import com.seoultech.sanEseo.post.domain.Post; import com.seoultech.sanEseo.post_district.domain.PostDistrict; import com.seoultech.sanEseo.post_district.application.port.PostDistrictPort; +import com.seoultech.sanEseo.public_api.Coordinate; import com.seoultech.sanEseo.public_api.CoordinateService; import com.seoultech.sanEseo.public_api.GetCoordinateResponse; import com.seoultech.sanEseo.public_api.GetGeometryResponse; @@ -78,6 +79,12 @@ public GetPostResponse getPost(Long postId) { @Transactional public void updatePost(Long postId, UpdatePostRequest request) { Post post = postPort.getPost(postId); + + // 좌표 정보 업데이트 + Coordinate coordinate = coordinateService.findCoordinate(post); + coordinate.update(request.getGeometry().getName(), request.getGeometry().getType(), request.getGeometry()); + + // 게시글 정보 업데이트 post.update( request.getCategory(), request.getTitle(), request.getSubTitle(), request.getDescription(), request.getLevel(), request.getTime(), diff --git a/src/main/java/com/seoultech/sanEseo/post/application/service/UpdatePostRequest.java b/src/main/java/com/seoultech/sanEseo/post/application/service/UpdatePostRequest.java index d432ea3..3dad015 100644 --- a/src/main/java/com/seoultech/sanEseo/post/application/service/UpdatePostRequest.java +++ b/src/main/java/com/seoultech/sanEseo/post/application/service/UpdatePostRequest.java @@ -2,14 +2,14 @@ import com.seoultech.sanEseo.post.domain.Category; import com.seoultech.sanEseo.image.PostImage; +import com.seoultech.sanEseo.public_api.GetGeometryResponse; import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotNull; import lombok.Getter; import java.util.List; @Getter -public class UpdatePostRequest { // 자료형과 변수명 변경 - +public class UpdatePostRequest { @NotNull(message = "카테고리는 필수입니다.") private Category category; @@ -37,14 +37,16 @@ public class UpdatePostRequest { // 자료형과 변수명 변경 @NotBlank(message = "교통수단은 필수입니다.") private String transportation; - @NotNull(message = "이미지는 필수입니다.") private List images; @NotNull(message = "자치구 ID는 필수입니다.") private Long districtId; - public UpdatePostRequest(Category category, String title, String subTitle, String description, String level, String time, String distance, String courseDetail, String transportation, List images, Long districtId) { + @NotNull(message = "좌표 정보는 필수입니다.") + private GetGeometryResponse geometry; + + public UpdatePostRequest(Category category, String title, String subTitle, String description, String level, String time, String distance, String courseDetail, String transportation, List images, Long districtId, GetGeometryResponse geometry) { this.category = category; this.title = title; this.subTitle = subTitle; @@ -56,5 +58,6 @@ public UpdatePostRequest(Category category, String title, String subTitle, Strin this.transportation = transportation; this.images = images; this.districtId = districtId; + this.geometry = geometry; } } diff --git a/src/main/java/com/seoultech/sanEseo/post/domain/Category.java b/src/main/java/com/seoultech/sanEseo/post/domain/Category.java index 1d14e6f..1c768b3 100644 --- a/src/main/java/com/seoultech/sanEseo/post/domain/Category.java +++ b/src/main/java/com/seoultech/sanEseo/post/domain/Category.java @@ -1,17 +1,27 @@ package com.seoultech.sanEseo.post.domain; +import com.fasterxml.jackson.annotation.JsonValue; + public enum Category { - DODREAM, CUSTOM; + DODREAM(0), CUSTOM(1); - public static Category from(int category) { - if (category == 1) { - return DODREAM; - } else if (category == 2) { - return CUSTOM; - } else { - throw new IllegalArgumentException("Unknown category: " + category); - } + private final int value; + + Category(int value) { + this.value = value; } + @JsonValue + public int getValue() { + return value; + } -} + public static Category from(int value) { + for (Category category : values()) { + if (category.getValue() == value) { + return category; + } + } + throw new IllegalArgumentException("Unknown category value: " + value); + } +} \ No newline at end of file diff --git a/src/main/java/com/seoultech/sanEseo/post_district/application/service/PostDistrictService.java b/src/main/java/com/seoultech/sanEseo/post_district/application/service/PostDistrictService.java index 12dd504..d89e8d1 100644 --- a/src/main/java/com/seoultech/sanEseo/post_district/application/service/PostDistrictService.java +++ b/src/main/java/com/seoultech/sanEseo/post_district/application/service/PostDistrictService.java @@ -49,7 +49,9 @@ public List getPostDistrict(Long districtId) { public List getAllPostDistrict(Pageable pageable, int category) { Category categoryEnum = Category.from(category); + System.out.println("categoryEnum = " + categoryEnum); Slice postDistrictsSlice = postDistrictPort.findByPostCategory(categoryEnum, pageable); + System.out.println("postDistrictsSlice = " + postDistrictsSlice.getContent()); // Slice의 실제 내용을 리스트로 변환하고, 응답 DTO 생성 List postDistricts = postDistrictsSlice.getContent(); diff --git a/src/main/java/com/seoultech/sanEseo/public_api/Coordinate.java b/src/main/java/com/seoultech/sanEseo/public_api/Coordinate.java index 41fbc66..6b29cff 100644 --- a/src/main/java/com/seoultech/sanEseo/public_api/Coordinate.java +++ b/src/main/java/com/seoultech/sanEseo/public_api/Coordinate.java @@ -7,6 +7,7 @@ import lombok.Getter; import lombok.NoArgsConstructor; +import java.util.ArrayList; import java.util.List; @Entity @@ -35,4 +36,18 @@ public Coordinate(String name, String type, List coordinates, Post post) this.coordinates = coordinates; this.post = post; } + public void update(String name, String type, GetGeometryResponse geometry) { + this.name = name; + this.type = type; + this.coordinates = convertGeometryToLatLngList(geometry); + } + + private List convertGeometryToLatLngList(GetGeometryResponse geometry) { + // geometry의 coordinates를 List로 변환하는 로직 + List newCoordinates = new ArrayList<>(); + for (LatLng coordinatePair : geometry.getCoordinates()) { + newCoordinates.add(new LatLng(coordinatePair.getLat(), coordinatePair.getLng())); + } + return newCoordinates; + } } \ No newline at end of file diff --git a/src/main/java/com/seoultech/sanEseo/public_api/CoordinateService.java b/src/main/java/com/seoultech/sanEseo/public_api/CoordinateService.java index a8f2cba..4ae1878 100644 --- a/src/main/java/com/seoultech/sanEseo/public_api/CoordinateService.java +++ b/src/main/java/com/seoultech/sanEseo/public_api/CoordinateService.java @@ -25,4 +25,9 @@ public GetCoordinateResponse getCoordinateResponse(Post post) { Coordinate byPost = coordinateRepository.findByPost(post); return new GetCoordinateResponse(byPost.getType(), byPost.getCoordinates()); } + + // coordinate찾기 + public Coordinate findCoordinate(Post post) { + return coordinateRepository.findByPost(post); + } } \ No newline at end of file diff --git a/src/main/java/com/seoultech/sanEseo/public_api/CoordinatesConverter.java b/src/main/java/com/seoultech/sanEseo/public_api/CoordinatesConverter.java index dfd51b6..004620e 100644 --- a/src/main/java/com/seoultech/sanEseo/public_api/CoordinatesConverter.java +++ b/src/main/java/com/seoultech/sanEseo/public_api/CoordinatesConverter.java @@ -9,11 +9,11 @@ import java.util.List; @Converter -public class CoordinatesConverter implements AttributeConverter>, String> { +public class CoordinatesConverter implements AttributeConverter, String> { private final static ObjectMapper objectMapper = new ObjectMapper(); @Override - public String convertToDatabaseColumn(List> attribute) { + public String convertToDatabaseColumn(List attribute) { try { return objectMapper.writeValueAsString(attribute); } catch (IOException e) { @@ -22,9 +22,9 @@ public String convertToDatabaseColumn(List> attribute) { } @Override - public List> convertToEntityAttribute(String dbData) { + public List convertToEntityAttribute(String dbData) { try { - return objectMapper.readValue(dbData, new TypeReference>>() {}); + return objectMapper.readValue(dbData, new TypeReference>() {}); } catch (IOException e) { throw new RuntimeException("Conversion error", e); }