@ -0,0 +1,56 @@ | |||
package com.jesuspinar.booksearch.service; | |||
import com.jesuspinar.booksearch.dao.BookDao; | |||
import com.jesuspinar.booksearch.model.Book; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.stereotype.Service; | |||
import javax.annotation.PostConstruct; | |||
import java.util.Arrays; | |||
import java.util.Collection; | |||
import java.util.function.Predicate; | |||
import java.util.stream.Collectors; | |||
@Service | |||
public class BookQueryService implements IBookQueryService{ | |||
@Autowired | |||
BookDao dao; | |||
private Predicate<Book> predicate; | |||
@PostConstruct | |||
public void init() { | |||
predicate = null; | |||
} | |||
@Override | |||
public Collection<Book> exec() { | |||
return dao.findAll() | |||
.stream() | |||
.filter(predicate) | |||
.collect(Collectors.toList()); | |||
} | |||
@Override | |||
public IBookQueryService anyGenre(String... genre) { | |||
Predicate<Book> pAnyGenre = (book -> Arrays.stream(genre).anyMatch(book.getGenre()::contains)); | |||
predicate = (predicate == null) ? pAnyGenre : predicate.and(pAnyGenre); | |||
return this; | |||
} | |||
@Override | |||
public IBookQueryService allGenres(String... genres) { | |||
Predicate<Book> pAnyGenre = (book -> Arrays.stream(genres).allMatch(book.getGenre()::contains)); | |||
predicate = (predicate == null) ? pAnyGenre : predicate.and(pAnyGenre); | |||
return this; | |||
} | |||
@Override | |||
public IBookQueryService anyTitle(String title) { | |||
Predicate<Book> pTitleContains = (book -> book.getTitle().toLowerCase().contains(title.toLowerCase())); | |||
predicate = (predicate == null) ? pTitleContains : predicate.and(pTitleContains); | |||
return this; | |||
} | |||
} |
@ -0,0 +1,57 @@ | |||
package com.jesuspinar.booksearch.service; | |||
import com.jesuspinar.booksearch.dao.BookDao; | |||
import com.jesuspinar.booksearch.model.Book; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.stereotype.Service; | |||
import java.util.Collection; | |||
import java.util.List; | |||
import java.util.stream.Collectors; | |||
@Service | |||
public class BookService implements IBookService{ | |||
@Autowired | |||
BookDao bookDao; | |||
@Autowired | |||
IBookQueryService queryService; | |||
@Override | |||
public Collection<String> findAllGenres() { | |||
List<String> result = null; | |||
result = bookDao.findAll() | |||
.stream() | |||
.map(book -> book.getGenre()) | |||
//.flatMap(l -> Stream.of(l))//TODO: Check this if other java version! | |||
.distinct() | |||
.sorted() | |||
.collect(Collectors.toList()); | |||
return result; | |||
} | |||
@Override | |||
public Collection<Book> findByAnyGenre(String... genres) { | |||
return queryService.anyGenre(genres).exec(); | |||
} | |||
@Override | |||
public Collection<Book> findByAllGenres(String... genres) { | |||
return queryService.allGenres(genres).exec(); | |||
} | |||
@Override | |||
public Collection<Book> findByTitleContains(String title) { | |||
return queryService.anyTitle(title).exec(); | |||
} | |||
@Override | |||
public Collection<Book> findAll() { | |||
return bookDao.findAll(); | |||
} | |||
} |
@ -0,0 +1,15 @@ | |||
package com.jesuspinar.booksearch.service; | |||
import com.jesuspinar.booksearch.model.Book; | |||
import java.util.Collection; | |||
public interface IBookQueryService { | |||
public Collection<Book> exec(); | |||
public IBookQueryService anyGenre(String... genre); | |||
public IBookQueryService allGenres(String... genres); | |||
//public IBookQueryService anyAuthor(String... author); | |||
//public IBookQueryService allAuthors(String... genres); | |||
public IBookQueryService anyTitle(String title); | |||
} |
@ -0,0 +1,13 @@ | |||
package com.jesuspinar.booksearch.service; | |||
import com.jesuspinar.booksearch.model.Book; | |||
import java.util.Collection; | |||
public interface IBookService { | |||
public Collection<String> findAllGenres(); | |||
public Collection<Book> findByAnyGenre(String... genres); | |||
public Collection<Book> findByAllGenres(String... genres); | |||
public Collection<Book> findByTitleContains(String title); | |||
public Collection<Book> findAll(); | |||
} |