task(module-web,module-ejb): add search by boardgame name
This commit is contained in:
parent
44e6ff895b
commit
0f4a954a94
@ -25,12 +25,26 @@ public class BoardGameDao {
|
|||||||
return boardGames;
|
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() {
|
public Long getCount() {
|
||||||
Query query = em.createQuery("SELECT COUNT(b) FROM BoardGameEntity b");
|
Query query = em.createQuery("SELECT COUNT(b) FROM BoardGameEntity b");
|
||||||
Long count = (long)query.getSingleResult();
|
Long count = (long)query.getSingleResult();
|
||||||
return count;
|
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) {
|
public BoardGameEntity getById(Long aId) {
|
||||||
BoardGameEntity boardGame = em.find(BoardGameEntity.class, aId);
|
BoardGameEntity boardGame = em.find(BoardGameEntity.class, aId);
|
||||||
if (boardGame == null) {
|
if (boardGame == null) {
|
||||||
|
@ -31,13 +31,23 @@ public class BoardGameResource {
|
|||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Produces("application/json; charset=UTF-8")
|
@Produces("application/json; charset=UTF-8")
|
||||||
public Response getAllBoardGames(@QueryParam("page") int page, @QueryParam("size") int size) {
|
public Response getAllBoardGames(@QueryParam("page") int page, @QueryParam("size") int size, @QueryParam("name") String namePhrase) {
|
||||||
if (page < 0 || size < 1 ) {
|
List<BoardGameShortDto> boardgames;
|
||||||
|
Long count = new Long(0);
|
||||||
|
if (page < 0 || size < 1) {
|
||||||
page = 0;
|
page = 0;
|
||||||
size = 5;
|
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);
|
BoardGameListDto ret = new BoardGameListDto(boardgames, page, size, count);
|
||||||
return Response.status(200).entity(ret).build();
|
return Response.status(200).entity(ret).build();
|
||||||
|
Loading…
Reference in New Issue
Block a user