Skip to content

Commit

Permalink
Merge pull request #19 from KHTML-Hexagonal/develop
Browse files Browse the repository at this point in the history
[FEAT]: Building 추천 api 구현 (#18)
  • Loading branch information
sejineer authored Aug 13, 2024
2 parents 4bec719 + 5b82fe4 commit c0bfdc7
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 128 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -49,10 +47,10 @@ public List<String> getBuildingImages(String buildingId) {
Building building = buildingRepository.findBuildingByGisBuildingId(buildingId)
.orElseThrow(() -> new IllegalArgumentException("Building not found"));

List<BuildingImage> buildingImages = buildingImageRepository.findAllByBuilding(building);
List<Image> buildingImages = imageRepository.findAllByBuilding(building);
List<String> images = new ArrayList<>();
for (BuildingImage buildingImage : buildingImages) {
images.add(buildingImage.getImage().getUrl());
for (Image buildingImage : buildingImages) {
images.add(buildingImage.getUrl());
}

return images;
Expand Down Expand Up @@ -98,18 +96,12 @@ public List<String> registerBuilding(String buildingId, User requestUser, List<M

Image image = Image.builder()
.url(url)
.building(building)
.imageType(ImageType.BUILDING)
.user(user)
.build();

BuildingImage buildingImage = BuildingImage.builder()
.building(building)
.image(image)
.build();

imageRepository.save(image);
buildingImageRepository.save(buildingImage);

ret.add(url);
}

Expand Down Expand Up @@ -137,7 +129,23 @@ public void updateBuildingDescription(String buildingId, Long userId, String des
}

public List<RecommendBuilding> recommendBuilding() {
return buildingImageRepository.recommendBuilding();
List<Building> buildings = buildingRepository.findAllByBuildingStatus(BuildingStatus.REGISTERED);
List<RecommendBuilding> 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;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -119,6 +122,9 @@ public class Building {
@JoinColumn(name = "user_id")
private User user;

@OneToMany(mappedBy = "building")
private List<Image> images = new ArrayList<>();

public void updateUser(User user) {
this.user = user;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,4 +14,6 @@ public interface BuildingRepository extends JpaRepository<Building, Long> {
@EntityGraph(attributePaths = {"user"})
Optional<Building> findBuildingByGisBuildingId(String gisBuildingId);

List<Building> findAllByBuildingStatus(BuildingStatus buildingStatus);

}
Original file line number Diff line number Diff line change
@@ -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<Image, Long> {

List<Image> findAllByBuilding(Building building);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -47,13 +44,11 @@ public void registerMaterials(String buildingId, User requestUser, List<Multipar
for (MultipartFile file : multipartFiles) {
String url = blobManager.storeFile(file.getOriginalFilename(), file.getInputStream(), file.getSize());

Image image = Image.builder().url(url).imageType(ImageType.MATERIAL).user(user).build();
BuildingImage buildingImage = BuildingImage.builder().image(image).building(building).build();
Image image = Image.builder().url(url).building(building).imageType(ImageType.MATERIAL).user(user).build();

urls.add(url);

imageRepository.save(image);
buildingImageRepository.save(buildingImage);
}

MaterialResult materialResult = gptManager.analyzeMaterial(urls);
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,14 @@ spring:
max-file-size: 50MB
max-request-size: 50MB

jpa:
hibernate:
ddl-auto: validate
properties:
hibernate:
format_sql: true
show_sql: true
h2:
console:
enabled: true

0 comments on commit c0bfdc7

Please sign in to comment.