task(module-web,module-ejb): add search by boardgame name

This commit is contained in:
Damian Pierzchalski 2018-12-17 22:18:04 +01:00
parent 44e6ff895b
commit 0f4a954a94
2 changed files with 29 additions and 5 deletions

View File

@ -25,11 +25,25 @@ public class BoardGameDao {
return boardGames;
}
public List<BoardGameEntity>getByName(int page, int size, String namePhrase) {
Query query = em.createQuery("SELECT b FROM BoardGameEntity b WHERE b.name LIKE CONCAT('%',:namePhrase,'%') order by b.id", BoardGameEntity.class);
query.setParameter("namePhrase", namePhrase);
List<BoardGameEntity> boardGames = new ArrayList<BoardGameEntity>(query.setFirstResult(page*size).setMaxResults(size).getResultList());
return boardGames;
}
public Long getCount() {
Query query = em.createQuery("SELECT COUNT(b) FROM BoardGameEntity b");
Long count = (long)query.getSingleResult();
return count;
}
public Long getCountByName(String namePhrase) {
Query query = em.createQuery("SELECT COUNT(b) FROM BoardGameEntity b WHERE b.name LIKE CONCAT('%',:namePhrase,'%')");
query.setParameter("namePhrase", namePhrase);
Long count = (long)query.getSingleResult();
return count;
}
public BoardGameEntity getById(Long aId) {
BoardGameEntity boardGame = em.find(BoardGameEntity.class, aId);
@ -39,7 +53,7 @@ public class BoardGameDao {
}
return boardGame;
}
public BoardGameEntity add(BoardGameEntity aBoardGame) {
em.persist(aBoardGame);
return aBoardGame;

View File

@ -31,13 +31,23 @@ public class BoardGameResource {
@GET
@Produces("application/json; charset=UTF-8")
public Response getAllBoardGames(@QueryParam("page") int page, @QueryParam("size") int size) {
if (page < 0 || size < 1 ) {
public Response getAllBoardGames(@QueryParam("page") int page, @QueryParam("size") int size, @QueryParam("name") String namePhrase) {
List<BoardGameShortDto> boardgames;
Long count = new Long(0);
if (page < 0 || size < 1) {
page = 0;
size = 5;
}
List<BoardGameShortDto> boardgames = boardGameDatabase.getAll(page, size).stream().map(ent -> new BoardGameShortDto(ent)).collect(Collectors.toList());
Long count = boardGameDatabase.getCount();
if (namePhrase != null) {
boardgames = boardGameDatabase.getByName(page, size, namePhrase).stream().map(ent -> new BoardGameShortDto(ent)).collect(Collectors.toList());
count = boardGameDatabase.getCountByName(namePhrase);
} else {
boardgames = boardGameDatabase.getAll(page, size).stream().map(ent -> new BoardGameShortDto(ent)).collect(Collectors.toList());
count = boardGameDatabase.getCount();
}
BoardGameListDto ret = new BoardGameListDto(boardgames, page, size, count);
return Response.status(200).entity(ret).build();