Skip to content

Commit

Permalink
Merge pull request #17 from KHTML-Hexagonal/develop
Browse files Browse the repository at this point in the history
[FEAT]: Building 추천 api 구현
  • Loading branch information
sejineer authored Aug 13, 2024
2 parents f5d764c + ce665d8 commit 4bec719
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 40 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ dependencies {
implementation 'io.jsonwebtoken:jjwt-impl:0.12.6'
implementation 'io.jsonwebtoken:jjwt-jackson:0.12.6'

//query dsl
implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta'
implementation 'com.querydsl:querydsl-sql:5.0.0'
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"

implementation 'com.azure:azure-storage-blob:12.27.0'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.RequiredArgsConstructor;
import org.khtml.hexagonal.domain.building.ImageType;
import org.khtml.hexagonal.domain.building.dto.BuildingUpdate;
import org.khtml.hexagonal.domain.building.dto.RecommendBuildingResult;
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;
Expand All @@ -22,7 +22,6 @@
import java.util.List;
import java.util.Objects;

import static org.khtml.hexagonal.domain.building.dto.RecommendBuildingResult.*;

@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand Down Expand Up @@ -130,26 +129,15 @@ public void updateBuildingDescription(String buildingId, Long userId, String des
Building building = buildingRepository.findBuildingByGisBuildingId(buildingId)
.orElseThrow(() -> new IllegalArgumentException("Building not found"));

if(!Objects.equals(building.getUser().getId(), userId)) {
if (!Objects.equals(building.getUser().getId(), userId)) {
throw new IllegalArgumentException(ErrorType.DEFAULT_ERROR.getMessage());
}

building.setBuildingDescription(description);
}

public RecommendBuildingResult recommendBuilding() {
List<BuildingImage> buildingImages = buildingImageRepository.recommendBuilding();
List<RecommendBuilding> recommendBuildings = new ArrayList<>();
for(BuildingImage buildingImage : buildingImages) {
Building building = buildingImage.getBuilding();
Image image = buildingImage.getImage();
recommendBuildings.add(new RecommendBuilding(
image.getUrl(),
building.getLegalDistrictName() + " " + building.getLandLotNumber(),
building.getRepairList()
));
}

return new RecommendBuildingResult(recommendBuildings);
public List<RecommendBuilding> recommendBuilding() {
return buildingImageRepository.recommendBuilding();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.khtml.hexagonal.domain.building.dto;

import com.querydsl.core.annotations.QueryProjection;
import lombok.Builder;
import lombok.Data;

@Data
public class RecommendBuilding {

private String buildingId;
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) {
this.buildingId = buildingId;
this.imageUrl = imageUrl;
this.address = address;
this.repairList = repairList;
this.totalScore = totalScore;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@

import java.util.List;

public interface BuildingImageRepository extends JpaRepository<BuildingImage, Long> {
public interface BuildingImageRepository extends JpaRepository<BuildingImage, Long>, BuildingQueryDslRepository {

@EntityGraph(attributePaths = {"image", "building"})
List<BuildingImage> findAllByBuilding(Building building);

@Query("SELECT bi FROM BuildingImage bi " +
"JOIN Building b ON bi.building.gisBuildingId = b.gisBuildingId " +
"JOIN Image i ON bi.image.id = i.id " +
"WHERE b.buildingStatus = 'REGISTERED' ORDER BY b.totalScore DESC LIMIT 10")
List<BuildingImage> recommendBuilding();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.khtml.hexagonal.domain.building.repository;

import org.khtml.hexagonal.domain.building.dto.RecommendBuilding;

import java.util.List;


public interface BuildingQueryDslRepository {

List<RecommendBuilding> recommendBuilding();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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> 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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.khtml.hexagonal.global.config;

import com.querydsl.jpa.impl.JPAQueryFactory;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class QueryDslConfig {

@PersistenceContext
private EntityManager entityManager;

@Bean
public JPAQueryFactory queryFactory() {
return new JPAQueryFactory(entityManager);
}

}

0 comments on commit 4bec719

Please sign in to comment.