task(module-web,module-ejb): add pagination

This commit is contained in:
Damian Pierzchalski 2018-12-17 21:15:40 +01:00
parent d594af2e64
commit 3b8d1a616b
9 changed files with 197 additions and 21 deletions

View File

@ -1,7 +1,7 @@
package pl.myboardgames.db;
import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@ -19,12 +19,18 @@ public class BoardGameDao {
public BoardGameDao() {
}
public Set<BoardGameEntity>getAll() {
Query query = em.createQuery("SELECT u FROM BoardGameEntity u", BoardGameEntity.class);
Set<BoardGameEntity> boardGames = new HashSet<BoardGameEntity>(query.getResultList());
public List<BoardGameEntity>getAll(int page, int size) {
Query query = em.createQuery("SELECT b FROM BoardGameEntity b order by b.id", BoardGameEntity.class);
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 BoardGameEntity getById(Long aId) {
BoardGameEntity boardGame = em.find(BoardGameEntity.class, aId);
if (boardGame == null) {

View File

@ -1,13 +1,13 @@
package pl.myboardgames.db;
import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import pl.myboardgames.exception.DataNotFoundException;
import pl.myboardgames.exception.DataNotFoundException;
import pl.myboardgames.model.BoardGameEntity;
import pl.myboardgames.model.UserEntity;
@ -20,16 +20,22 @@ public class UserDao {
public UserDao() {
}
public Set<UserEntity>getAll() {
Query query = em.createQuery("SELECT u FROM UserEntity u", UserEntity.class);
Set<UserEntity> users = new HashSet<UserEntity>(query.getResultList());
public List<UserEntity> getAll(int page, int size) {
Query query = em.createQuery("SELECT u FROM UserEntity u order by u.id", UserEntity.class);
List<UserEntity> users = new ArrayList<UserEntity>(query.setFirstResult(page * size).setMaxResults(size).getResultList());
return users;
}
public Long getCount() {
Query query = em.createQuery("SELECT COUNT(u) FROM UserEntity u");
Long count = (long) query.getSingleResult();
return count;
}
public UserEntity getById(Long aId) {
UserEntity user = em.find(UserEntity.class, aId);
if (user == null) {
throw new DataNotFoundException("Can't find User for ID "
throw new DataNotFoundException("Can't find User for ID "
+ aId);
}
return user;

View File

@ -27,7 +27,7 @@ public class BoardGameEntity {
private Integer maxPlayers;
private Integer playTime;
@ManyToMany(cascade = CascadeType.ALL)
@ManyToMany
@JoinTable(
name = "usersboardgames",
joinColumns = {

View File

@ -14,7 +14,7 @@
<property name="format_sql" value = "true"/>
<property name="use_sql_comments" value = "true"/>
<property name="hibernate.generate_statistics" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="hibernate.enable_lazy_load_no_trans"
value="true" />
</properties>

View File

@ -0,0 +1,70 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pl.myboardgames.dto;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Nao
*/
public class BoardGameListDto {
private int page;
private int size;
private Long total;
private List<BoardGameShortDto> content = new ArrayList<BoardGameShortDto>();
public BoardGameListDto() {
}
public BoardGameListDto(List<BoardGameShortDto> aBoardgames, int aPage, int aSize, Long aCount) {
content = aBoardgames;
page = aPage;
size = aSize;
total = aCount;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<BoardGameShortDto> getContent() {
return content;
}
public void setContent(List<BoardGameShortDto> content) {
this.content = content;
}
}

View File

@ -16,6 +16,7 @@ public class BoardGameShortDto {
private Long id;
private String name;
public BoardGameShortDto() {
}

View File

@ -0,0 +1,71 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pl.myboardgames.dto;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Nao
*/
public class UserListDto {
private int page;
private int size;
private Long total;
private List<UserShortDto> content = new ArrayList<UserShortDto>();
public UserListDto() {
}
public UserListDto(List<UserShortDto> aUsers, int aPage, int aSize, Long aCount) {
content = aUsers;
page = aPage;
size = aSize;
total = aCount;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<UserShortDto> getContent() {
return content;
}
public void setContent(List<UserShortDto> content) {
this.content = content;
}
}

View File

@ -1,5 +1,6 @@
package pl.myboardgames.resources;
import java.util.List;
import java.util.stream.Collectors;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
@ -11,10 +12,12 @@ import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import pl.myboardgames.db.BoardGameDao;
import pl.myboardgames.dto.BoardGameDto;
import pl.myboardgames.dto.BoardGameListDto;
import pl.myboardgames.dto.BoardGameMapper;
import pl.myboardgames.dto.BoardGameShortDto;
import pl.myboardgames.model.BoardGameEntity;
@ -28,16 +31,23 @@ public class BoardGameResource {
@GET
@Produces("application/json; charset=UTF-8")
public Response getAllBoardGames() {
boardGameDatabase.getAll().stream().map(ent -> new BoardGameShortDto(ent)).collect(Collectors.toList());
return Response.status(200).entity(boardGameDatabase.getAll()).build();
public Response getAllBoardGames(@QueryParam("page") int page, @QueryParam("size") int size) {
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();
BoardGameListDto ret = new BoardGameListDto(boardgames, page, size, count);
return Response.status(200).entity(ret).build();
}
@GET
@Path("{id}")
@Produces("application/json; charset=UTF-8")
public Response getBoardGame(@PathParam("id") Long aId) {
BoardGameEntity ret = boardGameDatabase.getById(aId);
BoardGameDto ret = new BoardGameDto(boardGameDatabase.getById(aId));
return Response.status(200).entity(ret).build();
}

View File

@ -1,5 +1,6 @@
package pl.myboardgames.resources;
import java.util.List;
import java.util.stream.Collectors;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
@ -11,11 +12,13 @@ import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import pl.myboardgames.dto.BoardGameShortDto;
import pl.myboardgames.db.UserDao;
import pl.myboardgames.dto.UserDto;
import pl.myboardgames.dto.UserListDto;
import pl.myboardgames.dto.UserMapper;
import pl.myboardgames.dto.UserShortDto;
import pl.myboardgames.model.UserEntity;
@ -30,15 +33,24 @@ public class UserResource {
@GET
@Produces("application/json; charset=UTF-8")
public Response getAllUsers() {
return Response.status(200).entity(userDatabase.getAll().stream().map(ent -> new UserShortDto(ent)).collect(Collectors.toList())).build();
public Response getAllUsers(@QueryParam("page") int page, @QueryParam("size") int size) {
if (page < 0 || size < 1 ) {
page = 0;
size = 5;
}
List<UserShortDto> users = userDatabase.getAll(page, size).stream().map(ent -> new UserShortDto(ent)).collect(Collectors.toList());
Long count = userDatabase.getCount();
UserListDto ret = new UserListDto(users, page, size, count);
return Response.status(200).entity(ret).build();
}
@GET
@Path("{id}")
@Produces("application/json; charset=UTF-8")
public Response getUser(@PathParam("id") Long aId) {
UserEntity ret = userDatabase.getById(aId);
UserDto ret = new UserDto(userDatabase.getById(aId));
return Response.status(200).entity(ret).build();
}