Skip to content

Commit

Permalink
[feature/Inhabas#234] 서비스 테스트 코드 구현 및 리펙토링
Browse files Browse the repository at this point in the history
  • Loading branch information
skytin1004 committed Feb 22, 2024
1 parent 1426dd5 commit 9747ad1
Show file tree
Hide file tree
Showing 16 changed files with 566 additions and 394 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.inhabas.api.domain.contest.domain.valueObject.ContestType;
import com.inhabas.api.domain.contest.domain.valueObject.Topic;
import com.inhabas.api.domain.file.domain.BoardFile;
import com.inhabas.api.domain.menu.domain.Menu;

@Entity
@Table(name = "CONTEST_BOARD")
Expand Down Expand Up @@ -71,7 +72,7 @@ public String getContent() {

@Builder
public ContestBoard(
ContestType contestType,
Menu menu,
Long contestFieldId,
String title,
String content,
Expand All @@ -80,7 +81,7 @@ public ContestBoard(
LocalDate dateContestStart,
LocalDate dateContestEnd) {

this.contestType = contestType;
super(title, menu);
this.contestField = new ContestField(contestFieldId);
this.title = new Title(title);
this.content = new Content(content);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
package com.inhabas.api.domain.contest.domain.valueObject;

public enum ContestType {
CONTEST, // 공모전
ACTIVITY // 대외활동
CONTEST("contest", 18),
ACTIVITY("activity", 19);

private final String boardType;
private final int menuId;

ContestType(String boardType, int menuId) {
this.boardType = boardType;
this.menuId = menuId;
}

public String getBoardType() {
return boardType;
}

public int getMenuId() {
return menuId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class ContestBoardDetailDto {

@NotNull @Positive private Long id;

@NotNull private Long contestField;
@NotNull private Long contestFieldId;

@NotBlank private String title;

Expand Down Expand Up @@ -60,7 +60,7 @@ public class ContestBoardDetailDto {
@Builder
public ContestBoardDetailDto(
Long id,
Long contestField,
Long contestFieldId,
String title,
String content,
String writerName,
Expand All @@ -74,7 +74,7 @@ public ContestBoardDetailDto(
List<FileDownloadDto> images,
List<FileDownloadDto> otherFiles) {
this.id = id;
this.contestField = contestField;
this.contestFieldId = contestFieldId;
this.title = title;
this.content = content;
this.writerName = writerName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,19 @@
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import org.springframework.web.multipart.MultipartFile;

import com.inhabas.api.domain.contest.domain.valueObject.ContestType;
import org.hibernate.validator.constraints.Length;

// 공모전 게시판 글 업데이트 및 저장
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@NoArgsConstructor()
public class SaveContestBoardDto {

@NotNull(message = "공모전 게시판 타입을 선택해주세요.")
private ContestType contestType;

@NotNull(message = "공모전 분야를 선택해주세요.")
private Long contestFieldId;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.inhabas.api.domain.contest.repository;

import java.util.List;
import java.util.Optional;

import com.inhabas.api.domain.contest.domain.ContestBoard;
import com.inhabas.api.domain.contest.domain.valueObject.ContestType;
import com.inhabas.api.domain.contest.dto.ContestBoardDto;

public interface ContestBoardRepositoryCustom {

List<ContestBoard> findAllByContestTypeAndFieldLike(
List<ContestBoardDto> findAllByTypeAndFieldAndSearch(
ContestType contestType, Long contestFieldId, String search, String sortBy);

public Optional<ContestBoard> findByTypeAndId(ContestType contestType, Long boardId);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package com.inhabas.api.domain.contest.repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;

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.inhabas.api.domain.contest.dto.ContestBoardDto;
import com.inhabas.api.global.util.ClassifiedFiles;
import com.inhabas.api.global.util.ClassifyFiles;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
Expand Down Expand Up @@ -37,11 +43,11 @@ private OrderSpecifier<?> getSortedColumn(
}

// 공모전 검색 및 필터링 기능
public List<ContestBoard> findAllByContestTypeAndFieldLike(
public List<ContestBoardDto> findAllByTypeAndFieldAndSearch(
ContestType contestType, Long contestFieldId, String search, String sortBy) {
BooleanExpression target =
contestTypeEq(contestType)
.and(contestFieldEq(contestFieldId))
eqContestType(contestType)
.and(eqContestField(contestFieldId))
.and(
titleLike(search)
.or(contentLike(search))
Expand All @@ -56,14 +62,46 @@ public List<ContestBoard> findAllByContestTypeAndFieldLike(
query.orderBy(contestBoard.dateContestEnd.desc()); // 마감순: dateContestEnd 순으로 내림차순 정렬
}

return query.fetch();
List<ContestBoard> boards = query.fetch();

return boards.stream()
.map(
board -> {
ClassifiedFiles classifiedFiles =
ClassifyFiles.classifyFiles(new ArrayList<>(board.getFiles()));
return ContestBoardDto.builder()
.id(board.getId())
.contestFieldId(board.getContestField().getId())
.title(board.getTitle())
.topic(board.getTopic())
.association(board.getAssociation())
.dateContestStart(board.getDateContestStart())
.dateContestEnd(board.getDateContestEnd())
.thumbnail(classifiedFiles.getThumbnail())
.build();
})
.collect(Collectors.toList());
}

private BooleanExpression contestTypeEq(ContestType contestType) {
@Override
public Optional<ContestBoard> findByTypeAndId(ContestType contestType, Long boardId) {
return Optional.ofNullable(
queryFactory
.selectFrom(contestBoard)
.where((eqContestType(contestType)).and(contestBoard.id.eq(boardId)))
.orderBy(contestBoard.dateCreated.desc())
.fetchOne());
}

private BooleanExpression eqMemberId(Long memberId) {
return contestBoard.writer.id.eq(memberId);
}

private BooleanExpression eqContestType(ContestType contestType) {
return contestBoard.contestType.eq(contestType);
}

private BooleanExpression contestFieldEq(Long contestFieldId) {
private BooleanExpression eqContestField(Long contestFieldId) {
if (contestFieldId == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@

public interface ContestBoardService {

List<ContestBoardDto> getContestBoardsByType(
List<ContestBoardDto> getContestBoards(
ContestType contestType, Long contestFieldId, String search, String sortBy);

Long writeContestBoard(
Long memberId, SaveContestBoardDto saveContestBoardDto, ContestType contestType);
Long memberId, ContestType contestType, SaveContestBoardDto saveContestBoardDto);

ContestBoardDetailDto getContestBoard(Long boardId);
// 공모전 게시판 단일조회
ContestBoardDetailDto getContestBoard(ContestType contestType, Long boardId);

void updateContestBoard(Long boardId, SaveContestBoardDto saveContestBoardDto);
void updateContestBoard(
Long boardId, ContestType contestType, SaveContestBoardDto saveContestBoardDto);

void deleteContestBoard(Long boardId);
}
Loading

0 comments on commit 9747ad1

Please sign in to comment.