Browse Source

Added book research

main
Jesus 2 years ago
parent
commit
d416d10f03
5 changed files with 155 additions and 2 deletions
  1. +14
    -2
      pom.xml
  2. +56
    -0
      src/main/java/com/jesuspinar/booksearch/service/BookQueryService.java
  3. +57
    -0
      src/main/java/com/jesuspinar/booksearch/service/BookService.java
  4. +15
    -0
      src/main/java/com/jesuspinar/booksearch/service/IBookQueryService.java
  5. +13
    -0
      src/main/java/com/jesuspinar/booksearch/service/IBookService.java

+ 14
- 2
pom.xml View File

@ -7,6 +7,18 @@
<groupId>com.jesuspinar</groupId>
<artifactId>booksearch</artifactId>
<version>0.0.2</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
<name>booksearch</name>
<description>Search books from a global book database</description>
<dependencies>
@ -22,8 +34,8 @@
</dependency>
</dependencies>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
</project>

+ 56
- 0
src/main/java/com/jesuspinar/booksearch/service/BookQueryService.java View File

@ -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;
}
}

+ 57
- 0
src/main/java/com/jesuspinar/booksearch/service/BookService.java View File

@ -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();
}
}

+ 15
- 0
src/main/java/com/jesuspinar/booksearch/service/IBookQueryService.java View File

@ -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);
}

+ 13
- 0
src/main/java/com/jesuspinar/booksearch/service/IBookService.java View File

@ -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();
}

Loading…
Cancel
Save