diff --git a/module-ejb/src/main/java/pl/myboardgames/db/BoardGameDao.java b/module-ejb/src/main/java/pl/myboardgames/db/BoardGameDao.java index db8f410..7680b3b 100644 --- a/module-ejb/src/main/java/pl/myboardgames/db/BoardGameDao.java +++ b/module-ejb/src/main/java/pl/myboardgames/db/BoardGameDao.java @@ -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,11 +19,17 @@ public class BoardGameDao { public BoardGameDao() { } - public SetgetAll() { - Query query = em.createQuery("SELECT u FROM BoardGameEntity u", BoardGameEntity.class); - Set boardGames = new HashSet(query.getResultList()); + public ListgetAll(int page, int size) { + Query query = em.createQuery("SELECT b FROM BoardGameEntity b order by b.id", BoardGameEntity.class); + List boardGames = new ArrayList(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); diff --git a/module-ejb/src/main/java/pl/myboardgames/db/UserDao.java b/module-ejb/src/main/java/pl/myboardgames/db/UserDao.java index d6d3d4c..53f132e 100644 --- a/module-ejb/src/main/java/pl/myboardgames/db/UserDao.java +++ b/module-ejb/src/main/java/pl/myboardgames/db/UserDao.java @@ -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 SetgetAll() { - Query query = em.createQuery("SELECT u FROM UserEntity u", UserEntity.class); - Set users = new HashSet(query.getResultList()); + public List getAll(int page, int size) { + Query query = em.createQuery("SELECT u FROM UserEntity u order by u.id", UserEntity.class); + List users = new ArrayList(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; diff --git a/module-ejb/src/main/java/pl/myboardgames/model/BoardGameEntity.java b/module-ejb/src/main/java/pl/myboardgames/model/BoardGameEntity.java index 2db9b71..80f899e 100644 --- a/module-ejb/src/main/java/pl/myboardgames/model/BoardGameEntity.java +++ b/module-ejb/src/main/java/pl/myboardgames/model/BoardGameEntity.java @@ -27,7 +27,7 @@ public class BoardGameEntity { private Integer maxPlayers; private Integer playTime; - @ManyToMany(cascade = CascadeType.ALL) + @ManyToMany @JoinTable( name = "usersboardgames", joinColumns = { diff --git a/module-ejb/src/main/resources/META-INF/persistence.xml b/module-ejb/src/main/resources/META-INF/persistence.xml index 73dbee7..b1d8e60 100644 --- a/module-ejb/src/main/resources/META-INF/persistence.xml +++ b/module-ejb/src/main/resources/META-INF/persistence.xml @@ -14,7 +14,7 @@ - + diff --git a/module-web/src/main/java/pl/myboardgames/dto/BoardGameListDto.java b/module-web/src/main/java/pl/myboardgames/dto/BoardGameListDto.java new file mode 100644 index 0000000..8e54a10 --- /dev/null +++ b/module-web/src/main/java/pl/myboardgames/dto/BoardGameListDto.java @@ -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 content = new ArrayList(); + + public BoardGameListDto() { + + } + + public BoardGameListDto(List 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 getContent() { + return content; + } + + public void setContent(List content) { + this.content = content; + } + + + + + +} diff --git a/module-web/src/main/java/pl/myboardgames/dto/BoardGameShortDto.java b/module-web/src/main/java/pl/myboardgames/dto/BoardGameShortDto.java index 180567c..cba088f 100644 --- a/module-web/src/main/java/pl/myboardgames/dto/BoardGameShortDto.java +++ b/module-web/src/main/java/pl/myboardgames/dto/BoardGameShortDto.java @@ -16,6 +16,7 @@ public class BoardGameShortDto { private Long id; private String name; + public BoardGameShortDto() { } diff --git a/module-web/src/main/java/pl/myboardgames/dto/UserListDto.java b/module-web/src/main/java/pl/myboardgames/dto/UserListDto.java new file mode 100644 index 0000000..2a08598 --- /dev/null +++ b/module-web/src/main/java/pl/myboardgames/dto/UserListDto.java @@ -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 content = new ArrayList(); + + public UserListDto() { + + } + + public UserListDto(List 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 getContent() { + return content; + } + + public void setContent(List content) { + this.content = content; + } + + + + + +} diff --git a/module-web/src/main/java/pl/myboardgames/resources/BoardGameResource.java b/module-web/src/main/java/pl/myboardgames/resources/BoardGameResource.java index 7f84940..e4097ef 100644 --- a/module-web/src/main/java/pl/myboardgames/resources/BoardGameResource.java +++ b/module-web/src/main/java/pl/myboardgames/resources/BoardGameResource.java @@ -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 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(); } diff --git a/module-web/src/main/java/pl/myboardgames/resources/UserResource.java b/module-web/src/main/java/pl/myboardgames/resources/UserResource.java index f637e6a..868786e 100644 --- a/module-web/src/main/java/pl/myboardgames/resources/UserResource.java +++ b/module-web/src/main/java/pl/myboardgames/resources/UserResource.java @@ -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 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(); }