Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] 공지사항 카테고리 조회 및 검색 기능 구현 #71

Merged
merged 7 commits into from
Sep 15, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.teamA.hicardi.common.dto.PageResponseDto;
import com.teamA.hicardi.domain.notice.dto.response.NoticeGetResponseDto;
import com.teamA.hicardi.domain.notice.service.NoticeService;
import com.teamA.hicardi.error.ErrorCode;
import com.teamA.hicardi.error.exception.custom.BusinessException;

import lombok.RequiredArgsConstructor;

Expand All @@ -26,4 +30,22 @@ public ResponseEntity<PageResponseDto> getAllNotices(Pageable pageable){
Page<NoticeGetResponseDto> response = noticeService.getAllNotices(pageable);
return PageResponseDto.of(response);
}

@GetMapping("/category")
public ResponseEntity<PageResponseDto> getCategoryNotices(@RequestParam String search, Pageable pageable) {
if (StringUtils.isEmpty(search)) {
throw new BusinessException(ErrorCode.WRONG_CATEGORY);
}
Page<NoticeGetResponseDto> response = noticeService.getCategoryNotices(search, pageable);
return PageResponseDto.of(response);
}

@GetMapping("/keyword")
public ResponseEntity<PageResponseDto> searchFaq(@RequestParam String search, Pageable pageable) {
if (StringUtils.isEmpty(search)) {
throw new BusinessException(ErrorCode.WRONG_SEARCH);
}
Page<NoticeGetResponseDto> response = noticeService.searchNotice(search, pageable);
return PageResponseDto.of(response);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.teamA.hicardi.domain.notice.entity;

import java.util.stream.Stream;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.teamA.hicardi.domain.faq.entity.Category;
import com.teamA.hicardi.error.ErrorCode;
import com.teamA.hicardi.error.exception.custom.BusinessException;

import lombok.AllArgsConstructor;
import lombok.Getter;

Expand All @@ -8,5 +15,13 @@
public enum NoticeCategory {
NECESSARY("필독"), UPDATE("업데이트"), DATA("자료"), NEWS("언론 보도");

@JsonCreator
public static NoticeCategory create(String requestValue) {
return Stream.of(values())
.filter(v -> v.desc.equalsIgnoreCase(requestValue))
.findFirst()
.orElseThrow(() -> new BusinessException(ErrorCode.INVALID_CATEGORY));
}

private final String desc;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,20 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import com.teamA.hicardi.domain.notice.entity.Notice;
import com.teamA.hicardi.domain.notice.entity.NoticeCategory;
import io.lettuce.core.dynamic.annotation.Param;

public interface NoticeRepository extends JpaRepository<Notice, Long> {

@Query("SELECT n FROM Notice n " +
"WHERE (n.isTop = true OR n.category = :category) " +
"ORDER BY n.isTop DESC, n.createdDate DESC")
Page<Notice> findAllOrderedByIsTopAndCategory(@Param("category") NoticeCategory category, Pageable pageable);

@Query("SELECT n FROM Notice n ORDER BY n.isTop DESC, n.createdDate DESC")
Page<Notice> findAllOrderedByIsTopAndCreateDate(Pageable pageable);

@Query("SELECT n FROM Notice n " +
" WHERE n.title LIKE %:search% OR n.content LIKE %:search% ")
Page<Notice> findAllBySearch(@Param("search") String search, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.teamA.hicardi.domain.faq.dto.response.FaqGetResponseDto;
import com.teamA.hicardi.domain.faq.entity.Faq;
import com.teamA.hicardi.domain.notice.dto.response.NoticeGetResponseDto;
import com.teamA.hicardi.domain.notice.entity.Notice;
import com.teamA.hicardi.domain.notice.entity.NoticeCategory;
import com.teamA.hicardi.domain.notice.repository.NoticeRepository;

import lombok.RequiredArgsConstructor;
Expand All @@ -24,4 +23,17 @@ public Page<NoticeGetResponseDto> getAllNotices(Pageable pageable) {
Page<NoticeGetResponseDto> response = notices.map(f -> NoticeGetResponseDto.from(f));
return response;
}

public Page<NoticeGetResponseDto> getCategoryNotices(String search, Pageable pageable) {
NoticeCategory category = NoticeCategory.create(search);
Page<Notice> notices = noticeRepository.findAllOrderedByIsTopAndCategory(category, pageable);
Page<NoticeGetResponseDto> response = notices.map(f -> NoticeGetResponseDto.from(f));
return response;
}

public Page<NoticeGetResponseDto> searchNotice(String search, Pageable pageable) {
Page<Notice> notices = noticeRepository.findAllBySearch(search, pageable);
Page<NoticeGetResponseDto> response = notices.map(f -> NoticeGetResponseDto.from(f));
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,40 @@ void setUp() {
result.andExpect(status().isOk());
verify(noticeService, times(1)).getAllNotices(any());
}

@Test
void 공지사항_카테고리별_조회() throws Exception {
PageRequest pageable = PageRequest.of(0, 5);
List<NoticeGetResponseDto> dtos = new ArrayList<>();
dtos.add(new NoticeGetResponseDto(1L, "NEWS", "뉴스", "내용1", "첨부파일1", false, LocalDate.now()));
dtos.add(new NoticeGetResponseDto(2L, "DATA", "자료", "내용2", "첨부파일2", true, LocalDate.now()));
Page<NoticeGetResponseDto> response = new PageImpl<>(dtos, pageable, 2);

given(noticeService.getCategoryNotices(anyString(), any())).willReturn(response);
ResultActions result = mockMvc.perform(
get("/notices/category")
.param("search", "자료")
);

result.andExpect(status().isOk());
verify(noticeService, times(1)).getCategoryNotices(anyString(), any());
}

@Test
void 공지사항_키워드_검색() throws Exception {
PageRequest pageable = PageRequest.of(0, 5);
List<NoticeGetResponseDto> dtos = new ArrayList<>();
dtos.add(new NoticeGetResponseDto(1L, "NEWS", "뉴스", "내용1", "첨부파일1", false, LocalDate.now()));
dtos.add(new NoticeGetResponseDto(2L, "DATA", "자료", "내용2", "첨부파일2", true, LocalDate.now()));
Page<NoticeGetResponseDto> response = new PageImpl<>(dtos, pageable, 2);

given(noticeService.searchNotice(anyString(), any())).willReturn(response);
ResultActions result = mockMvc.perform(
get("/notices/keyword")
.param("search", "뉴스")
);

result.andExpect(status().isOk());
verify(noticeService, times(1)).searchNotice(anyString(), any());
}
}
Loading