Skip to content

Commit

Permalink
[BE] REFACTOR: merge with dev
Browse files Browse the repository at this point in the history
  • Loading branch information
enaenen committed Aug 24, 2023
1 parent 430dd39 commit 9dbe540
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 34 deletions.
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,5 +1,6 @@
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;
Expand All @@ -23,6 +24,7 @@ public class CabinetComplexRepositoryImpl implements CabinetComplexRepository {
public CabinetComplexRepositoryImpl(EntityManager em) {
this.queryFactory = new JPAQueryFactory(em);
}

// CACHE
@Cacheable(key = "#building + #floor")
public List<Cabinet> findAllCabinetsByBuildingAndFloor(String building, Integer floor) {
Expand Down
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
package org.ftclub.cabinet.cabinet.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.ftclub.cabinet.cabinet.domain.Cabinet;
import org.ftclub.cabinet.cabinet.domain.CabinetStatus;
import org.ftclub.cabinet.cabinet.domain.Grid;
import org.ftclub.cabinet.cabinet.domain.LentType;
import org.ftclub.cabinet.cabinet.repository.CabinetOptionalFetcher;
import org.ftclub.cabinet.dto.*;
import org.ftclub.cabinet.dto.ActiveCabinetInfoEntities;
import org.ftclub.cabinet.dto.BuildingFloorsDto;
import org.ftclub.cabinet.dto.CabinetClubStatusRequestDto;
import org.ftclub.cabinet.dto.CabinetDto;
import org.ftclub.cabinet.dto.CabinetInfoPaginationDto;
import org.ftclub.cabinet.dto.CabinetInfoResponseDto;
import org.ftclub.cabinet.dto.CabinetPaginationDto;
import org.ftclub.cabinet.dto.CabinetPreviewDto;
import org.ftclub.cabinet.dto.CabinetSimpleDto;
import org.ftclub.cabinet.dto.CabinetSimplePaginationDto;
import org.ftclub.cabinet.dto.CabinetStatusRequestDto;
import org.ftclub.cabinet.dto.CabinetsPerSectionResponseDto;
import org.ftclub.cabinet.dto.LentDto;
import org.ftclub.cabinet.dto.LentHistoryDto;
import org.ftclub.cabinet.dto.LentHistoryPaginationDto;
import org.ftclub.cabinet.lent.domain.LentHistory;
import org.ftclub.cabinet.lent.repository.LentOptionalFetcher;
import org.ftclub.cabinet.mapper.CabinetMapper;
Expand All @@ -19,9 +40,6 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Log4j2
Expand Down Expand Up @@ -91,15 +109,18 @@ public CabinetSimplePaginationDto getCabinetsSimpleInfoByVisibleNum(Integer visi
*/
@Override
@Transactional(readOnly = true)
public List<CabinetsPerSectionResponseDto> getCabinetsPerSection(String building, Integer floor) {
public List<CabinetsPerSectionResponseDto> getCabinetsPerSection(String building,
Integer floor) {
log.debug("getCabinetsPerSection");
// List<ActiveCabinetInfoEntities> currentLentCabinets = cabinetOptionalFetcher.findCabinetsActiveLentHistoriesByBuildingAndFloor2(building, floor);
List<ActiveCabinetInfoEntities> currentLentCabinets = cabinetOptionalFetcher.findCabinetsActiveLentHistoriesByBuildingAndFloor(building, floor);
List<Cabinet> allCabinetsByBuildingAndFloor = cabinetOptionalFetcher.findAllCabinetsByBuildingAndFloor(building, floor);
List<ActiveCabinetInfoEntities> currentLentCabinets = cabinetOptionalFetcher.findCabinetsActiveLentHistoriesByBuildingAndFloor(
building, floor);
List<Cabinet> allCabinetsByBuildingAndFloor = cabinetOptionalFetcher.findAllCabinetsByBuildingAndFloor(
building, floor);

Map<Cabinet, List<LentHistory>> cabinetLentHistories = currentLentCabinets.stream().
collect(Collectors.groupingBy(ActiveCabinetInfoEntities::getCabinet,
Collectors.mapping(ActiveCabinetInfoEntities::getLentHistory, Collectors.toList())));
Collectors.mapping(ActiveCabinetInfoEntities::getLentHistory,
Collectors.toList())));

Map<String, List<CabinetPreviewDto>> cabinetPreviewsBySection = new HashMap<>();
cabinetLentHistories.forEach((cabinet, lentHistories) -> {
Expand All @@ -116,7 +137,8 @@ public List<CabinetsPerSectionResponseDto> getCabinetsPerSection(String building
allCabinetsByBuildingAndFloor.forEach(cabinet -> {
if (!cabinetLentHistories.containsKey(cabinet)) {
String section = cabinet.getCabinetPlace().getLocation().getSection();
CabinetPreviewDto preview = createCabinetPreviewDto(cabinet, Collections.emptyList());
CabinetPreviewDto preview = createCabinetPreviewDto(cabinet,
Collections.emptyList());
if (cabinetPreviewsBySection.containsKey(section)) {
cabinetPreviewsBySection.get(section).add(preview);
} else {
Expand All @@ -126,30 +148,117 @@ public List<CabinetsPerSectionResponseDto> getCabinetsPerSection(String building
}
}
});
cabinetPreviewsBySection.values().forEach(cabinetList -> cabinetList.sort(Comparator.comparing(CabinetPreviewDto::getVisibleNum)));
cabinetPreviewsBySection.values().forEach(cabinetList -> cabinetList.sort(
Comparator.comparing(CabinetPreviewDto::getVisibleNum)));
return cabinetPreviewsBySection.entrySet().stream()
.sorted(Comparator.comparing(entry -> entry.getValue().get(0).getVisibleNum()))
.map(entry -> cabinetMapper.toCabinetsPerSectionResponseDto(entry.getKey(), entry.getValue()))
.map(entry -> cabinetMapper.toCabinetsPerSectionResponseDto(entry.getKey(),
entry.getValue()))
.collect(Collectors.toList());
}

@Override
public List<CabinetsPerSectionResponseDto> getCabinetsPerSectionRefactor(String building,
Integer floor) {
List<Cabinet> cabinets = cabinetOptionalFetcher.findAllCabinetsByBuildingAndFloor(building,
floor);
Map<String, List<CabinetPreviewDto>> map = new HashMap<>();
cabinets.forEach(cabinet -> {
List<LentHistory> lentHistories = lentOptionalFetcher
.findActiveLentByCabinetIdWithUser(cabinet.getCabinetId());
String section = cabinet.getCabinetPlace().getLocation().getSection();
if (map.containsKey(section)) {
map.get(section)
.add(cabinetMapper.toCabinetPreviewDto(cabinet, lentHistories.size(),
lentHistories.isEmpty() ? null
: lentHistories.get(0).getUser().getName()));
} else {
List<CabinetPreviewDto> cabinetPreviewDtoList = new ArrayList<>();
cabinetPreviewDtoList.add(
cabinetMapper.toCabinetPreviewDto(cabinet, lentHistories.size(),
lentHistories.isEmpty() ? null
: lentHistories.get(0).getUser().getName()));
map.put(section, cabinetPreviewDtoList);
}
});
map.forEach(
(key, value) -> value.sort(Comparator.comparing(CabinetPreviewDto::getVisibleNum)));
return map.entrySet().stream()
.sorted(Comparator.comparing(entry -> entry.getValue().get(0).getVisibleNum()))
.map(entry -> cabinetMapper.toCabinetsPerSectionResponseDto(entry.getKey(),
entry.getValue()))
.collect(Collectors.toList());
}

@Override
public List<CabinetsPerSectionResponseDto> getCabinetsPerSectionDSL(String building,
Integer floor) {
log.debug("getCabinetsPerSection");
List<ActiveCabinetInfoEntities> currentLentCabinets = cabinetOptionalFetcher.findCabinetsActiveLentHistoriesByBuildingAndFloor2(
building, floor);
List<Cabinet> allCabinetsByBuildingAndFloor = cabinetOptionalFetcher.findAllCabinetsByBuildingAndFloor(
building, floor);

Map<Cabinet, List<LentHistory>> cabinetLentHistories = currentLentCabinets.stream().
collect(Collectors.groupingBy(ActiveCabinetInfoEntities::getCabinet,
Collectors.mapping(ActiveCabinetInfoEntities::getLentHistory,
Collectors.toList())));

Map<String, List<CabinetPreviewDto>> cabinetPreviewsBySection = new HashMap<>();
cabinetLentHistories.forEach((cabinet, lentHistories) -> {
String section = cabinet.getCabinetPlace().getLocation().getSection();
CabinetPreviewDto preview = createCabinetPreviewDto(cabinet, lentHistories);
if (cabinetPreviewsBySection.containsKey(section)) {
cabinetPreviewsBySection.get(section).add(preview);
} else {
List<CabinetPreviewDto> previews = new ArrayList<>();
previews.add(preview);
cabinetPreviewsBySection.put(section, previews);
}
});
allCabinetsByBuildingAndFloor.forEach(cabinet -> {
if (!cabinetLentHistories.containsKey(cabinet)) {
String section = cabinet.getCabinetPlace().getLocation().getSection();
CabinetPreviewDto preview = createCabinetPreviewDto(cabinet,
Collections.emptyList());
if (cabinetPreviewsBySection.containsKey(section)) {
cabinetPreviewsBySection.get(section).add(preview);
} else {
List<CabinetPreviewDto> previews = new ArrayList<>();
previews.add(preview);
cabinetPreviewsBySection.put(section, previews);
}
}
});
cabinetPreviewsBySection.values().forEach(cabinetList -> cabinetList.sort(
Comparator.comparing(CabinetPreviewDto::getVisibleNum)));
return cabinetPreviewsBySection.entrySet().stream()
.sorted(Comparator.comparing(entry -> entry.getValue().get(0).getVisibleNum()))
.map(entry -> cabinetMapper.toCabinetsPerSectionResponseDto(entry.getKey(),
entry.getValue()))
.collect(Collectors.toList());
}

@Transactional(readOnly = true)
public List<CabinetsPerSectionResponseDto> getCabinetsPerSection2(String building, Integer floor) {
public List<CabinetsPerSectionResponseDto> getCabinetsPerSection2(String building,
Integer floor) {
log.debug("getCabinetsPerSection2");
List<Cabinet> cabinetsByBuildingAndFloor2 = cabinetOptionalFetcher.findCabinetsByBuildingAndFloor2(
building, floor);
List<ActiveCabinetInfoEntities> currentLentCabinets = cabinetOptionalFetcher.findCabinetsActiveLentHistoriesByBuildingAndFloor2(building, floor);
List<ActiveCabinetInfoEntities> currentLentCabinets = cabinetOptionalFetcher.findCabinetsActiveLentHistoriesByBuildingAndFloor2(
building, floor);
// List<ActiveCabinetInfoEntities> currentLentCabinets = cabinetOptionalFetcher.findCabinetsActiveLentHistoriesByBuildingAndFloor(building, floor);
List<Cabinet> allCabinetsByBuildingAndFloor = cabinetOptionalFetcher.findAllCabinetsByBuildingAndFloor(building, floor);
List<Cabinet> allCabinetsByBuildingAndFloor = cabinetOptionalFetcher.findAllCabinetsByBuildingAndFloor(
building, floor);

// 층별 / 건물로 가져온 Cabinet 은 cache
// Cabinet 기준으로 lentHistory 를 조회
// LentHistory와 연결된 User 조회

Map<Cabinet, List<LentHistory>> cabinetLentHistories = currentLentCabinets.stream().
collect(Collectors.groupingBy(ActiveCabinetInfoEntities::getCabinet,
Collectors.mapping(ActiveCabinetInfoEntities::getLentHistory, Collectors.toList())));
Collectors.mapping(ActiveCabinetInfoEntities::getLentHistory,
Collectors.toList())));

Map<String, List<CabinetPreviewDto>> cabinetPreviewsBySection = new HashMap<>();
cabinetLentHistories.forEach((cabinet, lentHistories) -> {
Expand All @@ -166,7 +275,8 @@ public List<CabinetsPerSectionResponseDto> getCabinetsPerSection2(String buildin
allCabinetsByBuildingAndFloor.forEach(cabinet -> {
if (!cabinetLentHistories.containsKey(cabinet)) {
String section = cabinet.getCabinetPlace().getLocation().getSection();
CabinetPreviewDto preview = createCabinetPreviewDto(cabinet, Collections.emptyList());
CabinetPreviewDto preview = createCabinetPreviewDto(cabinet,
Collections.emptyList());
if (cabinetPreviewsBySection.containsKey(section)) {
cabinetPreviewsBySection.get(section).add(preview);
} else {
Expand All @@ -176,15 +286,18 @@ public List<CabinetsPerSectionResponseDto> getCabinetsPerSection2(String buildin
}
}
});
cabinetPreviewsBySection.values().forEach(cabinetList -> cabinetList.sort(Comparator.comparing(CabinetPreviewDto::getVisibleNum)));
cabinetPreviewsBySection.values().forEach(cabinetList -> cabinetList.sort(
Comparator.comparing(CabinetPreviewDto::getVisibleNum)));
return cabinetPreviewsBySection.entrySet().stream()
.sorted(Comparator.comparing(entry -> entry.getValue().get(0).getVisibleNum()))
.map(entry -> cabinetMapper.toCabinetsPerSectionResponseDto(entry.getKey(), entry.getValue()))
.map(entry -> cabinetMapper.toCabinetsPerSectionResponseDto(entry.getKey(),
entry.getValue()))
.collect(Collectors.toList());
}


private CabinetPreviewDto createCabinetPreviewDto(Cabinet cabinet, List<LentHistory> lentHistories) {
private CabinetPreviewDto createCabinetPreviewDto(Cabinet cabinet,
List<LentHistory> lentHistories) {
String lentUserName = null;
if (!lentHistories.isEmpty() && lentHistories.get(0).getUser() != null) {
lentUserName = lentHistories.get(0).getUser().getName();
Expand All @@ -199,7 +312,7 @@ private CabinetPreviewDto createCabinetPreviewDto(Cabinet cabinet, List<LentHist
@Override
@Transactional(readOnly = true)
public CabinetPaginationDto getCabinetPaginationByLentType(LentType lentType, Integer page,
Integer size) {
Integer size) {
log.debug("getCabinetPaginationByLentType");
if (size <= 0) {
size = Integer.MAX_VALUE;
Expand All @@ -220,7 +333,7 @@ public CabinetPaginationDto getCabinetPaginationByLentType(LentType lentType, In
@Override
@Transactional(readOnly = true)
public CabinetPaginationDto getCabinetPaginationByStatus(CabinetStatus status, Integer page,
Integer size) {
Integer size) {
log.debug("getCabinetPaginationByStatus");
if (size <= 0) {
size = Integer.MAX_VALUE;
Expand All @@ -240,7 +353,7 @@ public CabinetPaginationDto getCabinetPaginationByStatus(CabinetStatus status, I
@Override
@Transactional(readOnly = true)
public CabinetPaginationDto getCabinetPaginationByVisibleNum(Integer visibleNum, Integer page,
Integer size) {
Integer size) {
log.debug("getCabinetPaginationByVisibleNum");
if (size <= 0) {
size = Integer.MAX_VALUE;
Expand All @@ -261,7 +374,7 @@ public CabinetPaginationDto getCabinetPaginationByVisibleNum(Integer visibleNum,
@Override
@Transactional(readOnly = true)
public LentHistoryPaginationDto getCabinetLentHistoriesPagination(Long cabinetId, Integer page,
Integer size) {
Integer size) {
log.debug("getCabinetLentHistoriesPagination");
if (size <= 0) {
size = Integer.MAX_VALUE;
Expand Down

0 comments on commit 9dbe540

Please sign in to comment.