더북(TheBook)

6. getBookListByFilter() 메서드도 다음과 같이 수정합니다.

코드 17-14 BookRepositoryImpl.java

package com.springmvc.repository;
...
@Repository
public class BookRepositoryImpl implements BookRepository {
    ...

    public Set<Book> getBookListByFilter(Map<String, List<String>> filter) {
        Set<Book> booksByPublisher = new HashSet<Book>();
        Set<Book> booksByCategory = new HashSet<Book>();
        Set<String> criterias = filter.keySet();
        if (criterias.contains("publisher")) {
            for (int j = 0; j < filter.get("publisher").size(); j++) { 
                 String publisherName = filter.get("publisher").get(j); 
                 String SQL = "SELECT * FROM book where b_publisher LIKE '%" + publisherName + "%'"; 
                 booksByPublisher.addAll(template.query(SQL, new BookRowMapper())); 
            }
        }

        if (criterias.contains("category")) {
            for (int i = 0; i < filter.get("category").size(); i++) {
                String category = filter.get("category").get(i);
                String SQL = "SELECT * FROM book where b_category LIKE '%" + category + "%'"; 
                booksByCategory.addAll(template.query(SQL, new BookRowMapper())); 
            }
        }
        booksByCategory.retainAll(booksByPublisher);
        return booksByCategory;
    }
    ...
}
신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.