Skip to content

Commit

Permalink
Merge pull request #73 from SanE-Seo/changha
Browse files Browse the repository at this point in the history
Changha
  • Loading branch information
Changha-dev authored May 1, 2024
2 parents 61474ec + cde4e57 commit 67b4f25
Show file tree
Hide file tree
Showing 13 changed files with 122 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.seoultech.sanEseo.post.application.service;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.seoultech.sanEseo.post.domain.Category;
import com.seoultech.sanEseo.image.PostImage;
import com.seoultech.sanEseo.public_api.GetCoordinateResponse;
import com.seoultech.sanEseo.public_api.CoordinateRequest;

import com.seoultech.sanEseo.public_api.GetGeometryResponse;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
Expand Down Expand Up @@ -43,11 +45,22 @@ public class AddPostRequest {
private Long districtId;

@NotNull(message = "좌표 정보는 필수입니다.")
private GetGeometryResponse geometry;
private CoordinateRequest geometry;

// 생성자, 게터, 세터 등 추가 필요

public AddPostRequest(Category category, String title, String subTitle, String description, String level, String time, String distance, String courseDetail, String transportation, Long districtId, GetGeometryResponse geometry) {
// 생성자에 @JsonProperty 어노테이션 추가
public AddPostRequest(@JsonProperty("category") Category category,
@JsonProperty("title") String title,
@JsonProperty("subTitle") String subTitle,
@JsonProperty("description") String description,
@JsonProperty("level") String level,
@JsonProperty("time") String time,
@JsonProperty("distance") String distance,
@JsonProperty("courseDetail") String courseDetail,
@JsonProperty("transportation") String transportation,
@JsonProperty("districtId") Long districtId,
@JsonProperty("geometry") CoordinateRequest geometry) {
this.category = category;
this.title = title;
this.subTitle = subTitle;
Expand All @@ -60,4 +73,8 @@ public AddPostRequest(Category category, String title, String subTitle, String d
this.districtId = districtId;
this.geometry = geometry;
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +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;
import com.seoultech.sanEseo.public_api.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -46,7 +43,7 @@ public Post addPost(AddPostRequest request) {
request.getLevel(), request.getTime(), request.getDistance(), request.getCourseDetail(),
request.getTransportation());

GetGeometryResponse geometry = request.getGeometry();
CoordinateRequest geometry = request.getGeometry();
coordinateService.saveCoordinate(geometry, post);

postPort.save(post); // Post 저장
Expand Down Expand Up @@ -82,7 +79,7 @@ public void updatePost(Long postId, UpdatePostRequest request) {

// 좌표 정보 업데이트
Coordinate coordinate = coordinateService.findCoordinate(post);
coordinate.update(request.getGeometry().getName(), request.getGeometry().getType(), request.getGeometry());
coordinate.update(request.getGeometry().getName(), request.getGeometry().getType(), request.getGeometry().getCoordinates());

// 게시글 정보 업데이트
post.update(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.seoultech.sanEseo.post.domain.Category;
import com.seoultech.sanEseo.image.PostImage;
import com.seoultech.sanEseo.public_api.GetGeometryResponse;
import com.seoultech.sanEseo.public_api.CoordinateRequest;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
Expand Down Expand Up @@ -44,9 +44,9 @@ public class UpdatePostRequest {
private Long districtId;

@NotNull(message = "좌표 정보는 필수입니다.")
private GetGeometryResponse geometry;
private CoordinateRequest 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) {
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, CoordinateRequest geometry) {
this.category = category;
this.title = title;
this.subTitle = subTitle;
Expand Down
21 changes: 7 additions & 14 deletions src/main/java/com/seoultech/sanEseo/public_api/Coordinate.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,25 @@ public class Coordinate {
private String name;
private String type;

@ElementCollection
@CollectionTable(name = "coordinate_points", joinColumns = @JoinColumn(name = "coordinate_id"))
private List<LatLng> coordinates;

@Convert(converter = CoordinatesConverter.class)
@Column(columnDefinition = "TEXT")
private List<List<Double>> coordinates;

@OneToOne
@JoinColumn(name = "post_id")
private Post post;

public Coordinate(String name, String type, List<LatLng> coordinates, Post post) {
public Coordinate(String name, String type, List<List<Double>> coordinates, Post post) {
this.name = name;
this.type = type;
this.coordinates = coordinates;
this.post = post;
}
public void update(String name, String type, GetGeometryResponse geometry) {
public void update(String name, String type, List<List<Double>> coordinates) {
this.name = name;
this.type = type;
this.coordinates = convertGeometryToLatLngList(geometry);
this.coordinates = coordinates;
}

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
@@ -0,0 +1,18 @@
package com.seoultech.sanEseo.public_api;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

@AllArgsConstructor
@Getter
public class CoordinateRequest {

private String name;
private String type;

private List<List<Double>> coordinates;

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CoordinateService {
private CoordinateRepository coordinateRepository;

@Transactional
public void saveCoordinate(GetGeometryResponse geometryResponse, Post post) {
public void saveCoordinate(CoordinateRequest geometryResponse, Post post) {
Coordinate coordinate = new Coordinate(
geometryResponse.getName(),
geometryResponse.getType(),
Expand Down
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<LatLng>, String> {
public class CoordinatesConverter implements AttributeConverter<List<List<Double>>, String> {
private final static ObjectMapper objectMapper = new ObjectMapper();

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

@Override
public List<LatLng> convertToEntityAttribute(String dbData) {
public List<List<Double>> convertToEntityAttribute(String dbData) {
try {
return objectMapper.readValue(dbData, new TypeReference<List<LatLng>>() {});
return objectMapper.readValue(dbData, new TypeReference<List<List<Double>>>() {});
} catch (IOException e) {
throw new RuntimeException("Conversion error", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.seoultech.sanEseo.public_api;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class CoordinatesDeserializer extends JsonDeserializer<List<List<Double>>> {
@Override
public List<List<Double>> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
List<List<Double>> coordinates = new ArrayList<>();
while (p.nextToken() != JsonToken.END_ARRAY) {
JsonNode node = p.getCodec().readTree(p);
double lat = node.get("lat").asDouble();
double lng = node.get("lng").asDouble();
List<Double> coordPair = new ArrayList<>();
coordPair.add(lat);
coordPair.add(lng);
coordinates.add(coordPair);
}
return coordinates;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.seoultech.sanEseo.public_api;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.util.List;

public class CoordinatesSerializer extends JsonSerializer<List<List<Double>>> {
@Override
public void serialize(List<List<Double>> value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeStartArray();
for (List<Double> coord : value) {
gen.writeStartObject();
gen.writeNumberField("lat", coord.get(0));
gen.writeNumberField("lng", coord.get(1));
gen.writeEndObject();
}
gen.writeEndArray();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.seoultech.sanEseo.public_api;


import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.AllArgsConstructor;
import lombok.Getter;

Expand All @@ -11,5 +12,7 @@
public class GetCoordinateResponse {

String type;
List<LatLng> coordinates;

@JsonSerialize(using = CoordinatesSerializer.class)
List<List<Double>> coordinates;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
public class GetGeometryResponse {
private String type;
private String name;
private List<LatLng> coordinates;
private List<List<Double>> coordinates;
}
33 changes: 0 additions & 33 deletions src/main/java/com/seoultech/sanEseo/public_api/LatLng.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,16 @@ public List<GetGeometryResponse> parsingCoordinate(int dataIndex) {
for (Feature feature : featureCollection.getFeatures()) {
MultiLineString multiLineString = (MultiLineString) feature.getGeometry();
String name = feature.getProperty("NAME");
List<List<Double>> coordinate_list = new ArrayList<>();
for (List<LngLatAlt> coordinates : multiLineString.getCoordinates()) {
List<LatLng> latLngList = new ArrayList<>();
for (LngLatAlt lngLatAlt : coordinates) {

double latitude = lngLatAlt.getLatitude();
double longitude = lngLatAlt.getLongitude();
latLngList.add(new LatLng(latitude, longitude));
coordinate_list.add(List.of(latitude, longitude));

}
responses.add(new GetGeometryResponse("polyline", name, latLngList));
responses.add(new GetGeometryResponse("polyline", name, coordinate_list));
}
}
} catch (IOException e) {
Expand Down Expand Up @@ -166,7 +168,7 @@ public void addPublicData(int dataIndex){
safeSubstring(getCourseResponse.getDescription(), 0, 255),
getCourseResponse.getLevel(), getCourseResponse.getTime(),
getCourseResponse.getDistance(), safeSubstring(getCourseResponse.getCourseDetail(), 0 ,255),
getCourseResponse.getTransportation(), id, getGeometryResponse));
getCourseResponse.getTransportation(), id, convertToCoordinateRequest(getGeometryResponse)));


}
Expand Down Expand Up @@ -198,6 +200,11 @@ public boolean isNameSimilar(String name1, String name2) {
return score > 0.95; // 유사도 점수가 0.85 이상이면 유사하다고 판단
}

public CoordinateRequest convertToCoordinateRequest(GetGeometryResponse geometry){
return new CoordinateRequest(geometry.getName(), geometry.getType(), geometry.getCoordinates());

}



}

0 comments on commit 67b4f25

Please sign in to comment.