Skip to content

Commit

Permalink
Merge pull request #66 from Korea-Certified-Store/feature/fix-mbr-to-…
Browse files Browse the repository at this point in the history
…polygon(#65)

가게 조회 API MBR 영역에서 Polygon 영역으로 변경 (#65)
  • Loading branch information
sungjindev authored Jan 19, 2024
2 parents ab38aa8 + 8c7c529 commit 4c536ea
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class StoreCertificationApi {
"certificationName: 가게의 인증제 목록<br>" +
"=> 각 인증제별 순서는 보장되지 않습니다.")
@GetMapping("api/v1/storecertification/byLocation")
public Result<List<StoreCertificationsByLocationResponse>> findStoreCertificationsByLocation(@RequestParam("nwLong") double nwLong, @RequestParam("nwLat") double nwLat, @RequestParam("seLong") double seLong, @RequestParam("seLat") double seLat) {
List<StoreCertification> storeCertificationsByLocation = storeCertificationService.findStoreCertificationsByLocation(new Location(nwLong, nwLat), new Location(seLong, seLat));
public Result<List<StoreCertificationsByLocationResponse>> findStoreCertificationsByLocation(@RequestParam("nwLong") double nwLong, @RequestParam("nwLat") double nwLat, @RequestParam("swLong") double swLong, @RequestParam("swLat") double swLat, @RequestParam("seLong") double seLong, @RequestParam("seLat") double seLat, @RequestParam("neLong") double neLong, @RequestParam("neLat") double neLat) {
List<StoreCertification> storeCertificationsByLocation = storeCertificationService.findStoreCertificationsByLocation(new Location(nwLong, nwLat), new Location(swLong, swLat), new Location(seLong, seLat), new Location(neLong, neLat));
List<Long> storeIdsWithMultipleCertifications = storeCertificationService.findStoreIdsWithMultipleCertifications(); //여러 인증제를 가지고 있는 가게의 id 리스트
List<StoreCertificationsByLocationResponse> storeCertificationsByLocationResponses = new ArrayList<>(); //반환해줄 StoreCertificationsByLocationResponse들의 List
Map<Long, StoreCertificationsByLocationResponse> map = new HashMap<>(); //여러 인증제를 가지고 있는 가게들의 response를 임시로 저장하고 있을 map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
@RequiredArgsConstructor
public class StoreCertificationService {
private final StoreCertificationRepository storeCertificationRepository;
public List<StoreCertification> findStoreCertificationsByLocation(Location northWestLocation, Location southEastLocation) {
return storeCertificationRepository.findStoreCertificationsByLocation(northWestLocation, southEastLocation);
public List<StoreCertification> findStoreCertificationsByLocation(Location northWestLocation, Location southWestLocation, Location southEastLocation, Location northEastLocation) {
return storeCertificationRepository.findStoreCertificationsByLocation(northWestLocation, southWestLocation, southEastLocation, northEastLocation);
}

public List<Long> findStoreIdsWithMultipleCertifications() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public Optional<StoreCertification> findByStoreIdCertificationId(Long storeId, L
return result.stream().findAny();
}

//북서쪽 좌표와 남동쪽 좌표를 받아 그 좌표로 만들어지는 최소 사각형 내에 위치하는 가게들 리턴
public List<StoreCertification> findStoreCertificationsByLocation(Location northWestLocation, Location southEastLocation) {
//북서쪽 좌표, 남서쪽 좌표, 남동쪽 좌표, 북동쪽 좌표를 받아 그 좌표로 만들어지는 사각형 영역 내에 위치하는 가게들 리턴
public List<StoreCertification> findStoreCertificationsByLocation(Location northWestLocation, Location southWestLocation, Location southEastLocation, Location northEastLocation) {
String pointFormat = String.format(
"'LINESTRING(%f %f, %f %f)'", //POINT는 (경도, 위도) 순이다. 즉, (Logitude, Latitude)순
northWestLocation.getLongitude(), northWestLocation.getLatitude(), southEastLocation.getLongitude(), southEastLocation.getLatitude()
"'POLYGON((%f %f, %f %f, %f %f, %f %f, %f %f))'", //POINT는 (경도, 위도) 순이다. 즉, (Logitude, Latitude)순
northWestLocation.getLongitude(), northWestLocation.getLatitude(), southWestLocation.getLongitude(), southWestLocation.getLatitude(), southEastLocation.getLongitude(), southEastLocation.getLatitude(), northEastLocation.getLongitude(), northEastLocation.getLatitude(), northWestLocation.getLongitude(), northWestLocation.getLatitude()
);

Query query = em.createNativeQuery("SELECT sc.* " + "FROM store_certification AS sc " + "JOIN store AS s ON sc.store_id = s.store_id " + "JOIN certification AS c ON sc.certification_id = c.certification_id " + "WHERE MBRCONTAINS(ST_LINESTRINGFROMTEXT(" + pointFormat + "), s.location)", StoreCertification.class);
Query query = em.createNativeQuery("SELECT sc.* " + "FROM store_certification AS sc " + "JOIN store AS s ON sc.store_id = s.store_id " + "JOIN certification AS c ON sc.certification_id = c.certification_id " + "WHERE ST_CONTAINS(ST_POLYGONFROMTEXT(" + pointFormat + "), s.location)", StoreCertification.class);

return query.getResultList();
}
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/com/nainga/nainga/global/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.nainga.nainga.global.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
import io.swagger.v3.oas.models.Paths;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;
import io.swagger.v3.oas.models.tags.Tag;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class SwaggerConfig {

@Bean
public OpenAPI customOpenAPI() {
//Google Cloud Storage API는 별도로 Swagger에 명세
return new OpenAPI()
.paths(new Paths().addPathItem("https://storage.googleapis.com/{BUCKET_NAME}/{IMAGE_NAME}",
new PathItem().get(new Operation().summary("저장된 가게 이미지 제공")
.description("저장된 가게 이미지를 제공하는 API입니다.<br>" +
"본 API는 Google Cloud Storage에서 제공하는 API로 URL이 위와 같으며 이 정보는 각 가게별 local_photos 필드에 저장되어 있습니다.<br>" +
"Dev 환경에서 BUCKET_NAME은 kcs-dev-bucket1이고 Prod 환경에서 BUCKET_NAME은 kcs-prod-bucket1입니다.<br>" +
"가게 이름은 UUID를 활용한 난수로 제공됩니다.<br>" +
"참고로 Swagger 상에서는 Base URL이 달라 테스트가 불가능합니다.<br>" +
"만약 테스트를 원하신다면 브라우저 상에서 직접 URL을 입력해주시면 됩니다.<br>" +
"예) https://storage.googleapis.com/kcs-dev-bucket1/ad06294c-d4ed-42bd-9839-82af8714bd1e")
.tags(List.of("가게 상세 정보"))
.responses(new ApiResponses().addApiResponse("200",
new ApiResponse().description("OK")
.content(new Content().addMediaType("image/jpeg", new MediaType()
.schema(new Schema<>().type("string")
.format("binary")))))))));
}
}

2 changes: 1 addition & 1 deletion src/main/resources/backend-submodule
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ void findStoreCertificationsByLocation() {
}

//when
Location location1 = new Location(minLongitude - 1.0, minLatitude - 1.0);
Location location2 = new Location(maxLongitude + 1.0, maxLatitude + 1.0);
List<StoreCertification> storeCertificationsByLocation = storeCertificationService.findStoreCertificationsByLocation(location1, location2);
Location location1 = new Location(minLongitude - 1.0, maxLatitude + 1.0);
Location location2 = new Location(minLongitude - 1.0, minLatitude - 1.0);
Location location3 = new Location(maxLongitude + 1.0, minLatitude - 1.0);
Location location4 = new Location(maxLongitude + 1.0, maxLatitude + 1.0);
List<StoreCertification> storeCertificationsByLocation = storeCertificationService.findStoreCertificationsByLocation(location1, location2, location3, location4);

//then
assertThat(storeCertificationsByLocation.size()).isEqualTo(stores.size());
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/backend-submodule

0 comments on commit 4c536ea

Please sign in to comment.