task(module-web,module-ejb): add CRUD for the BoardGameEntity

This commit is contained in:
Damian Pierzchalski 2018-12-16 20:09:53 +01:00
parent 7ecd940ab4
commit ddf0d3862f
14 changed files with 246 additions and 144 deletions

View File

@ -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<BoardGameEntity> 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 Set<BoardGameEntity>getAll() {
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;
}
}

View File

@ -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 Set<BoardGameEntity>getAll() {
Query query = em.createQuery("SELECT u FROM BoardGameEntity u", BoardGameEntity.class);
Set<BoardGameEntity> boardGames = new HashSet<BoardGameEntity>(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;
}
}

View File

@ -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;

View File

@ -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<UserEntity> users;
@PersistenceContext
EntityManager em;

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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;
}
}

View File

@ -1,4 +1,4 @@
package pl.myboardgames;
package pl.myboardgames.user;
import java.util.stream.Collectors;
import javax.ejb.EJB;

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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;
}