Skip to content

Commit

Permalink
Merge pull request #1326 from innovationacademy-kr/be/dev/querydsl/#1271
Browse files Browse the repository at this point in the history


[BE] QueryDSL 레포지토리 추가
  • Loading branch information
enaenen authored Aug 24, 2023
2 parents 737810d + e8f72b9 commit 24e76eb
Show file tree
Hide file tree
Showing 19 changed files with 407 additions and 58 deletions.
4 changes: 4 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-mail'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'

// cache
implementation 'org.springframework.boot:spring-boot-starter-cache'


// querydsl
implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public List<CabinetsPerSectionResponseDto> getCabinetsPerSection(
@PathVariable("building") String building,
@PathVariable("floor") Integer floor) {
log.info("Called getCabinetsPerSection");
return cabinetFacadeService.getCabinetsPerSection(building, floor);
return cabinetFacadeService.getCabinetsPerSectionDSL(building, floor);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.ftclub.cabinet.cabinet.domain;

import java.util.List;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
Expand Down Expand Up @@ -45,7 +47,10 @@ public class CabinetPlace {
*/
@Embedded
private MapArea mapArea;

/* 양방향 매핑
@OneToMany(mappedBy = "cabinetPlace")
private List<Cabinet> cabinets;
*/
protected CabinetPlace(Location location, SectionFormation sectionFormation, MapArea mapArea) {
this.location = location;
this.sectionFormation = sectionFormation;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.ftclub.cabinet.cabinet.repository;

import java.util.List;
import org.ftclub.cabinet.cabinet.domain.Cabinet;
import org.ftclub.cabinet.cabinet.domain.CabinetPlace;
import org.ftclub.cabinet.dto.ActiveCabinetInfoEntities;
import org.ftclub.cabinet.lent.domain.LentHistory;
import org.ftclub.cabinet.user.domain.User;
import org.ftclub.cabinet.utils.annotations.ComplexRepository;

@ComplexRepository(entityClass = {Cabinet.class, CabinetPlace.class, LentHistory.class, User.class})
public interface CabinetComplexRepository {
List<ActiveCabinetInfoEntities> findCabinetsActiveLentHistoriesByBuildingAndFloor(String building, Integer floor);
List<Cabinet> findCabinetsByBuildingAndFloor(String building, Integer floor);
List <Cabinet> findAllCabinetsByBuildingAndFloor(String building, Integer floor);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.ftclub.cabinet.cabinet.repository;


import static org.ftclub.cabinet.cabinet.domain.QCabinet.cabinet;
import static org.ftclub.cabinet.cabinet.domain.QCabinetPlace.cabinetPlace;
import static org.ftclub.cabinet.lent.domain.QLentHistory.lentHistory;
import static org.ftclub.cabinet.user.domain.QUser.user;

import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import javax.persistence.EntityManager;
import org.ftclub.cabinet.cabinet.domain.Cabinet;
import org.ftclub.cabinet.dto.ActiveCabinetInfoEntities;
import org.ftclub.cabinet.dto.QActiveCabinetInfoEntities;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;

@CacheConfig(cacheNames = "cabinets")
public class CabinetComplexRepositoryImpl implements CabinetComplexRepository {

private final JPAQueryFactory queryFactory;

public CabinetComplexRepositoryImpl(EntityManager em) {
this.queryFactory = new JPAQueryFactory(em);
}

// CACHE
@Cacheable(key = "#building + #floor")
public List<Cabinet> findAllCabinetsByBuildingAndFloor(String building, Integer floor) {
JPAQuery<Cabinet> query = queryFactory.selectFrom(cabinet)
.join(cabinetPlace)
.on(cabinet.cabinetPlace.cabinetPlaceId.eq(cabinetPlace.cabinetPlaceId))
.where(cabinetPlace.location.building
.eq(building)
.and(cabinetPlace.location.floor.eq(floor))
);
return query.fetch();
}

@Override
public List<ActiveCabinetInfoEntities> findCabinetsActiveLentHistoriesByBuildingAndFloor(
String building, Integer floor) {
return queryFactory.selectDistinct(
new QActiveCabinetInfoEntities(cabinet, lentHistory, user))
.from(cabinet)
.join(cabinetPlace)
.on(cabinet.cabinetPlace.cabinetPlaceId.eq(cabinetPlace.cabinetPlaceId))
.join(lentHistory)
.on(cabinet.cabinetId.eq(lentHistory.cabinet.cabinetId))
.join(user)
.on(lentHistory.user.userId.eq(user.userId))
.where(cabinetPlace.location.building
.eq(building)
.and(cabinetPlace.location.floor.eq(floor))
.and(lentHistory.endedAt.isNull())
)
.fetch();
}

//WIP
@Override
public List<Cabinet> findCabinetsByBuildingAndFloor(String building, Integer floor) {
JPAQuery<Cabinet> where = queryFactory.select(cabinet)
.from(cabinet)
.join(cabinetPlace)
.on(cabinet.cabinetId.eq(cabinetPlace.cabinetPlaceId))
.where(cabinetPlace.location.building
.eq(building)
.and(cabinetPlace.location.floor.eq(floor)));
return where.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,20 @@ public List<ActiveCabinetInfoEntities> findCabinetsActiveLentHistoriesByBuilding
return cabinetMapper.toActiveCabinetInfoEntitiesDto(cabinet, lentHistory, user);
}).collect(Collectors.toList());
}
public List<ActiveCabinetInfoEntities> findCabinetsActiveLentHistoriesByBuildingAndFloor2(String building, Integer floor) {
return cabinetRepository.findCabinetsActiveLentHistoriesByBuildingAndFloor(building, floor);
}
public List<Cabinet> findCabinetsByBuildingAndFloor2(String building, Integer floor) {
return cabinetRepository.findAllCabinetsByBuildingAndFloor(building, floor);
}

/**
* 유저 ID로 사물함을 찾습니다.
*
* @param userId 유저ID
* @return 사물함 엔티티
* @throws ServiceException 사물함을 찾을 수 없는 경우
*/
/**
* 유저 ID로 사물함을 찾습니다.
*
* @param userId 유저ID
* @return 사물함 엔티티
* @throws ServiceException 사물함을 찾을 수 없는 경우
*/
public Cabinet findLentCabinetByUserId(Long userId) {
log.debug("Called findLentCabinetByUserId: {}", userId);
return cabinetRepository.findLentCabinetByUserId(userId).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.ftclub.cabinet.cabinet.repository;

import java.util.List;
import java.util.Optional;
import javax.persistence.LockModeType;
import org.ftclub.cabinet.cabinet.domain.Cabinet;
import org.ftclub.cabinet.cabinet.domain.CabinetStatus;
import org.ftclub.cabinet.cabinet.domain.LentType;
Expand All @@ -16,8 +13,12 @@
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.persistence.LockModeType;
import java.util.List;
import java.util.Optional;

@Repository
public interface CabinetRepository extends JpaRepository<Cabinet, Long> {
public interface CabinetRepository extends JpaRepository<Cabinet, Long>, CabinetComplexRepository {

@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("SELECT c "
Expand Down Expand Up @@ -97,7 +98,6 @@ Page<Cabinet> findPaginationByVisibleNum(@Param("visibleNum") Integer visibleNum
List<Object[]> findCabinetActiveLentHistoryUserListByBuildingAndFloor(
@Param("building") String building, @Param("floor") Integer floor);


@EntityGraph(attributePaths = {"cabinetPlace"})
@Query("SELECT c " +
"FROM Cabinet c " +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.ftclub.cabinet.cabinet.repository;

import org.ftclub.cabinet.cabinet.domain.Cabinet;
import org.ftclub.cabinet.utils.annotations.ComplexRepository;
import org.ftclub.cabinet.lent.domain.LentHistory;
import org.ftclub.cabinet.user.domain.User;

@ComplexRepository(entityClass = {Cabinet.class, LentHistory.class, User.class})
public interface FloorLoadRepository {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.ftclub.cabinet.cabinet.repository;

import com.querydsl.jpa.impl.JPAQueryFactory;
import javax.persistence.EntityManager;
import org.ftclub.cabinet.cabinet.domain.Cabinet;
import org.ftclub.cabinet.cabinet.domain.CabinetPlace;
import org.ftclub.cabinet.utils.annotations.ComplexRepository;
import org.ftclub.cabinet.lent.domain.LentHistory;
import org.ftclub.cabinet.user.domain.User;

@ComplexRepository(entityClass = {Cabinet.class, CabinetPlace.class, LentHistory.class, User.class})
public class FloorLoadRepositoryImpl implements FloorLoadRepository {

private final JPAQueryFactory queryFactory;

public FloorLoadRepositoryImpl(EntityManager em) {
this.queryFactory = new JPAQueryFactory(em);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ public interface CabinetFacadeService {
*/
List<CabinetsPerSectionResponseDto> getCabinetsPerSection(String building, Integer floor);

List<CabinetsPerSectionResponseDto> getCabinetsPerSectionRefactor(String building, Integer floor);
List<CabinetsPerSectionResponseDto> getCabinetsPerSectionRefactor(String building,
Integer floor);

List<CabinetsPerSectionResponseDto> getCabinetsPerSectionDSL(String building, Integer floor);


/**
* 사물함의 상태 메모를 업데이트합니다.
Expand Down Expand Up @@ -104,8 +108,8 @@ public interface CabinetFacadeService {
* 대여 타입에 따른 사물함 페이지네이션을 가져옵니다.
*
* @param lentType 대여 타입
* @param page 페이지 번호
* @param size 페이지 당 보여줄 개수
* @param page 페이지 번호
* @param size 페이지 당 보여줄 개수
* @return 사물함 페이지네이션
*/
CabinetPaginationDto getCabinetPaginationByLentType(LentType lentType, Integer page,
Expand All @@ -114,9 +118,9 @@ CabinetPaginationDto getCabinetPaginationByLentType(LentType lentType, Integer p
/**
* 사물함 상태에 따른 사물함 페이지네이션을 가져옵니다.
*
* @param status 사물함 상태
* @param page 페이지 번호
* @param size 페이지 당 보여줄 개수
* @param status 사물함 상태
* @param page 페이지 번호
* @param size 페이지 당 보여줄 개수
* @return 사물함 페이지네이션
*/
CabinetPaginationDto getCabinetPaginationByStatus(CabinetStatus status, Integer page,
Expand All @@ -126,8 +130,8 @@ CabinetPaginationDto getCabinetPaginationByStatus(CabinetStatus status, Integer
* 사물함 표시 번호에 따른 사물함 페이지네이션을 가져옵니다.
*
* @param visibleNum 사물함 표시 번호
* @param page 페이지 번호
* @param size 페이지 당 보여줄 개수
* @param page 페이지 번호
* @param size 페이지 당 보여줄 개수
* @return 사물함 페이지네이션
*/
CabinetPaginationDto getCabinetPaginationByVisibleNum(Integer visibleNum, Integer page,
Expand All @@ -137,7 +141,7 @@ CabinetPaginationDto getCabinetPaginationByVisibleNum(Integer visibleNum, Intege
* 사물함 Id에 따른 대여 기록 페이지네이션을 가져옵니다.
*
* @param cabinetId 사물함 Id
* @param page 페이지네이션(page, size)
* @param page 페이지네이션(page, size)
* @return 대여 기록 페이지네이션
*/
LentHistoryPaginationDto getCabinetLentHistoriesPagination(Long cabinetId,
Expand Down
Loading

0 comments on commit 24e76eb

Please sign in to comment.