게시판 글 날짜 순 정렬

뚜우웅이·2023년 4월 20일
0

SpringBoot웹

목록 보기
15/23

Service와 Repository 분리

확장성과, 단일 책임 원칙을 준수하기 위해 Service와 Repository를 분리해줍니다.

BoardRepository

Page<Board> findByTitleContainingOrContentContaining(String title, String content, Pageable pageable);


List<Board> findByTitleOrContent(String title, String content);

BoardService

public Page<Board> searchBoards(String searchText, String text, Pageable pageable) {
        return boardRepository.findByTitleContainingOrContentContaining(searchText, searchText, pageable);
    }
    
    
public List<Board> findByTitleOrContent(String title, String content) {
        return boardRepository.findByTitleOrContent(title, content);
    }    

UserRepository

public interface UserRepository extends JpaRepository<User, Long>{
    @EntityGraph(attributePaths = {"boards"})
    List<User> findAll();
    User findByUsername(String username);

    @Query("select u from User u where u.username like %?1%")
    List<User> findByUsernameQuery(String username);

    @Query(value = "select * from User u where u.username like %?1%", nativeQuery = true)
    List<User> findByUsernameNativeQuery(String username);

    boolean existsByUsername(String username);
}

UserService

public boolean checkUserName(String username){
        return userRepository.existsByUsername(username);
    }

    public List<User> findByUsernameQuery(String username) {
        return userRepository.findByUsernameQuery(username);
    }

    public List<User> findByUsernameNativeQuery(String username) {
        return userRepository.findByUsernameNativeQuery(username);
    }

게시판 순서 날짜 내림차순

BoardRepository

// 생성일자를 기준으로 내림차순 정렬된 게시글 목록 조회(검색까지)
    Page<Board> findByTitleContainingOrContentContainingOrderByCreatedAtDesc(String title, String content, Pageable pageable);
    Page<Board> findByTitleContainingOrContentContainingOrderByCreatedAtAsc(String title, String content, Pageable pageable);
    

생성일자를 기준으로 내림차순, 오름차순으로 정렬된 게시글 목록을 조회하는 메소드입니다.

BoardService

    public Page<Board> searchBoardsOrderByCreatedAtAsc(String title, String content, Pageable pageable){
        return boardRepository.findByTitleContainingOrContentContainingOrderByCreatedAtAsc(title,content,pageable);
    }

    public Page<Board> searchBoardsOrderByCreatedAtDesc(String title, String content, Pageable pageable){
        return boardRepository.findByTitleContainingOrContentContainingOrderByCreatedAtDesc(title,content,pageable);
    }  

BoardController

@GetMapping("/list")
    public String list(Model model, @PageableDefault(size = 15) Pageable pageable,
                       @RequestParam(required = false, defaultValue = "") String searchText,
                       @RequestParam(required = false) String orderBy,
                       HttpSession session) {
        if (orderBy == null) {
            orderBy = (String) session.getAttribute("orderBy");
            if (orderBy == null) {
                orderBy = "desc";
            }
        }
        session.setAttribute("orderBy", orderBy);
        Page<Board> boards;
        if (orderBy.equals("asc")) {
            boards = boardService.searchBoardsOrderByCreatedAtAsc(searchText, searchText, pageable);
        } else {
            boards = boardService.searchBoardsOrderByCreatedAtDesc(searchText, searchText, pageable);
        }
        int block = 5;
        int currentBlock = (boards.getPageable().getPageNumber() / block) * block;
        int startPage = currentBlock + 1;
        int endPage = Math.min(boards.getTotalPages(), currentBlock + block);
        model.addAttribute("startPage", startPage);
        model.addAttribute("endPage", endPage);
        model.addAttribute("boards", boards);
        model.addAttribute("orderBy", orderBy);
        return "board/list";
    }

세션(Session)을 이용하여 사용자의 정렬 방식을 저장하고, 저장된 값이 없는 경우 디폴트 값으로 desc를 사용하는 코드입니다.

먼저, orderBy 변수가 null인지 확인합니다. 만약 null이라면, 사용자의 세션에서 "orderBy" 값을 찾아서 가져옵니다. 세션에서 "orderBy" 값을 가져왔는데도 null이라면, orderBy 변수에 "desc" 값을 할당합니다

그 다음, session.setAttribute("orderBy", orderBy) 코드를 통해 orderBy 변수에 할당된 값을 세션에 저장합니다. 이를 통해 사용자가 페이지를 나갔다 다시 들어왔을 때에도 이전에 선택한 정렬 방식을 유지할 수 있습니다.

메소드 내부에서는 orderBy 값에 따라 게시글 목록을 정렬하여 조회하고, 페이징 처리를 수행한 후, 뷰에 전달할 데이터를 Model 객체에 담아 반환합니다.

HttpSession
HttpSession은 클라이언트와 서버 간에 유지되는 상태 정보를 담는 객체입니다. 이 코드에서는 orderBy 값을 세션에 저장하고 있습니다.

list.html

게시글 정렬 순서를 최신순, 오래된순으로 정렬 가능한 버튼을 추가해줍니다.
이 코드는 BootStrap css style을 가져온 것입니다.

<div>
            <label for="orderBy" class="sr-only">정렬 방식</label>
            <select id="orderBy" class="custom-select" style="width: 120px"
                    th:onchange="|location.href='?searchText=' + document.querySelector('#searchText').value + '&amp;orderBy=' + this.value;|">
                <option value="desc" th:selected="${orderBy == 'desc'}">최신순</option>
                <option value="asc" th:selected="${orderBy == 'asc'}">오래된순</option>
            </select>
        </div>

document.querySelector('#searchText').value를 통해 현재 검색어를 가져옵니다.
orderBy 파라미터는 드롭다운 메뉴에서 선택한 정렬 방식을 나타내며, this.value를 통해 현재 선택한 값을 가져옵니다.
이렇게 가져온 두 개의 파라미터를 문자열로 조합하여 location.href에 할당하면, 해당 URL로 페이지가 다시 로드됩니다.

<script>
        document.querySelector('#orderBy').addEventListener('change', function() {
            var orderBy = document.querySelector('#orderBy').value;
            window.location.href = '/board/view?id=' + id + '&orderBy=' + orderBy;
        });
    </script>

querySelector 함수를 사용하여 HTML 요소 중에서 id 속성이 "orderBy"인 요소를 찾아 이벤트 리스너를 등록합니다. 즉, select 요소의 값이 변경되면 이벤트가 발생하여 등록된 함수가 실행됩니다.

함수 내부에서는 선택된 값을 변수 orderBy에 저장하고, 이를 새로운 URL로 설정하여 페이지를 다시 로드합니다. 이 때, id 변수와 함께 orderBy 값도 URL의 쿼리 문자열로 추가됩니다.

profile
공부하는 초보 개발자

0개의 댓글