1+ package com .somemore .community .service .query ;
2+
3+ import com .somemore .center .usecase .query .CenterQueryUseCase ;
4+ import com .somemore .community .domain .CommunityBoard ;
5+ import com .somemore .community .dto .response .CommunityBoardGetDetailResponseDto ;
6+ import com .somemore .community .dto .response .CommunityBoardGetResponseDto ;
7+ import com .somemore .community .dto .response .WriterDetailDto ;
8+ import com .somemore .community .repository .CommunityBoardRepository ;
9+ import com .somemore .community .usecase .query .CommunityBoardQueryUseCase ;
10+ import com .somemore .global .exception .BadRequestException ;
11+ import com .somemore .volunteer .dto .response .VolunteerForCommunityResponseDto ;
12+ import com .somemore .volunteer .usecase .query .FindVolunteerIdUseCase ;
13+ import lombok .RequiredArgsConstructor ;
14+ import org .springframework .stereotype .Service ;
15+ import org .springframework .transaction .annotation .Transactional ;
16+
17+ import java .util .List ;
18+ import java .util .UUID ;
19+ import java .util .stream .Collectors ;
20+
21+ import static com .somemore .global .exception .ExceptionMessage .NOT_EXISTS_COMMUNITY_BOARD ;
22+
23+
24+ @ RequiredArgsConstructor
25+ @ Transactional (readOnly = true )
26+ @ Service
27+ public class CommunityBoardQueryService implements CommunityBoardQueryUseCase {
28+
29+ private final CommunityBoardRepository communityBoardRepository ;
30+ private final CenterQueryUseCase centerQueryUseCase ;
31+ private final FindVolunteerIdUseCase findVolunteerIdUseCase ;
32+
33+ @ Override
34+ public List <CommunityBoardGetResponseDto > getCommunityBoards () {
35+ List <CommunityBoard > boards = communityBoardRepository .getCommunityBoards ();
36+
37+ return boards .stream ()
38+ .map (board -> {
39+ String writerNickname = getWriterNickname (board .getWriterId ());
40+ return CommunityBoardGetResponseDto .fromEntity (board , writerNickname );
41+ })
42+ .collect (Collectors .toList ());
43+ }
44+
45+ @ Override
46+ public List <CommunityBoardGetResponseDto > getCommunityBoardsByWriterId (UUID writerId ) {
47+ List <CommunityBoard > boards = communityBoardRepository .getCommunityBoardsByWriterId (writerId );
48+ String writerNickname = getWriterNickname (writerId );
49+
50+ return boards .stream ()
51+ .map (board -> CommunityBoardGetResponseDto .fromEntity (board , writerNickname ))
52+ .collect (Collectors .toList ());
53+ }
54+
55+ @ Override
56+ public CommunityBoardGetDetailResponseDto getCommunityBoardDetail (Long id ) {
57+ CommunityBoard board = communityBoardRepository .findById (id )
58+ .orElseThrow (() -> new BadRequestException (NOT_EXISTS_COMMUNITY_BOARD .getMessage ()));
59+
60+ return CommunityBoardGetDetailResponseDto .fromEntity (board , getWriterDetail (board .getWriterId ()));
61+ }
62+
63+ private String getWriterNickname (UUID writerId ) {
64+ String nickname = findVolunteerIdUseCase .getNicknameById (writerId );
65+
66+ if (nickname == null ) {
67+ nickname = centerQueryUseCase .getNameById (writerId );
68+ }
69+
70+ return nickname ;
71+ }
72+
73+ private WriterDetailDto getWriterDetail (UUID writerId ) {
74+ VolunteerForCommunityResponseDto volunteer = findVolunteerIdUseCase .getVolunteerDetailForCommunity (writerId );
75+
76+ if (volunteer == null ) {
77+ return centerQueryUseCase .getCenterDetailForCommunity (writerId );
78+ } else {
79+ return volunteer ;
80+ }
81+ }
82+ }
0 commit comments