Skip to content

Commit

Permalink
[feature/Inhabas#298] 동아리활동 게시글 수정시 text변경안되는 문제 해결 및 정렬 기 추가능
Browse files Browse the repository at this point in the history
  • Loading branch information
skytin1004 committed May 9, 2024
1 parent 440169c commit 289548d
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

import com.inhabas.api.domain.board.domain.AlbumBoard;

public interface ClubActivityRepository extends JpaRepository<AlbumBoard, Long> {}
public interface ClubActivityRepository
extends JpaRepository<AlbumBoard, Long>, ClubActivityRepositoryCustom {}
Original file line number Diff line number Diff line change
@@ -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<ClubActivityDto> findAllAndSearch(String search);
}
Original file line number Diff line number Diff line change
@@ -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<ClubActivityDto> 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 + "%");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public interface ClubActivityService {

List<ClubActivityDto> getClubActivities();
List<ClubActivityDto> getClubActivities(String search);

Long writeClubActivity(Long memberId, SaveClubActivityDto saveClubActivityDto);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -40,24 +40,12 @@ public class ClubActivityServiceImpl implements ClubActivityService {

@Override
@Transactional(readOnly = true)
public List<ClubActivityDto> getClubActivities() {

List<AlbumBoard> 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<ClubActivityDto> getClubActivities(String search) {
List<ClubActivityDto> clubActivityList = new ArrayList<>();

clubActivityList.addAll(clubActivityRepository.findAllAndSearch(search));

return clubActivityList;
}

@Override
Expand Down Expand Up @@ -118,6 +106,7 @@ public void updateClubActivity(
AlbumBoard clubActivity =
clubActivityRepository.findById(boardId).orElseThrow(NotFoundException::new);

clubActivity.updateText(saveClubActivityDto.getTitle(), saveClubActivityDto.getContent());
List<String> fileIdList = saveClubActivityDto.getFiles();
List<BoardFile> boardFileList = boardFileRepository.getAllByIdInAndUploader(fileIdList, writer);
clubActivity.updateFiles(boardFileList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ public ResponseEntity<PagedResponseDto<ClubActivityDto>> 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<ClubActivityDto> allDtos = clubActivityService.getClubActivities();
List<ClubActivityDto> allDtos = clubActivityService.getClubActivities(search);
List<ClubActivityDto> pagedDtos = PageUtil.getPagedDtoList(pageable, allDtos);

PageImpl<ClubActivityDto> ClubActivityDtoPage =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ClubActivityDto> clubActivityDtoList = clubActivityService.getClubActivities();
List<ClubActivityDto> 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("동아리 활동 생성 성공")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void getClubActivities() throws Exception {
.thumbnail(null)
.build();
List<ClubActivityDto> clubActivityDtoList = List.of(clubActivityDto);
given(clubActivityService.getClubActivities()).willReturn(clubActivityDtoList);
given(clubActivityService.getClubActivities("")).willReturn(clubActivityDtoList);

// when
mvc.perform(get("/club/activities"))
Expand Down

0 comments on commit 289548d

Please sign in to comment.