From 1b278c52829aa9963cbec6588599d79a7416ae35 Mon Sep 17 00:00:00 2001 From: hellouz818 Date: Thu, 24 Mar 2022 22:50:22 +0900 Subject: [PATCH] [#52] feat: Search List API --- .../domain/repository/NoticeRepository.java | 12 +++- .../Search/controller/SearchController.java | 33 ++++++++++ .../domain/repository/SearchDateInf.java | 7 ++ .../notinote/Search/dto/SearchDetailDto.java | 7 ++ .../notinote/Search/dto/SearchListDto.java | 26 ++++++++ .../Search/service/SearchService.java | 65 +++++++++++++++++++ 6 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 spring/notinote/src/main/java/com/answer/notinote/Search/controller/SearchController.java create mode 100644 spring/notinote/src/main/java/com/answer/notinote/Search/domain/repository/SearchDateInf.java create mode 100644 spring/notinote/src/main/java/com/answer/notinote/Search/dto/SearchDetailDto.java create mode 100644 spring/notinote/src/main/java/com/answer/notinote/Search/dto/SearchListDto.java create mode 100644 spring/notinote/src/main/java/com/answer/notinote/Search/service/SearchService.java 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 91f6bf7..f32d95e 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,11 +1,21 @@ package com.answer.notinote.Notice.domain.repository; import com.answer.notinote.Notice.domain.entity.Notice; +import com.answer.notinote.Search.domain.repository.SearchDateInf; +import com.answer.notinote.User.domain.entity.User; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository -public interface NoticeRepository extends JpaRepository { +public interface NoticeRepository extends JpaRepository { Notice findByNid(Long nid); + + @Query("SELECT distinct n.ndate as ndate FROM Notice n, User u WHERE n.user.uid = u.uid") + List findUniqueNdate(User user); + + List findByUser(User user); } 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 new file mode 100644 index 0000000..2a1d1d3 --- /dev/null +++ b/spring/notinote/src/main/java/com/answer/notinote/Search/controller/SearchController.java @@ -0,0 +1,33 @@ +package com.answer.notinote.Search.controller; + +import com.answer.notinote.Search.dto.SearchListDto; +import com.answer.notinote.Search.service.SearchService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; +import java.util.List; + +@RestController +public class SearchController { + + @Autowired + SearchService searchService; + + public SearchController(SearchService searchService){ + this.searchService = searchService; + } + + @RequestMapping(value="/search", method = RequestMethod.GET) + public List searchList(HttpServletRequest request){ + return searchService.searchList(request); + } + + @RequestMapping(value="/search/{nid}", method = RequestMethod.GET) + public String searchDetail(){ + return "SearchDetail"; + } + +} diff --git a/spring/notinote/src/main/java/com/answer/notinote/Search/domain/repository/SearchDateInf.java b/spring/notinote/src/main/java/com/answer/notinote/Search/domain/repository/SearchDateInf.java new file mode 100644 index 0000000..9c12b15 --- /dev/null +++ b/spring/notinote/src/main/java/com/answer/notinote/Search/domain/repository/SearchDateInf.java @@ -0,0 +1,7 @@ +package com.answer.notinote.Search.domain.repository; + +import java.time.LocalDate; + +public interface SearchDateInf { + LocalDate getNdate(); +} diff --git a/spring/notinote/src/main/java/com/answer/notinote/Search/dto/SearchDetailDto.java b/spring/notinote/src/main/java/com/answer/notinote/Search/dto/SearchDetailDto.java new file mode 100644 index 0000000..2f36163 --- /dev/null +++ b/spring/notinote/src/main/java/com/answer/notinote/Search/dto/SearchDetailDto.java @@ -0,0 +1,7 @@ +package com.answer.notinote.Search.dto; + +import lombok.Getter; + +@Getter +public class SearchDetailDto { +} diff --git a/spring/notinote/src/main/java/com/answer/notinote/Search/dto/SearchListDto.java b/spring/notinote/src/main/java/com/answer/notinote/Search/dto/SearchListDto.java new file mode 100644 index 0000000..b6587ca --- /dev/null +++ b/spring/notinote/src/main/java/com/answer/notinote/Search/dto/SearchListDto.java @@ -0,0 +1,26 @@ +package com.answer.notinote.Search.dto; + +import com.answer.notinote.Notice.domain.entity.Notice; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.time.LocalDate; +import java.util.List; + +@Getter +@NoArgsConstructor +public class SearchListDto { + private LocalDate date; + private List save_titles; + + @Builder + public SearchListDto(LocalDate date, List save_titles){ + this.date = date; + this.save_titles = save_titles; + } + + + + +} 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 new file mode 100644 index 0000000..ae07936 --- /dev/null +++ b/spring/notinote/src/main/java/com/answer/notinote/Search/service/SearchService.java @@ -0,0 +1,65 @@ +package com.answer.notinote.Search.service; + +import com.answer.notinote.Auth.token.provider.JwtTokenProvider; +import com.answer.notinote.Notice.domain.entity.Notice; +import com.answer.notinote.Notice.domain.repository.NoticeRepository; +import com.answer.notinote.Search.dto.SearchListDto; +import com.answer.notinote.User.domain.entity.User; +import com.answer.notinote.User.domain.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.servlet.http.HttpServletRequest; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + + +@Service +public class SearchService { + + @Autowired + NoticeRepository noticeRepository; + @Autowired + UserRepository userRepository; + @Autowired + JwtTokenProvider jwtTokenProvider; + + public List searchList(HttpServletRequest request){ + + String token = jwtTokenProvider.resolveToken(request); + String useremail = jwtTokenProvider.getUserEmail(token); + User user = userRepository.findByUemail(useremail).orElseThrow(IllegalArgumentException::new); + + List notices = noticeRepository.findByUser(user); + + 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))) { + titleList.add(notices.get(j).getTitle()); + } + } + titleLists.add(titleList); + SearchListDto searchListDto = SearchListDto.builder() + .date(dateList.get(i)) + .save_titles(titleLists.get(i)) + .build(); + searchListDtos.add(searchListDto); + } + return searchListDtos; + } + +}