-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathBoardService.java
More file actions
61 lines (50 loc) · 2.29 KB
/
BoardService.java
File metadata and controls
61 lines (50 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.example.solidconnection.board.service;
import com.example.solidconnection.board.domain.Board;
import com.example.solidconnection.board.repository.BoardRepository;
import com.example.solidconnection.custom.exception.CustomException;
import com.example.solidconnection.custom.exception.ErrorCode;
import com.example.solidconnection.post.domain.Post;
import com.example.solidconnection.post.dto.BoardFindPostResponse;
import com.example.solidconnection.type.BoardCode;
import com.example.solidconnection.type.PostCategory;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.EnumUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.stream.Collectors;
import static com.example.solidconnection.custom.exception.ErrorCode.INVALID_POST_CATEGORY;
@Service
@RequiredArgsConstructor
public class BoardService {
private final BoardRepository boardRepository;
@Transactional(readOnly = true)
public List<BoardFindPostResponse> findPostsByCodeAndPostCategory(String code, String category) {
String boardCode = validateCode(code);
PostCategory postCategory = validatePostCategory(category);
Board board = boardRepository.getByCodeUsingEntityGraph(boardCode);
List<Post> postList = getPostListByPostCategory(board.getPostList(), postCategory);
return BoardFindPostResponse.from(postList);
}
private String validateCode(String code) {
try {
return String.valueOf(BoardCode.valueOf(code));
} catch (IllegalArgumentException ex) {
throw new CustomException(ErrorCode.INVALID_BOARD_CODE);
}
}
private PostCategory validatePostCategory(String category) {
if (!EnumUtils.isValidEnum(PostCategory.class, category)) {
throw new CustomException(INVALID_POST_CATEGORY);
}
return PostCategory.valueOf(category);
}
private List<Post> getPostListByPostCategory(List<Post> postList, PostCategory postCategory) {
if (postCategory.equals(PostCategory.전체)) {
return postList;
}
return postList.stream()
.filter(post -> post.getCategory().equals(postCategory))
.collect(Collectors.toList());
}
}