Skip to content

Commit

Permalink
[#52] feat: Search List API
Browse files Browse the repository at this point in the history
  • Loading branch information
hellouz818 committed Mar 24, 2022
1 parent 0ce5053 commit 1b278c5
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<Notice, Integer> {
public interface NoticeRepository extends JpaRepository<Notice, Long> {
Notice findByNid(Long nid);

@Query("SELECT distinct n.ndate as ndate FROM Notice n, User u WHERE n.user.uid = u.uid")
List<SearchDateInf> findUniqueNdate(User user);

List<Notice> findByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -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<SearchListDto> searchList(HttpServletRequest request){
return searchService.searchList(request);
}

@RequestMapping(value="/search/{nid}", method = RequestMethod.GET)
public String searchDetail(){
return "SearchDetail";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.answer.notinote.Search.domain.repository;

import java.time.LocalDate;

public interface SearchDateInf {
LocalDate getNdate();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.answer.notinote.Search.dto;

import lombok.Getter;

@Getter
public class SearchDetailDto {
}
Original file line number Diff line number Diff line change
@@ -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<String> save_titles;

@Builder
public SearchListDto(LocalDate date, List<String> save_titles){
this.date = date;
this.save_titles = save_titles;
}




}
Original file line number Diff line number Diff line change
@@ -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<SearchListDto> searchList(HttpServletRequest request){

String token = jwtTokenProvider.resolveToken(request);
String useremail = jwtTokenProvider.getUserEmail(token);
User user = userRepository.findByUemail(useremail).orElseThrow(IllegalArgumentException::new);

List<Notice> notices = noticeRepository.findByUser(user);

List<LocalDate> dateList = new ArrayList<>();
List<List<String>> titleLists = new ArrayList<>();
List<SearchListDto> 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<String> 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;
}

}

0 comments on commit 1b278c5

Please sign in to comment.