task(module-web): bring back UserEntity

This commit is contained in:
Damian Pierzchalski 2018-12-08 22:22:16 +01:00
parent 8a2c36bdec
commit f693e1804c
11 changed files with 229 additions and 30 deletions

View File

@ -13,15 +13,13 @@ public class BoardGameDao {
boardgames = new HashSet<>();
BoardGameEntity board1 = new BoardGameEntity();
BoardGameEntity board2 = new BoardGameEntity();
board1.setName("Dominion");
board2.setName("Agricola");
board1.setId(new Long(0));
board2.setId(new Long(1));
BoardGameEntity boardGame1 = new BoardGameEntity();
BoardGameEntity boardGame2 = new BoardGameEntity();
boardGame1.setName("Dominion");
boardGame2.setName("Agricola");
boardgames.add(board1);
boardgames.add(board2);
boardgames.add(boardGame1);
boardgames.add(boardGame2);
}
public Set<BoardGameEntity>getAll() {
@ -29,7 +27,7 @@ public class BoardGameDao {
}
public BoardGameEntity getById(Long aId) {
return boardgames.stream().filter(book -> book.getId().equals(aId)).findFirst().get();
return boardgames.stream().filter(boardGame -> boardGame.getId().equals(aId)).findFirst().get();
}
public BoardGameEntity add(BoardGameEntity aBoardGame) {

View File

@ -1,6 +1,5 @@
package pl.myboardgames;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@ -15,8 +14,7 @@ public class BoardGameEntity {
private Integer minPlayers;
private Integer maxPlayers;
private Integer playTime;
private String location;
public BoardGameEntity() {
}
@ -36,10 +34,6 @@ public class BoardGameEntity {
return playTime;
}
public String getLocation() {
return location;
}
public void setId(Long id) {
this.id = id;
}
@ -65,8 +59,4 @@ public class BoardGameEntity {
this.playTime = playTime;
}
public void setLocation(String location) {
this.location = location;
}
}

View File

@ -0,0 +1,40 @@
package pl.myboardgames;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.ejb.Stateless;
@Stateless
public class UserDao {
private Set<UserEntity> users;
public UserDao() {
users = new HashSet<>();
UserEntity user = new UserEntity();
user.setUsername("test");
users.add(user);
}
public Set<UserEntity>getAll() {
return users;
}
public UserEntity getById(Long aId) {
return users.stream().filter(user -> user.getId().equals(aId)).findFirst().get();
}
public UserEntity add(UserEntity aUser) {
aUser.setId(Long.valueOf(users.size()+1));
users.add(aUser);
return aUser;
}
}

View File

@ -0,0 +1,44 @@
/*
* 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;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
*
* @author Nao
*/
@Entity
public class UserEntity {
@Id
@GeneratedValue
private Long id;
private String username;
public Long getId() {
return id;
}
public String getUsername() {
return username;
}
public void setId(Long id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
}

View File

@ -29,7 +29,7 @@ public class BoardGameController {
@GET
@Path("{id}")
@Produces("application/json; charset=UTF-8")
public Response getAllBoardGames(@PathParam("id") Long aId) {
public Response getBoardGame(@PathParam("id") Long aId) {
BoardGameEntity ret = boardGameDatabase.getById(aId);
return Response.status(200).entity(ret).build();
}
@ -37,7 +37,7 @@ public class BoardGameController {
@POST
@Consumes("application/json; charset=UTF-8")
@Produces("application/json; charset=UTF-8")
public Response addBook(BoardGameDto aBoardGame) {
public Response addBoardGame(BoardGameDto aBoardGame) {
BoardGameEntity boardGame = BoardGameMapper.fromDto(aBoardGame);
BoardGameDto ret = new BoardGameDto(boardGameDatabase.add(boardGame));
return Response.status(201).entity(ret).build();

View File

@ -5,7 +5,6 @@
*/
package pl.myboardgames;
import java.util.List;
/**
*
@ -18,7 +17,6 @@ public class BoardGameDto {
private Integer minPlayers;
private Integer maxPlayers;
private Integer playTime;
private String location;
public BoardGameDto() {
}
@ -29,7 +27,6 @@ public class BoardGameDto {
this.minPlayers = aBoardGame.getMinPlayers();
this.maxPlayers = aBoardGame.getMaxPlayers();
this.playTime = aBoardGame.getPlayTime();
this.location = aBoardGame.getLocation();
this.id = aBoardGame.getId();
}
@ -52,9 +49,5 @@ public class BoardGameDto {
public Integer getPlayTime() {
return playTime;
}
public String getLocation() {
return location;
}
}

View File

@ -21,7 +21,6 @@ class BoardGameMapper {
ret.setMinPlayers(aBoardGame.getMinPlayers());
ret.setMaxPlayers(aBoardGame.getMaxPlayers());
ret.setPlayTime(aBoardGame.getPlayTime());
ret.setLocation(aBoardGame.getLocation());
ret.setId(aBoardGame.getId());
return ret;

View File

@ -0,0 +1,49 @@
package pl.myboardgames;
import java.util.stream.Collectors;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
@Path("/user")
@RequestScoped
public class UserController {
@EJB
UserDao userDatabase;
@GET
@Produces("application/json; charset=UTF-8")
public Response getAllUsers() {
userDatabase.getAll().stream().map(ent -> new UserShortDto(ent)).collect(Collectors.toList());
return Response.status(200).entity(userDatabase.getAll()).build();
}
@GET
@Path("{id}")
@Produces("application/json; charset=UTF-8")
public Response getUser(@PathParam("id") Long aId) {
UserEntity ret = userDatabase.getById(aId);
return Response.status(200).entity(ret).build();
}
@POST
@Consumes("application/json; charset=UTF-8")
@Produces("application/json; charset=UTF-8")
public Response addUser(UserDto aUser) {
UserEntity user = UserMapper.fromDto(aUser);
UserDto ret = new UserDto(userDatabase.add(user));
return Response.status(201).entity(ret).build();
}
}

View File

@ -0,0 +1,36 @@
/*
* 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;
import java.util.List;
/**
*
* @author Nao
*/
public class UserDto {
private Long id;
private String username;
public UserDto() {
}
public UserDto(UserEntity aUser) {
this.username = aUser.getUsername();
this.id = aUser.getId();
}
public Long getId() {
return id;
}
public String getUsername() {
return username;
}
}

View File

@ -0,0 +1,28 @@
/*
* To change ret license header, choose License Headers in Project Properties.
* To change ret template file, choose Tools | Templates
* and open the template in the editor.
*/
package pl.myboardgames;
/**
*
* @author Nao
*/
class UserMapper {
public UserMapper() {
}
public static UserEntity fromDto(UserDto aUser) {
UserEntity ret = new UserEntity();
ret.setUsername(aUser.getUsername());
ret.setId(aUser.getId());
return ret;
}
}

View File

@ -0,0 +1,22 @@
/*
* 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 UserShortDto {
private Long id;
private String username;
public UserShortDto(UserEntity aUser) {
id = aUser.getId();
username = aUser.getUsername();
}
}