diff --git a/module-ejb/src/main/java/pl/myboardgames/BoardGameDao.java b/module-ejb/src/main/java/pl/myboardgames/BoardGameDao.java index 27d2bb1..94dda9c 100644 --- a/module-ejb/src/main/java/pl/myboardgames/BoardGameDao.java +++ b/module-ejb/src/main/java/pl/myboardgames/BoardGameDao.java @@ -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 SetgetAll() { @@ -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) { diff --git a/module-ejb/src/main/java/pl/myboardgames/BoardGameEntity.java b/module-ejb/src/main/java/pl/myboardgames/BoardGameEntity.java index 3a0aed6..4b2647c 100644 --- a/module-ejb/src/main/java/pl/myboardgames/BoardGameEntity.java +++ b/module-ejb/src/main/java/pl/myboardgames/BoardGameEntity.java @@ -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; - } - } diff --git a/module-ejb/src/main/java/pl/myboardgames/UserDao.java b/module-ejb/src/main/java/pl/myboardgames/UserDao.java new file mode 100644 index 0000000..666b689 --- /dev/null +++ b/module-ejb/src/main/java/pl/myboardgames/UserDao.java @@ -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 users; + + public UserDao() { + + users = new HashSet<>(); + + UserEntity user = new UserEntity(); + user.setUsername("test"); + + users.add(user); + } + + public SetgetAll() { + 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; + } + + + +} diff --git a/module-ejb/src/main/java/pl/myboardgames/UserEntity.java b/module-ejb/src/main/java/pl/myboardgames/UserEntity.java new file mode 100644 index 0000000..394262d --- /dev/null +++ b/module-ejb/src/main/java/pl/myboardgames/UserEntity.java @@ -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; + } + +} + + diff --git a/module-web/src/main/java/pl/myboardgames/BoardGameController.java b/module-web/src/main/java/pl/myboardgames/BoardGameController.java index 8e05328..f891cdb 100644 --- a/module-web/src/main/java/pl/myboardgames/BoardGameController.java +++ b/module-web/src/main/java/pl/myboardgames/BoardGameController.java @@ -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(); diff --git a/module-web/src/main/java/pl/myboardgames/BoardGameDto.java b/module-web/src/main/java/pl/myboardgames/BoardGameDto.java index 292f0f2..aae7ad1 100644 --- a/module-web/src/main/java/pl/myboardgames/BoardGameDto.java +++ b/module-web/src/main/java/pl/myboardgames/BoardGameDto.java @@ -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; - } } diff --git a/module-web/src/main/java/pl/myboardgames/BoardGameMapper.java b/module-web/src/main/java/pl/myboardgames/BoardGameMapper.java index bde729f..999e05a 100644 --- a/module-web/src/main/java/pl/myboardgames/BoardGameMapper.java +++ b/module-web/src/main/java/pl/myboardgames/BoardGameMapper.java @@ -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; diff --git a/module-web/src/main/java/pl/myboardgames/UserController.java b/module-web/src/main/java/pl/myboardgames/UserController.java new file mode 100644 index 0000000..edd9f18 --- /dev/null +++ b/module-web/src/main/java/pl/myboardgames/UserController.java @@ -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(); + } + +} + + + \ No newline at end of file diff --git a/module-web/src/main/java/pl/myboardgames/UserDto.java b/module-web/src/main/java/pl/myboardgames/UserDto.java new file mode 100644 index 0000000..3053c9a --- /dev/null +++ b/module-web/src/main/java/pl/myboardgames/UserDto.java @@ -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; + } + +} diff --git a/module-web/src/main/java/pl/myboardgames/UserMapper.java b/module-web/src/main/java/pl/myboardgames/UserMapper.java new file mode 100644 index 0000000..c55e4e8 --- /dev/null +++ b/module-web/src/main/java/pl/myboardgames/UserMapper.java @@ -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; + } + + + +} diff --git a/module-web/src/main/java/pl/myboardgames/UserShortDto.java b/module-web/src/main/java/pl/myboardgames/UserShortDto.java new file mode 100644 index 0000000..9401e94 --- /dev/null +++ b/module-web/src/main/java/pl/myboardgames/UserShortDto.java @@ -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(); + } + +}