diff --git a/resource-server/src/main/java/com/inhabas/api/domain/club/repository/ClubActivityRepository.java b/resource-server/src/main/java/com/inhabas/api/domain/club/repository/ClubActivityRepository.java index 47819252..d6f78101 100644 --- a/resource-server/src/main/java/com/inhabas/api/domain/club/repository/ClubActivityRepository.java +++ b/resource-server/src/main/java/com/inhabas/api/domain/club/repository/ClubActivityRepository.java @@ -4,4 +4,5 @@ import com.inhabas.api.domain.board.domain.AlbumBoard; -public interface ClubActivityRepository extends JpaRepository {} +public interface ClubActivityRepository + extends JpaRepository, ClubActivityRepositoryCustom {} diff --git a/resource-server/src/main/java/com/inhabas/api/domain/club/repository/ClubActivityRepositoryCustom.java b/resource-server/src/main/java/com/inhabas/api/domain/club/repository/ClubActivityRepositoryCustom.java new file mode 100644 index 00000000..f30010aa --- /dev/null +++ b/resource-server/src/main/java/com/inhabas/api/domain/club/repository/ClubActivityRepositoryCustom.java @@ -0,0 +1,10 @@ +package com.inhabas.api.domain.club.repository; + +import java.util.List; + +import com.inhabas.api.domain.club.dto.ClubActivityDto; + +public interface ClubActivityRepositoryCustom { + + List findAllAndSearch(String search); +} diff --git a/resource-server/src/main/java/com/inhabas/api/domain/club/repository/ClubActivityRepositoryImpl.java b/resource-server/src/main/java/com/inhabas/api/domain/club/repository/ClubActivityRepositoryImpl.java new file mode 100644 index 00000000..f0c4bbbe --- /dev/null +++ b/resource-server/src/main/java/com/inhabas/api/domain/club/repository/ClubActivityRepositoryImpl.java @@ -0,0 +1,48 @@ +package com.inhabas.api.domain.club.repository; + +import static com.inhabas.api.domain.board.domain.QAlbumBoard.albumBoard; + +import java.util.List; + +import lombok.RequiredArgsConstructor; + +import com.inhabas.api.domain.club.dto.ClubActivityDto; +import com.querydsl.core.types.Projections; +import com.querydsl.core.types.dsl.BooleanExpression; +import com.querydsl.jpa.impl.JPAQueryFactory; + +@RequiredArgsConstructor +public class ClubActivityRepositoryImpl implements ClubActivityRepositoryCustom { + + private final JPAQueryFactory queryFactory; + + @Override + public List findAllAndSearch(String search) { + return queryFactory + .select( + Projections.constructor( + ClubActivityDto.class, + albumBoard.id, + albumBoard.title.value, + albumBoard.writer.id, + albumBoard.writer.name.value, + albumBoard.dateCreated, + albumBoard.dateUpdated)) + .from(albumBoard) + .where(likeTitle(search).or(likeContent(search)).or(likeWriterName(search))) + .orderBy(albumBoard.dateCreated.desc()) + .fetch(); + } + + private BooleanExpression likeTitle(String search) { + return albumBoard.title.value.like("%" + search + "%"); + } + + private BooleanExpression likeContent(String search) { + return albumBoard.content.value.like("%" + search + "%"); + } + + private BooleanExpression likeWriterName(String search) { + return albumBoard.writer.name.value.like("%" + search + "%"); + } +} diff --git a/resource-server/src/main/java/com/inhabas/api/domain/club/usecase/ClubActivityService.java b/resource-server/src/main/java/com/inhabas/api/domain/club/usecase/ClubActivityService.java index 38c804a6..3b34d516 100644 --- a/resource-server/src/main/java/com/inhabas/api/domain/club/usecase/ClubActivityService.java +++ b/resource-server/src/main/java/com/inhabas/api/domain/club/usecase/ClubActivityService.java @@ -8,7 +8,7 @@ public interface ClubActivityService { - List getClubActivities(); + List getClubActivities(String search); Long writeClubActivity(Long memberId, SaveClubActivityDto saveClubActivityDto); diff --git a/resource-server/src/main/java/com/inhabas/api/domain/club/usecase/ClubActivityServiceImpl.java b/resource-server/src/main/java/com/inhabas/api/domain/club/usecase/ClubActivityServiceImpl.java index ffd2cd9f..08193c7a 100644 --- a/resource-server/src/main/java/com/inhabas/api/domain/club/usecase/ClubActivityServiceImpl.java +++ b/resource-server/src/main/java/com/inhabas/api/domain/club/usecase/ClubActivityServiceImpl.java @@ -1,7 +1,7 @@ package com.inhabas.api.domain.club.usecase; +import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; @@ -40,24 +40,12 @@ public class ClubActivityServiceImpl implements ClubActivityService { @Override @Transactional(readOnly = true) - public List getClubActivities() { - - List clubActivityList = clubActivityRepository.findAll(); - - return clubActivityList.stream() - .map( - board -> { - ClassifiedFiles classifiedFiles = ClassifyFiles.classifyFiles(board.getFiles()); - return ClubActivityDto.builder() - .id(board.getId()) - .title(board.getTitle()) - .writerName(board.getWriter().getName()) - .dateCreated(board.getDateCreated()) - .dateUpdated(board.getDateUpdated()) - .thumbnail(classifiedFiles.getThumbnail()) - .build(); - }) - .collect(Collectors.toList()); + public List getClubActivities(String search) { + List clubActivityList = new ArrayList<>(); + + clubActivityList.addAll(clubActivityRepository.findAllAndSearch(search)); + + return clubActivityList; } @Override @@ -118,6 +106,7 @@ public void updateClubActivity( AlbumBoard clubActivity = clubActivityRepository.findById(boardId).orElseThrow(NotFoundException::new); + clubActivity.updateText(saveClubActivityDto.getTitle(), saveClubActivityDto.getContent()); List fileIdList = saveClubActivityDto.getFiles(); List boardFileList = boardFileRepository.getAllByIdInAndUploader(fileIdList, writer); clubActivity.updateFiles(boardFileList); diff --git a/resource-server/src/main/java/com/inhabas/api/web/ClubActivityController.java b/resource-server/src/main/java/com/inhabas/api/web/ClubActivityController.java index 8e88c03e..7bd5573c 100644 --- a/resource-server/src/main/java/com/inhabas/api/web/ClubActivityController.java +++ b/resource-server/src/main/java/com/inhabas/api/web/ClubActivityController.java @@ -56,10 +56,13 @@ public ResponseEntity> getClubActivities( int page, @Parameter(description = "페이지당 개수", example = "10") @RequestParam(name = "size", defaultValue = "10") - int size) { + int size, + @Parameter(description = "검색어 (작성자 이름 or 제목 or 내용)", example = "") + @RequestParam(name = "search", defaultValue = "") + String search) { Pageable pageable = PageRequest.of(page, size); - List allDtos = clubActivityService.getClubActivities(); + List allDtos = clubActivityService.getClubActivities(search); List pagedDtos = PageUtil.getPagedDtoList(pageable, allDtos); PageImpl ClubActivityDtoPage = diff --git a/resource-server/src/test/java/com/inhabas/api/domain/club/usecase/ClubActivityServiceImplTest.java b/resource-server/src/test/java/com/inhabas/api/domain/club/usecase/ClubActivityServiceImplTest.java index 880abd90..50547813 100644 --- a/resource-server/src/test/java/com/inhabas/api/domain/club/usecase/ClubActivityServiceImplTest.java +++ b/resource-server/src/test/java/com/inhabas/api/domain/club/usecase/ClubActivityServiceImplTest.java @@ -6,12 +6,13 @@ import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.*; +import java.time.LocalDateTime; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.Optional; import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.transaction.annotation.Transactional; import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member; import com.inhabas.api.auth.domain.oauth2.member.repository.MemberRepository; @@ -46,26 +47,27 @@ public class ClubActivityServiceImplTest { @Mock private MenuRepository menuRepository; @DisplayName("동아리 활동 조회 성공") + @Transactional(readOnly = true) @Test public void getClubActivitiesTest_Success() { // given Member member = MemberTest.chiefMember(); - AlbumBoard clubActivity = - AlbumBoard.builder() + ClubActivityDto dto = + ClubActivityDto.builder() .title("title") - .content("content") - .menu(mock(Menu.class)) - .build() - .writtenBy(member, AlbumBoard.class); + .writerName(member.getName()) + .dateCreated(LocalDateTime.now()) + .dateUpdated(LocalDateTime.now()) + .build(); - given(clubActivityRepository.findAll()).willReturn(Arrays.asList(clubActivity)); + given(clubActivityRepository.findAllAndSearch(any())).willReturn(List.of(dto)); // when - List clubActivityDtoList = clubActivityService.getClubActivities(); + List clubActivityDtoList = clubActivityService.getClubActivities(""); // then assertThat(clubActivityDtoList).hasSize(1); - assertThat(clubActivityDtoList.get(0).getTitle()).isEqualTo(clubActivity.getTitle()); + assertThat(clubActivityDtoList.get(0).getTitle()).isEqualTo(dto.getTitle()); } @DisplayName("동아리 활동 생성 성공") diff --git a/resource-server/src/test/java/com/inhabas/api/web/ClubActivityControllerTest.java b/resource-server/src/test/java/com/inhabas/api/web/ClubActivityControllerTest.java index 6f6004ad..af5a0923 100644 --- a/resource-server/src/test/java/com/inhabas/api/web/ClubActivityControllerTest.java +++ b/resource-server/src/test/java/com/inhabas/api/web/ClubActivityControllerTest.java @@ -61,7 +61,7 @@ void getClubActivities() throws Exception { .thumbnail(null) .build(); List clubActivityDtoList = List.of(clubActivityDto); - given(clubActivityService.getClubActivities()).willReturn(clubActivityDtoList); + given(clubActivityService.getClubActivities("")).willReturn(clubActivityDtoList); // when mvc.perform(get("/club/activities"))