From ddf0d3862f73d97c9eca2bd9bc738b70e3333a1c Mon Sep 17 00:00:00 2001 From: Damian Pierzchalski Date: Sun, 16 Dec 2018 20:09:53 +0100 Subject: [PATCH] task(module-web,module-ejb): add CRUD for the BoardGameEntity --- .../java/pl/myboardgames/BoardGameDao.java | 43 ----------- .../myboardgames/boardgame/BoardGameDao.java | 62 ++++++++++++++++ .../{ => boardgame}/BoardGameEntity.java | 8 +- .../pl/myboardgames/{ => user}/UserDao.java | 4 +- .../myboardgames/{ => user}/UserEntity.java | 30 +++++--- .../java/pl/myboardgames/BoardGameDto.java | 53 ------------- .../{ => boardgame}/BoardGameController.java | 39 +++++++--- .../myboardgames/boardgame/BoardGameDto.java | 74 +++++++++++++++++++ .../{ => boardgame}/BoardGameMapper.java | 11 ++- .../{ => boardgame}/BoardGameShortDto.java | 18 ++++- .../{ => user}/UserController.java | 2 +- .../pl/myboardgames/{ => user}/UserDto.java | 28 +++++-- .../myboardgames/{ => user}/UserMapper.java | 6 +- .../myboardgames/{ => user}/UserShortDto.java | 12 ++- 14 files changed, 246 insertions(+), 144 deletions(-) delete mode 100644 module-ejb/src/main/java/pl/myboardgames/BoardGameDao.java create mode 100644 module-ejb/src/main/java/pl/myboardgames/boardgame/BoardGameDao.java rename module-ejb/src/main/java/pl/myboardgames/{ => boardgame}/BoardGameEntity.java (85%) rename module-ejb/src/main/java/pl/myboardgames/{ => user}/UserDao.java (92%) rename module-ejb/src/main/java/pl/myboardgames/{ => user}/UserEntity.java (63%) delete mode 100644 module-web/src/main/java/pl/myboardgames/BoardGameDto.java rename module-web/src/main/java/pl/myboardgames/{ => boardgame}/BoardGameController.java (59%) create mode 100644 module-web/src/main/java/pl/myboardgames/boardgame/BoardGameDto.java rename module-web/src/main/java/pl/myboardgames/{ => boardgame}/BoardGameMapper.java (67%) rename module-web/src/main/java/pl/myboardgames/{ => boardgame}/BoardGameShortDto.java (63%) rename module-web/src/main/java/pl/myboardgames/{ => user}/UserController.java (98%) rename module-web/src/main/java/pl/myboardgames/{ => user}/UserDto.java (51%) rename module-web/src/main/java/pl/myboardgames/{ => user}/UserMapper.java (80%) rename module-web/src/main/java/pl/myboardgames/{ => user}/UserShortDto.java (74%) diff --git a/module-ejb/src/main/java/pl/myboardgames/BoardGameDao.java b/module-ejb/src/main/java/pl/myboardgames/BoardGameDao.java deleted file mode 100644 index d96aa18..0000000 --- a/module-ejb/src/main/java/pl/myboardgames/BoardGameDao.java +++ /dev/null @@ -1,43 +0,0 @@ -package pl.myboardgames; - -import java.util.HashSet; -import java.util.Set; -import javax.ejb.Stateless; - -@Stateless -public class BoardGameDao { - - private Set boardgames; - - public BoardGameDao() { - - boardgames = new HashSet<>(); - - BoardGameEntity boardGame1 = new BoardGameEntity(); - BoardGameEntity boardGame2 = new BoardGameEntity(); - boardGame1.setName("Dominion"); - boardGame2.setName("Agricola"); - boardGame1.setId(new Long(0)); - boardGame2.setId(new Long(1)); - - boardgames.add(boardGame1); - boardgames.add(boardGame2); - } - - public SetgetAll() { - return boardgames; - } - - public BoardGameEntity getById(Long aId) { - return boardgames.stream().filter(boardGame -> boardGame.getId().equals(aId)).findFirst().get(); - } - - public BoardGameEntity add(BoardGameEntity aBoardGame) { - aBoardGame.setId(Long.valueOf(boardgames.size()+1)); - boardgames.add(aBoardGame); - return aBoardGame; - } - - - -} diff --git a/module-ejb/src/main/java/pl/myboardgames/boardgame/BoardGameDao.java b/module-ejb/src/main/java/pl/myboardgames/boardgame/BoardGameDao.java new file mode 100644 index 0000000..c913a88 --- /dev/null +++ b/module-ejb/src/main/java/pl/myboardgames/boardgame/BoardGameDao.java @@ -0,0 +1,62 @@ +package pl.myboardgames.boardgame; + +import java.util.HashSet; +import java.util.Set; +import javax.ejb.Stateless; +import javax.persistence.EntityManager; +import javax.persistence.EntityNotFoundException; +import javax.persistence.PersistenceContext; +import javax.persistence.Query; + +@Stateless +public class BoardGameDao { + + @PersistenceContext + EntityManager em; + + public BoardGameDao() { + } + + public SetgetAll() { + Query query = em.createQuery("SELECT u FROM BoardGameEntity u", BoardGameEntity.class); + Set boardGames = new HashSet(query.getResultList()); + return boardGames; + } + + public BoardGameEntity getById(Long aId) { + BoardGameEntity boardGame = em.find(BoardGameEntity.class, aId); + if (boardGame == null) { + throw new EntityNotFoundException("Can't find BoardGame for ID " + + aId); + } + return boardGame; + } + + public BoardGameEntity add(BoardGameEntity aBoardGame) { + em.persist(aBoardGame); + return aBoardGame; + } + + public Boolean remove(Long aId) { + BoardGameEntity boardGame = em.find(BoardGameEntity.class, aId); + if (boardGame == null) { + throw new EntityNotFoundException("Can't find BoardGame for ID " + + aId); + } + em.remove(boardGame); + return true; + } + + public BoardGameEntity update(Long aId, BoardGameEntity aBoardGame){ + BoardGameEntity boardGame = em.find(BoardGameEntity.class, aId); + if (aBoardGame == null) { + throw new EntityNotFoundException("Can't find BoardGame for ID " + + aId); + } + em.merge(aBoardGame); + return boardGame; + } + + + +} diff --git a/module-ejb/src/main/java/pl/myboardgames/BoardGameEntity.java b/module-ejb/src/main/java/pl/myboardgames/boardgame/BoardGameEntity.java similarity index 85% rename from module-ejb/src/main/java/pl/myboardgames/BoardGameEntity.java rename to module-ejb/src/main/java/pl/myboardgames/boardgame/BoardGameEntity.java index 4b2647c..c874ce4 100644 --- a/module-ejb/src/main/java/pl/myboardgames/BoardGameEntity.java +++ b/module-ejb/src/main/java/pl/myboardgames/boardgame/BoardGameEntity.java @@ -1,14 +1,18 @@ -package pl.myboardgames; +package pl.myboardgames.boardgame; +import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; +import javax.persistence.Table; @Entity +@Table(name = "boardgames") public class BoardGameEntity { - @GeneratedValue @Id + @GeneratedValue + @Column(name = "id", updatable = false, nullable = false) private Long id; private String name; private Integer minPlayers; diff --git a/module-ejb/src/main/java/pl/myboardgames/UserDao.java b/module-ejb/src/main/java/pl/myboardgames/user/UserDao.java similarity index 92% rename from module-ejb/src/main/java/pl/myboardgames/UserDao.java rename to module-ejb/src/main/java/pl/myboardgames/user/UserDao.java index 2f7f0c0..6cd4f36 100644 --- a/module-ejb/src/main/java/pl/myboardgames/UserDao.java +++ b/module-ejb/src/main/java/pl/myboardgames/user/UserDao.java @@ -1,4 +1,4 @@ -package pl.myboardgames; +package pl.myboardgames.user; import java.util.HashSet; import java.util.Set; @@ -10,8 +10,6 @@ import javax.persistence.Query; @Stateless public class UserDao { - - private Set users; @PersistenceContext EntityManager em; diff --git a/module-ejb/src/main/java/pl/myboardgames/UserEntity.java b/module-ejb/src/main/java/pl/myboardgames/user/UserEntity.java similarity index 63% rename from module-ejb/src/main/java/pl/myboardgames/UserEntity.java rename to module-ejb/src/main/java/pl/myboardgames/user/UserEntity.java index e3e7276..20b876a 100644 --- a/module-ejb/src/main/java/pl/myboardgames/UserEntity.java +++ b/module-ejb/src/main/java/pl/myboardgames/user/UserEntity.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package pl.myboardgames; +package pl.myboardgames.user; import javax.persistence.Column; import javax.persistence.Entity; @@ -15,9 +15,8 @@ import javax.persistence.Table; * * @author Nao */ - @Entity -@Table( name = "users" ) +@Table(name = "users") public class UserEntity { @Id @@ -25,7 +24,8 @@ public class UserEntity { @Column(name = "id", updatable = false, nullable = false) private Long id; private String username; - private String address; + private String description; + private String location; public Long getId() { return id; @@ -35,8 +35,12 @@ public class UserEntity { return username; } - public String getAddress() { - return address; + public String getDescription() { + return description; + } + + public String getLocation() { + return location; } public void setId(Long id) { @@ -46,11 +50,13 @@ public class UserEntity { public void setUsername(String username) { this.username = username; } - - public void setAddress(String address) { - this.address = address; + + public void setDescription(String description) { + this.description = description; } - + + public void setLocation(String location) { + this.location = location; + } + } - - diff --git a/module-web/src/main/java/pl/myboardgames/BoardGameDto.java b/module-web/src/main/java/pl/myboardgames/BoardGameDto.java deleted file mode 100644 index aae7ad1..0000000 --- a/module-web/src/main/java/pl/myboardgames/BoardGameDto.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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; - - -/** - * - * @author Nao - */ -public class BoardGameDto { - - private Long id; - private String name; - private Integer minPlayers; - private Integer maxPlayers; - private Integer playTime; - - public BoardGameDto() { - } - - - public BoardGameDto(BoardGameEntity aBoardGame) { - this.name = aBoardGame.getName(); - this.minPlayers = aBoardGame.getMinPlayers(); - this.maxPlayers = aBoardGame.getMaxPlayers(); - this.playTime = aBoardGame.getPlayTime(); - this.id = aBoardGame.getId(); - } - - public Long getId() { - return id; - } - - public String getName() { - return name; - } - - public Integer getMinPlayers() { - return minPlayers; - } - - public Integer getMaxPlayers() { - return maxPlayers; - } - - public Integer getPlayTime() { - return playTime; - } - -} diff --git a/module-web/src/main/java/pl/myboardgames/BoardGameController.java b/module-web/src/main/java/pl/myboardgames/boardgame/BoardGameController.java similarity index 59% rename from module-web/src/main/java/pl/myboardgames/BoardGameController.java rename to module-web/src/main/java/pl/myboardgames/boardgame/BoardGameController.java index f891cdb..a87c861 100644 --- a/module-web/src/main/java/pl/myboardgames/BoardGameController.java +++ b/module-web/src/main/java/pl/myboardgames/boardgame/BoardGameController.java @@ -1,24 +1,25 @@ -package pl.myboardgames; +package pl.myboardgames.boardgame; import java.util.stream.Collectors; import javax.ejb.EJB; import javax.enterprise.context.RequestScoped; import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; +import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; -@Path("/board") +@Path("/boardgame") @RequestScoped public class BoardGameController { - @EJB - BoardGameDao boardGameDatabase; - - + @EJB + BoardGameDao boardGameDatabase; + @GET @Produces("application/json; charset=UTF-8") public Response getAllBoardGames() { @@ -33,7 +34,7 @@ public class BoardGameController { BoardGameEntity ret = boardGameDatabase.getById(aId); return Response.status(200).entity(ret).build(); } - + @POST @Consumes("application/json; charset=UTF-8") @Produces("application/json; charset=UTF-8") @@ -42,8 +43,24 @@ public class BoardGameController { BoardGameDto ret = new BoardGameDto(boardGameDatabase.add(boardGame)); return Response.status(201).entity(ret).build(); } - + + @DELETE + @Path("{id}") + @Produces("application/json; charset=UTF-8") + public Response removeBoardGame(@PathParam("id") Long aId) { + Boolean ret = boardGameDatabase.remove(aId); + return Response.status(201).entity(ret).build(); + } + + @PUT + @Path("{id}") + @Consumes("application/json; charset=UTF-8") + @Produces("application/json; charset=UTF-8") + public Response updateBoardGame(BoardGameDto aBoardGame, @PathParam("id") Long aId) { + BoardGameEntity boardGame = BoardGameMapper.fromDto(aBoardGame); + boardGame.setId(aId); + BoardGameDto ret = new BoardGameDto(boardGameDatabase.update(aId, boardGame)); + return Response.status(201).entity(ret).build(); + } + } - - - \ No newline at end of file diff --git a/module-web/src/main/java/pl/myboardgames/boardgame/BoardGameDto.java b/module-web/src/main/java/pl/myboardgames/boardgame/BoardGameDto.java new file mode 100644 index 0000000..317e3d9 --- /dev/null +++ b/module-web/src/main/java/pl/myboardgames/boardgame/BoardGameDto.java @@ -0,0 +1,74 @@ +/* + * 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.boardgame; + + +/** + * + * @author Nao + */ +public class BoardGameDto { + + private Long id; + private String name; + private Integer minPlayers; + private Integer maxPlayers; + private Integer playTime; + + public BoardGameDto(){ + + } + + + public BoardGameDto(BoardGameEntity aBoardGame) { + name = aBoardGame.getName(); + minPlayers = aBoardGame.getMinPlayers(); + maxPlayers = aBoardGame.getMaxPlayers(); + playTime = aBoardGame.getPlayTime(); + id = aBoardGame.getId(); + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public Integer getMinPlayers() { + return minPlayers; + } + + public Integer getMaxPlayers() { + return maxPlayers; + } + + public Integer getPlayTime() { + return playTime; + } + + public void setId(Long id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } + + public void setMinPlayers(Integer minPlayers) { + this.minPlayers = minPlayers; + } + + public void setMaxPlayers(Integer maxPlayers) { + this.maxPlayers = maxPlayers; + } + + public void setPlayTime(Integer playTime) { + this.playTime = playTime; + } + +} diff --git a/module-web/src/main/java/pl/myboardgames/BoardGameMapper.java b/module-web/src/main/java/pl/myboardgames/boardgame/BoardGameMapper.java similarity index 67% rename from module-web/src/main/java/pl/myboardgames/BoardGameMapper.java rename to module-web/src/main/java/pl/myboardgames/boardgame/BoardGameMapper.java index 999e05a..f67336a 100644 --- a/module-web/src/main/java/pl/myboardgames/BoardGameMapper.java +++ b/module-web/src/main/java/pl/myboardgames/boardgame/BoardGameMapper.java @@ -3,7 +3,7 @@ * To change ret template file, choose Tools | Templates * and open the template in the editor. */ -package pl.myboardgames; +package pl.myboardgames.boardgame; /** * @@ -25,6 +25,15 @@ class BoardGameMapper { return ret; } + + public static BoardGameEntity fromShortDto(BoardGameShortDto aBoardGame) { + BoardGameEntity ret = new BoardGameEntity(); + + ret.setName(aBoardGame.getName()); + ret.setId(aBoardGame.getId()); + + return ret; + } diff --git a/module-web/src/main/java/pl/myboardgames/BoardGameShortDto.java b/module-web/src/main/java/pl/myboardgames/boardgame/BoardGameShortDto.java similarity index 63% rename from module-web/src/main/java/pl/myboardgames/BoardGameShortDto.java rename to module-web/src/main/java/pl/myboardgames/boardgame/BoardGameShortDto.java index 2c56600..7999456 100644 --- a/module-web/src/main/java/pl/myboardgames/BoardGameShortDto.java +++ b/module-web/src/main/java/pl/myboardgames/boardgame/BoardGameShortDto.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package pl.myboardgames; +package pl.myboardgames.boardgame; /** * @@ -13,10 +13,26 @@ public class BoardGameShortDto { private Long id; private String name; + + + public BoardGameShortDto() { + } + public BoardGameShortDto(BoardGameEntity aBoardGame) { id = aBoardGame.getId(); name = aBoardGame.getName(); } + + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + } diff --git a/module-web/src/main/java/pl/myboardgames/UserController.java b/module-web/src/main/java/pl/myboardgames/user/UserController.java similarity index 98% rename from module-web/src/main/java/pl/myboardgames/UserController.java rename to module-web/src/main/java/pl/myboardgames/user/UserController.java index f75ee79..bda87fd 100644 --- a/module-web/src/main/java/pl/myboardgames/UserController.java +++ b/module-web/src/main/java/pl/myboardgames/user/UserController.java @@ -1,4 +1,4 @@ -package pl.myboardgames; +package pl.myboardgames.user; import java.util.stream.Collectors; import javax.ejb.EJB; diff --git a/module-web/src/main/java/pl/myboardgames/UserDto.java b/module-web/src/main/java/pl/myboardgames/user/UserDto.java similarity index 51% rename from module-web/src/main/java/pl/myboardgames/UserDto.java rename to module-web/src/main/java/pl/myboardgames/user/UserDto.java index 6834008..aab9bde 100644 --- a/module-web/src/main/java/pl/myboardgames/UserDto.java +++ b/module-web/src/main/java/pl/myboardgames/user/UserDto.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package pl.myboardgames; +package pl.myboardgames.user; /** * @@ -13,15 +13,19 @@ public class UserDto { private Long id; private String username; - private String address; + private String description; + private String location; + public UserDto() { } public UserDto(UserEntity aUser) { - username = aUser.getUsername(); - id = aUser.getId(); + username = aUser.getUsername(); + id = aUser.getId(); + description = aUser.getDescription(); + location = aUser.getLocation(); } public Long getId() { @@ -32,8 +36,16 @@ public class UserDto { return username; } - public String getAddress() { - return address; + public String getDescription() { + return description; + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; } public void setId(Long id) { @@ -44,8 +56,8 @@ public class UserDto { this.username = username; } - public void setAddress(String address) { - this.address = address; + public void setDescription(String description) { + this.description = description; } } diff --git a/module-web/src/main/java/pl/myboardgames/UserMapper.java b/module-web/src/main/java/pl/myboardgames/user/UserMapper.java similarity index 80% rename from module-web/src/main/java/pl/myboardgames/UserMapper.java rename to module-web/src/main/java/pl/myboardgames/user/UserMapper.java index d52dbdb..83ba82a 100644 --- a/module-web/src/main/java/pl/myboardgames/UserMapper.java +++ b/module-web/src/main/java/pl/myboardgames/user/UserMapper.java @@ -3,7 +3,7 @@ * To change ret template file, choose Tools | Templates * and open the template in the editor. */ -package pl.myboardgames; +package pl.myboardgames.user; /** * @@ -17,8 +17,10 @@ class UserMapper { public static UserEntity fromDto(UserDto aUser) { UserEntity ret = new UserEntity(); - ret.setUsername(aUser.getUsername()); ret.setId(aUser.getId()); + ret.setUsername(aUser.getUsername()); + ret.setDescription(aUser.getDescription()); + ret.setLocation(aUser.getLocation()); return ret; } diff --git a/module-web/src/main/java/pl/myboardgames/UserShortDto.java b/module-web/src/main/java/pl/myboardgames/user/UserShortDto.java similarity index 74% rename from module-web/src/main/java/pl/myboardgames/UserShortDto.java rename to module-web/src/main/java/pl/myboardgames/user/UserShortDto.java index 0835ef0..19c0637 100644 --- a/module-web/src/main/java/pl/myboardgames/UserShortDto.java +++ b/module-web/src/main/java/pl/myboardgames/user/UserShortDto.java @@ -3,7 +3,7 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package pl.myboardgames; +package pl.myboardgames.user; /** * @@ -13,12 +13,14 @@ public class UserShortDto { private Long id; private String username; - private String address; + + public UserShortDto() { + + } public UserShortDto(UserEntity aUser) { id = aUser.getId(); username = aUser.getUsername(); - address = aUser.getUsername(); } public Long getId() { @@ -28,10 +30,6 @@ public class UserShortDto { public String getUsername() { return username; } - - public String getAddress() { - return address; - }