From fbeddc7f9fed404374a5e7699a5f581841369b47 Mon Sep 17 00:00:00 2001 From: hellouz818 Date: Thu, 26 May 2022 21:02:33 +0900 Subject: [PATCH] [#52] feat: search filter with child --- .../answer/notinote/Config/SwaggerConfig.java | 2 + .../domain/repository/NoticeRepository.java | 7 ++- .../Search/controller/SearchController.java | 6 +++ .../Search/service/SearchService.java | 44 +++++++++++++++++++ 4 files changed, 57 insertions(+), 2 deletions(-) diff --git a/spring/notinote/src/main/java/com/answer/notinote/Config/SwaggerConfig.java b/spring/notinote/src/main/java/com/answer/notinote/Config/SwaggerConfig.java index 039c351..2bc5b0b 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Config/SwaggerConfig.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Config/SwaggerConfig.java @@ -2,6 +2,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; @@ -11,6 +12,7 @@ @Configuration @EnableSwagger2 +@EnableWebMvc public class SwaggerConfig { @Bean public Docket restAPI() { diff --git a/spring/notinote/src/main/java/com/answer/notinote/Notice/domain/repository/NoticeRepository.java b/spring/notinote/src/main/java/com/answer/notinote/Notice/domain/repository/NoticeRepository.java index d79ad04..529b2f4 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Notice/domain/repository/NoticeRepository.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Notice/domain/repository/NoticeRepository.java @@ -1,5 +1,6 @@ package com.answer.notinote.Notice.domain.repository; +import com.answer.notinote.Child.domain.Child; import com.answer.notinote.Notice.domain.entity.Notice; import com.answer.notinote.Search.domain.repository.SearchDateInf; import com.answer.notinote.User.domain.entity.User; @@ -10,7 +11,6 @@ import java.time.LocalDate; import java.util.List; -import java.util.Optional; @Repository @@ -24,4 +24,7 @@ public interface NoticeRepository extends JpaRepository { List findByUser(User user); List findByNdate(LocalDate date); -} + + List findByUserAndChild(User user, Child child); + +} \ No newline at end of file diff --git a/spring/notinote/src/main/java/com/answer/notinote/Search/controller/SearchController.java b/spring/notinote/src/main/java/com/answer/notinote/Search/controller/SearchController.java index 6c32677..c352de1 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Search/controller/SearchController.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Search/controller/SearchController.java @@ -33,4 +33,10 @@ public SearchDetailDto searchDetail(@RequestParam("date") String date, HttpServl return searchService.searchDetailList(date, request); } + @RequestMapping(value="/search/child", method = RequestMethod.GET) + public List searchChildList(@RequestParam("cid") Long cid, HttpServletRequest request){ + return searchService.searchChildList(cid, request); + } + + } diff --git a/spring/notinote/src/main/java/com/answer/notinote/Search/service/SearchService.java b/spring/notinote/src/main/java/com/answer/notinote/Search/service/SearchService.java index 4d640eb..8e89272 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Search/service/SearchService.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Search/service/SearchService.java @@ -1,6 +1,8 @@ package com.answer.notinote.Search.service; import com.answer.notinote.Auth.token.provider.JwtTokenProvider; +import com.answer.notinote.Child.domain.Child; +import com.answer.notinote.Child.domain.repository.ChildRepository; import com.answer.notinote.Event.domain.Event; import com.answer.notinote.Event.service.EventService; import com.answer.notinote.Notice.domain.entity.Notice; @@ -34,6 +36,8 @@ public class SearchService { @Autowired UserRepository userRepository; @Autowired + ChildRepository childRepository; + @Autowired JwtTokenProvider jwtTokenProvider; public List searchList(HttpServletRequest request){ @@ -107,4 +111,44 @@ public SearchDetailDto searchDetailList(String date, HttpServletRequest request) .build(); } + + public List searchChildList(Long cid, HttpServletRequest request){ + + String token = jwtTokenProvider.resolveToken(request); + String useremail = jwtTokenProvider.getUserEmail(token); + User user = userRepository.findByUemail(useremail).orElseThrow(IllegalArgumentException::new); + Child child = childRepository.findById(cid).orElseThrow(IllegalArgumentException::new); + List notices = noticeRepository.findByUserAndChild(user,child); + + List dateList = new ArrayList<>(); + List> titleLists = new ArrayList<>(); + List searchListDtos = new ArrayList<>(); + + //유니크한 날짜값만 리스트에 저장하는 jpa 쿼리 메소드 + for(int i = 0; i < noticeRepository.findUniqueNdate(user).size(); i++){ + dateList.add(noticeRepository.findUniqueNdate(user).get(i).getNdate()); + } + + dateList.sort(Comparator.reverseOrder()); //최신순 정렬 + + for(int i = 0; i < dateList.size(); i++){ + List titleList = new ArrayList<>(); + for(int j = 0; j < notices.size(); j++){ + if((notices.get(j).getNdate()).equals(dateList.get(i))) { + if (notices.get(j).getTitle() != null) { + titleList.add(notices.get(j).getTitle()); + } + } + } + + if (!titleLists.get(i).isEmpty()) { + SearchListDto searchListDto = SearchListDto.builder() + .date(dateList.get(i)) + .saved_titles(titleLists.get(i)) + .build(); + searchListDtos.add(searchListDto); + } + } + return searchListDtos; + } }