From 5b82fe46ecf9bb682287b0528337d79e44795bec Mon Sep 17 00:00:00 2001 From: Sejin Park <95167215+sejineer@users.noreply.github.com> Date: Wed, 14 Aug 2024 03:28:21 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]:=20Building=20=EC=B6=94=EC=B2=9C=20api?= =?UTF-8?q?=20=EA=B5=AC=ED=98=84=20(#18)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../building/application/BuildingService.java | 36 ++++++++++------ .../controller/BuildingController.java | 2 - .../building/dto/RecommendBuilding.java | 5 +-- .../domain/building/entity/Building.java | 6 +++ .../domain/building/entity/BuildingImage.java | 31 ------------- .../domain/building/entity/Image.java | 7 ++- .../repository/BuildingImageRepository.java | 16 ------- .../BuildingQueryDslRepository.java | 11 ----- .../BuildingQueryDslRepositoryImpl.java | 43 ------------------- .../repository/BuildingRepository.java | 3 ++ .../building/repository/ImageRepository.java | 5 +++ .../domain/material/MaterialService.java | 7 +-- src/main/resources/application.yml | 11 +++++ 13 files changed, 55 insertions(+), 128 deletions(-) delete mode 100644 src/main/java/org/khtml/hexagonal/domain/building/entity/BuildingImage.java delete mode 100644 src/main/java/org/khtml/hexagonal/domain/building/repository/BuildingImageRepository.java delete mode 100644 src/main/java/org/khtml/hexagonal/domain/building/repository/BuildingQueryDslRepository.java delete mode 100644 src/main/java/org/khtml/hexagonal/domain/building/repository/BuildingQueryDslRepositoryImpl.java diff --git a/src/main/java/org/khtml/hexagonal/domain/building/application/BuildingService.java b/src/main/java/org/khtml/hexagonal/domain/building/application/BuildingService.java index 2afb054..77485de 100644 --- a/src/main/java/org/khtml/hexagonal/domain/building/application/BuildingService.java +++ b/src/main/java/org/khtml/hexagonal/domain/building/application/BuildingService.java @@ -1,13 +1,12 @@ package org.khtml.hexagonal.domain.building.application; import lombok.RequiredArgsConstructor; +import org.khtml.hexagonal.domain.building.BuildingStatus; import org.khtml.hexagonal.domain.building.ImageType; import org.khtml.hexagonal.domain.building.dto.BuildingUpdate; import org.khtml.hexagonal.domain.building.dto.RecommendBuilding; import org.khtml.hexagonal.domain.building.entity.Building; -import org.khtml.hexagonal.domain.building.entity.BuildingImage; import org.khtml.hexagonal.domain.building.entity.Image; -import org.khtml.hexagonal.domain.building.repository.BuildingImageRepository; import org.khtml.hexagonal.domain.building.repository.BuildingRepository; import org.khtml.hexagonal.domain.building.repository.ImageRepository; import org.khtml.hexagonal.domain.user.User; @@ -29,7 +28,6 @@ public class BuildingService { private final BuildingRepository buildingRepository; - private final BuildingImageRepository buildingImageRepository; private final ImageRepository imageRepository; private final UserRepository userRepository; private final BlobManager blobManager; @@ -49,10 +47,10 @@ public List getBuildingImages(String buildingId) { Building building = buildingRepository.findBuildingByGisBuildingId(buildingId) .orElseThrow(() -> new IllegalArgumentException("Building not found")); - List buildingImages = buildingImageRepository.findAllByBuilding(building); + List buildingImages = imageRepository.findAllByBuilding(building); List images = new ArrayList<>(); - for (BuildingImage buildingImage : buildingImages) { - images.add(buildingImage.getImage().getUrl()); + for (Image buildingImage : buildingImages) { + images.add(buildingImage.getUrl()); } return images; @@ -98,18 +96,12 @@ public List registerBuilding(String buildingId, User requestUser, List recommendBuilding() { - return buildingImageRepository.recommendBuilding(); + List buildings = buildingRepository.findAllByBuildingStatus(BuildingStatus.REGISTERED); + List recommendBuildings = new ArrayList<>(); + for (Building building : buildings) { + String imageUrl = null; + if (building.getImages() != null && !building.getImages().isEmpty()) { + imageUrl = building.getImages().getFirst().getUrl(); + } + + recommendBuildings.add(RecommendBuilding.builder() + .buildingId(building.getGisBuildingId()) + .imageUrl(imageUrl) + .address(building.getLegalDistrictName() + " " + building.getLandLotNumber()) + .repairList(building.getRepairList()) + .build()); + } + return recommendBuildings; } + } diff --git a/src/main/java/org/khtml/hexagonal/domain/building/controller/BuildingController.java b/src/main/java/org/khtml/hexagonal/domain/building/controller/BuildingController.java index c5e708a..223d440 100644 --- a/src/main/java/org/khtml/hexagonal/domain/building/controller/BuildingController.java +++ b/src/main/java/org/khtml/hexagonal/domain/building/controller/BuildingController.java @@ -6,8 +6,6 @@ import org.khtml.hexagonal.domain.auth.JwtValidator; import org.khtml.hexagonal.domain.building.application.BuildingService; import org.khtml.hexagonal.domain.building.entity.Building; -import org.khtml.hexagonal.domain.building.entity.Image; -import org.khtml.hexagonal.domain.building.repository.BuildingImageRepository; import org.khtml.hexagonal.domain.user.User; import org.khtml.hexagonal.global.support.response.ApiResponse; import org.springframework.web.bind.annotation.*; diff --git a/src/main/java/org/khtml/hexagonal/domain/building/dto/RecommendBuilding.java b/src/main/java/org/khtml/hexagonal/domain/building/dto/RecommendBuilding.java index 3fac3b2..9e37cf7 100644 --- a/src/main/java/org/khtml/hexagonal/domain/building/dto/RecommendBuilding.java +++ b/src/main/java/org/khtml/hexagonal/domain/building/dto/RecommendBuilding.java @@ -11,16 +11,13 @@ public class RecommendBuilding { private String imageUrl; private String address; private String repairList; - private Integer totalScore; @Builder @QueryProjection - public RecommendBuilding(String buildingId, String imageUrl, String address, String repairList, Integer totalScore) { + public RecommendBuilding(String buildingId, String imageUrl, String address, String repairList) { this.buildingId = buildingId; this.imageUrl = imageUrl; this.address = address; this.repairList = repairList; - this.totalScore = totalScore; } - } diff --git a/src/main/java/org/khtml/hexagonal/domain/building/entity/Building.java b/src/main/java/org/khtml/hexagonal/domain/building/entity/Building.java index 22578d7..366d533 100644 --- a/src/main/java/org/khtml/hexagonal/domain/building/entity/Building.java +++ b/src/main/java/org/khtml/hexagonal/domain/building/entity/Building.java @@ -8,6 +8,9 @@ import org.khtml.hexagonal.domain.building.dto.BuildingUpdate; import org.khtml.hexagonal.domain.user.User; +import java.util.ArrayList; +import java.util.List; + @Table(name = "building") @Getter @Setter @@ -119,6 +122,9 @@ public class Building { @JoinColumn(name = "user_id") private User user; + @OneToMany(mappedBy = "building") + private List images = new ArrayList<>(); + public void updateUser(User user) { this.user = user; } diff --git a/src/main/java/org/khtml/hexagonal/domain/building/entity/BuildingImage.java b/src/main/java/org/khtml/hexagonal/domain/building/entity/BuildingImage.java deleted file mode 100644 index 943c997..0000000 --- a/src/main/java/org/khtml/hexagonal/domain/building/entity/BuildingImage.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.khtml.hexagonal.domain.building.entity; - - -import jakarta.persistence.*; -import lombok.AccessLevel; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; -import org.khtml.hexagonal.domain.common.BaseEntity; - -@Table(name = "building_image") -@Getter -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@Entity -public class BuildingImage extends BaseEntity { - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "building_id") - private Building building; - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "image_id") - private Image image; - - @Builder - public BuildingImage(Building building, Image image) { - this.building = building; - this.image = image; - } - -} diff --git a/src/main/java/org/khtml/hexagonal/domain/building/entity/Image.java b/src/main/java/org/khtml/hexagonal/domain/building/entity/Image.java index b8ad672..94e8237 100644 --- a/src/main/java/org/khtml/hexagonal/domain/building/entity/Image.java +++ b/src/main/java/org/khtml/hexagonal/domain/building/entity/Image.java @@ -20,14 +20,19 @@ public class Image extends BaseEntity { @Enumerated(EnumType.STRING) private ImageType imageType; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "building_id") + private Building building; + @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") private User user; @Builder - public Image(String url, ImageType imageType, User user) { + public Image(String url, ImageType imageType, Building building, User user) { this.url = url; this.imageType = imageType; + this.building = building; this.user = user; } diff --git a/src/main/java/org/khtml/hexagonal/domain/building/repository/BuildingImageRepository.java b/src/main/java/org/khtml/hexagonal/domain/building/repository/BuildingImageRepository.java deleted file mode 100644 index 70fdc71..0000000 --- a/src/main/java/org/khtml/hexagonal/domain/building/repository/BuildingImageRepository.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.khtml.hexagonal.domain.building.repository; - -import org.khtml.hexagonal.domain.building.entity.Building; -import org.khtml.hexagonal.domain.building.entity.BuildingImage; -import org.springframework.data.jpa.repository.EntityGraph; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; - -import java.util.List; - -public interface BuildingImageRepository extends JpaRepository, BuildingQueryDslRepository { - - @EntityGraph(attributePaths = {"image", "building"}) - List findAllByBuilding(Building building); - -} diff --git a/src/main/java/org/khtml/hexagonal/domain/building/repository/BuildingQueryDslRepository.java b/src/main/java/org/khtml/hexagonal/domain/building/repository/BuildingQueryDslRepository.java deleted file mode 100644 index d37452c..0000000 --- a/src/main/java/org/khtml/hexagonal/domain/building/repository/BuildingQueryDslRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.khtml.hexagonal.domain.building.repository; - -import org.khtml.hexagonal.domain.building.dto.RecommendBuilding; - -import java.util.List; - - -public interface BuildingQueryDslRepository { - - List recommendBuilding(); -} diff --git a/src/main/java/org/khtml/hexagonal/domain/building/repository/BuildingQueryDslRepositoryImpl.java b/src/main/java/org/khtml/hexagonal/domain/building/repository/BuildingQueryDslRepositoryImpl.java deleted file mode 100644 index 6dac53e..0000000 --- a/src/main/java/org/khtml/hexagonal/domain/building/repository/BuildingQueryDslRepositoryImpl.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.khtml.hexagonal.domain.building.repository; - -import com.querydsl.core.types.dsl.Expressions; -import com.querydsl.jpa.impl.JPAQueryFactory; -import lombok.RequiredArgsConstructor; -import org.khtml.hexagonal.domain.building.BuildingStatus; -import org.khtml.hexagonal.domain.building.dto.QRecommendBuilding; -import org.khtml.hexagonal.domain.building.dto.RecommendBuilding; - -import java.beans.Expression; -import java.util.List; - -import static com.querydsl.core.types.dsl.Expressions.stringTemplate; -import static org.khtml.hexagonal.domain.building.entity.QBuilding.*; -import static org.khtml.hexagonal.domain.building.entity.QBuildingImage.*; -import static org.khtml.hexagonal.domain.building.entity.QImage.*; - -@RequiredArgsConstructor -public class BuildingQueryDslRepositoryImpl implements BuildingQueryDslRepository { - - private final JPAQueryFactory queryFactory; - - @Override - public List recommendBuilding() { - return queryFactory - .select(new QRecommendBuilding( - building.gisBuildingId, - image.url, - Expressions.stringTemplate("CONCAT({0}, ' ', {1})", building.legalDistrictName, building.landLotNumber), - building.repairList, - building.totalScore - )) - .from(buildingImage) - .leftJoin(building).on(buildingImage.building.eq(building)) - .leftJoin(image).on(buildingImage.image.eq(image)) - .where(building.buildingStatus.eq(BuildingStatus.REGISTERED)) - .distinct() - .orderBy(building.totalScore.desc()) - .limit(10) - .fetch(); - } - -} diff --git a/src/main/java/org/khtml/hexagonal/domain/building/repository/BuildingRepository.java b/src/main/java/org/khtml/hexagonal/domain/building/repository/BuildingRepository.java index 710c1a1..9140b8e 100644 --- a/src/main/java/org/khtml/hexagonal/domain/building/repository/BuildingRepository.java +++ b/src/main/java/org/khtml/hexagonal/domain/building/repository/BuildingRepository.java @@ -1,5 +1,6 @@ package org.khtml.hexagonal.domain.building.repository; +import org.khtml.hexagonal.domain.building.BuildingStatus; import org.khtml.hexagonal.domain.building.entity.Building; import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; @@ -13,4 +14,6 @@ public interface BuildingRepository extends JpaRepository { @EntityGraph(attributePaths = {"user"}) Optional findBuildingByGisBuildingId(String gisBuildingId); + List findAllByBuildingStatus(BuildingStatus buildingStatus); + } diff --git a/src/main/java/org/khtml/hexagonal/domain/building/repository/ImageRepository.java b/src/main/java/org/khtml/hexagonal/domain/building/repository/ImageRepository.java index 0fec944..55b8150 100644 --- a/src/main/java/org/khtml/hexagonal/domain/building/repository/ImageRepository.java +++ b/src/main/java/org/khtml/hexagonal/domain/building/repository/ImageRepository.java @@ -1,7 +1,12 @@ package org.khtml.hexagonal.domain.building.repository; +import org.khtml.hexagonal.domain.building.entity.Building; import org.khtml.hexagonal.domain.building.entity.Image; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface ImageRepository extends JpaRepository { + + List findAllByBuilding(Building building); } diff --git a/src/main/java/org/khtml/hexagonal/domain/material/MaterialService.java b/src/main/java/org/khtml/hexagonal/domain/material/MaterialService.java index 8bdc255..7ebac43 100644 --- a/src/main/java/org/khtml/hexagonal/domain/material/MaterialService.java +++ b/src/main/java/org/khtml/hexagonal/domain/material/MaterialService.java @@ -6,9 +6,7 @@ import org.khtml.hexagonal.domain.building.application.BlobManager; import org.khtml.hexagonal.domain.building.dto.MaterialResult; import org.khtml.hexagonal.domain.building.entity.Building; -import org.khtml.hexagonal.domain.building.entity.BuildingImage; import org.khtml.hexagonal.domain.building.entity.Image; -import org.khtml.hexagonal.domain.building.repository.BuildingImageRepository; import org.khtml.hexagonal.domain.building.repository.BuildingRepository; import org.khtml.hexagonal.domain.building.repository.ImageRepository; import org.khtml.hexagonal.domain.user.User; @@ -29,7 +27,6 @@ public class MaterialService { private final BuildingRepository buildingRepository; - private final BuildingImageRepository buildingImageRepository; private final ImageRepository imageRepository; private final UserRepository userRepository; private final MaterialRepository materialRepository; @@ -47,13 +44,11 @@ public void registerMaterials(String buildingId, User requestUser, List