Skip to content

Commit

Permalink
bugfix: enum index맞춤
Browse files Browse the repository at this point in the history
  • Loading branch information
Changha-dev committed Apr 30, 2024
1 parent 77602bc commit d4dd7e0
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -37,14 +37,16 @@ public class UpdatePostRequest { // 자료형과 변수명 변경
@NotBlank(message = "교통수단은 필수입니다.")
private String transportation;


@NotNull(message = "이미지는 필수입니다.")
private List<PostImage> 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<PostImage> 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<PostImage> images, Long districtId, GetGeometryResponse geometry) {
this.category = category;
this.title = title;
this.subTitle = subTitle;
Expand All @@ -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;
}
}
30 changes: 20 additions & 10 deletions src/main/java/com/seoultech/sanEseo/post/domain/Category.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ public List<GetPostDistrictResponse> getPostDistrict(Long districtId) {

public List<GetPostDistrictResponse> getAllPostDistrict(Pageable pageable, int category) {
Category categoryEnum = Category.from(category);
System.out.println("categoryEnum = " + categoryEnum);
Slice<PostDistrict> postDistrictsSlice = postDistrictPort.findByPostCategory(categoryEnum, pageable);
System.out.println("postDistrictsSlice = " + postDistrictsSlice.getContent());

// Slice의 실제 내용을 리스트로 변환하고, 응답 DTO 생성
List<PostDistrict> postDistricts = postDistrictsSlice.getContent();
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/seoultech/sanEseo/public_api/Coordinate.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Entity
Expand Down Expand Up @@ -35,4 +36,18 @@ public Coordinate(String name, String type, List<LatLng> 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<LatLng> convertGeometryToLatLngList(GetGeometryResponse geometry) {
// geometry의 coordinates를 List<LatLng>로 변환하는 로직
List<LatLng> newCoordinates = new ArrayList<>();
for (LatLng coordinatePair : geometry.getCoordinates()) {
newCoordinates.add(new LatLng(coordinatePair.getLat(), coordinatePair.getLng()));
}
return newCoordinates;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import java.util.List;

@Converter
public class CoordinatesConverter implements AttributeConverter<List<List<Double>>, String> {
public class CoordinatesConverter implements AttributeConverter<List<LatLng>, String> {
private final static ObjectMapper objectMapper = new ObjectMapper();

@Override
public String convertToDatabaseColumn(List<List<Double>> attribute) {
public String convertToDatabaseColumn(List<LatLng> attribute) {
try {
return objectMapper.writeValueAsString(attribute);
} catch (IOException e) {
Expand All @@ -22,9 +22,9 @@ public String convertToDatabaseColumn(List<List<Double>> attribute) {
}

@Override
public List<List<Double>> convertToEntityAttribute(String dbData) {
public List<LatLng> convertToEntityAttribute(String dbData) {
try {
return objectMapper.readValue(dbData, new TypeReference<List<List<Double>>>() {});
return objectMapper.readValue(dbData, new TypeReference<List<LatLng>>() {});
} catch (IOException e) {
throw new RuntimeException("Conversion error", e);
}
Expand Down

0 comments on commit d4dd7e0

Please sign in to comment.