Skip to content

Commit

Permalink
[feature/Inhabas#228] 공모전 게시판 구현 완료 - 리펙토링
Browse files Browse the repository at this point in the history
  • Loading branch information
skytin1004 committed Feb 16, 2024
1 parent c6115ca commit 5e822d3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.inhabas.api.domain.contest.repository;

import com.inhabas.api.domain.contest.domain.QContestBoard;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import java.util.Map;
import java.util.function.BiFunction;

import lombok.RequiredArgsConstructor;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import com.inhabas.api.domain.contest.domain.ContestBoard;
import com.inhabas.api.domain.contest.domain.QContestBoard;
import com.inhabas.api.domain.contest.domain.valueObject.ContestType;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
Expand All @@ -27,12 +27,16 @@ public class ContestBoardRepositoryImpl implements ContestBoardRepositoryCustom
private QContestBoard contestBoard = QContestBoard.contestBoard;

// 필드 이름과 정렬 기준을 매핑
private OrderSpecifier<?> getSortedColumn(Order order, QContestBoard contestBoard, String fieldName) {
Map<String, BiFunction<Order, QContestBoard, OrderSpecifier<?>>> orderSpecifierMap = new HashMap<>();
private OrderSpecifier<?> getSortedColumn(
Order order, QContestBoard contestBoard, String fieldName) {
Map<String, BiFunction<Order, QContestBoard, OrderSpecifier<?>>> orderSpecifierMap =
new HashMap<>();
orderSpecifierMap.put("id", (o, cb) -> new OrderSpecifier<>(o, cb.id));
orderSpecifierMap.put("dateContestEnd", (o, cb) -> new OrderSpecifier<>(o, cb.dateContestEnd));

return orderSpecifierMap.getOrDefault(fieldName, (o, cb) -> new OrderSpecifier<>(Order.DESC, cb.dateContestEnd)).apply(order, contestBoard);
return orderSpecifierMap
.getOrDefault(fieldName, (o, cb) -> new OrderSpecifier<>(Order.DESC, cb.dateContestEnd))
.apply(order, contestBoard);
}

// 페이지 요청에 따른 정렬
Expand All @@ -41,7 +45,8 @@ public List<OrderSpecifier<?>> getAllOrderSpecifiers(Pageable pageable) {
if (pageable.getSort() != null) {
for (Sort.Order order : pageable.getSort()) {
Order direction = order.getDirection().isAscending() ? Order.ASC : Order.DESC;
OrderSpecifier<?> orderSpecifier = getSortedColumn(direction, contestBoard, order.getProperty());
OrderSpecifier<?> orderSpecifier =
getSortedColumn(direction, contestBoard, order.getProperty());
orders.add(orderSpecifier);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.inhabas.api.domain.contest.dto.ContestBoardDto;
import com.inhabas.api.domain.contest.dto.SaveContestBoardDto;
import com.inhabas.api.domain.contest.repository.ContestBoardRepository;
import com.inhabas.api.domain.contest.repository.ContestFieldRepository;
import com.inhabas.api.domain.file.domain.BoardFile;
import com.inhabas.api.domain.file.dto.FileDownloadDto;
import com.inhabas.api.domain.file.usecase.S3Service;
Expand Down Expand Up @@ -65,13 +64,8 @@ public List<ContestBoardDto> getContestBoardsByType(
return contestBoardList.stream()
.map(
contestBoard -> {
//첨부파일들 중 첫번째 이미지 = 썸네일
FileDownloadDto thumbnail =
contestBoard.getFiles().stream()
.filter(file -> isImageFile(file.getName()))
.findFirst()
.map(file -> new FileDownloadDto(file.getName(), file.getUrl()))
.orElse(null);
getFirstImageAsThumbnail(new ArrayList<>(contestBoard.getFiles()));

return ContestBoardDto.builder()
.id(contestBoard.getId())
Expand All @@ -98,6 +92,15 @@ private boolean isImageFile(String fileName) {
|| lowerCaseFileName.endsWith(".webp");
}

// 첨부파일들 중 첫 번째 이미지 파일을 썸네일로 반환
private FileDownloadDto getFirstImageAsThumbnail(List<BoardFile> files) {
return files.stream()
.filter(file -> isImageFile(file.getName()))
.findFirst()
.map(file -> new FileDownloadDto(file.getName(), file.getUrl()))
.orElse(null);
}

// contestType 별로 게시글 작성
@Override
@Transactional
Expand Down

0 comments on commit 5e822d3

Please sign in to comment.