task(module-web,module-ejb): add CRUD for the BoardGameEntity
This commit is contained in:
parent
7ecd940ab4
commit
ddf0d3862f
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -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;
|
@ -1,4 +1,4 @@
|
||||
package pl.myboardgames;
|
||||
package pl.myboardgames.user;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
@ -11,8 +11,6 @@ import javax.persistence.Query;
|
||||
@Stateless
|
||||
public class UserDao {
|
||||
|
||||
private Set<UserEntity> users;
|
||||
|
||||
@PersistenceContext
|
||||
EntityManager em;
|
||||
|
@ -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,7 +15,6 @@ import javax.persistence.Table;
|
||||
*
|
||||
* @author Nao
|
||||
*/
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class UserEntity {
|
||||
@ -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) {
|
||||
@ -47,10 +51,12 @@ public class UserEntity {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
||||
|
||||
@GET
|
||||
@Produces("application/json; charset=UTF-8")
|
||||
public Response getAllBoardGames() {
|
||||
@ -43,7 +44,23 @@ public class BoardGameController {
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -26,6 +26,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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -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;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -14,9 +14,25 @@ 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package pl.myboardgames;
|
||||
package pl.myboardgames.user;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
import javax.ejb.EJB;
|
@ -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,7 +13,9 @@ public class UserDto {
|
||||
|
||||
private Long id;
|
||||
private String username;
|
||||
private String address;
|
||||
private String description;
|
||||
private String location;
|
||||
|
||||
|
||||
public UserDto() {
|
||||
|
||||
@ -22,6 +24,8 @@ public class UserDto {
|
||||
public UserDto(UserEntity aUser) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
@ -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() {
|
||||
@ -29,10 +31,6 @@ public class UserShortDto {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user