task(module-web,module-ejb): add ManyToMany relationship between the UserEntity and BoardGameEntity
This commit is contained in:
parent
ddf0d3862f
commit
bbb61497cc
@ -1,27 +1,59 @@
|
||||
package pl.myboardgames.boardgame;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.JoinTable;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import pl.myboardgames.user.UserEntity;
|
||||
|
||||
@Entity
|
||||
@Table(name = "boardgames")
|
||||
public class BoardGameEntity {
|
||||
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
@Column(name = "id", updatable = false, nullable = false)
|
||||
private Long id;
|
||||
private String name;
|
||||
private Integer minPlayers;
|
||||
private Integer maxPlayers;
|
||||
private Integer playTime;
|
||||
|
||||
|
||||
@ManyToMany(cascade = CascadeType.ALL)
|
||||
@JoinTable(
|
||||
name = "usersboardgames",
|
||||
joinColumns = {
|
||||
@JoinColumn(name = "boardgame_id", referencedColumnName = "id")},
|
||||
inverseJoinColumns = {
|
||||
@JoinColumn(name = "user_id", referencedColumnName = "id")}
|
||||
)
|
||||
private Set<UserEntity> users = new HashSet<>();
|
||||
|
||||
public BoardGameEntity() {
|
||||
}
|
||||
|
||||
public Set<UserEntity> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public void setUsers(Set<UserEntity> users) {
|
||||
this.users = users;
|
||||
}
|
||||
|
||||
public void addUser(UserEntity user) {
|
||||
this.users.add(user);
|
||||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
@ -46,7 +78,6 @@ public class BoardGameEntity {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package pl.myboardgames.user;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.ejb.Stateless;
|
||||
import javax.persistence.EntityManager;
|
||||
@ -8,6 +9,8 @@ import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import pl.myboardgames.boardgame.BoardGameEntity;
|
||||
|
||||
@Stateless
|
||||
public class UserDao {
|
||||
|
||||
@ -49,13 +52,30 @@ public class UserDao {
|
||||
|
||||
public UserEntity update(Long aId, UserEntity aUser){
|
||||
UserEntity user = em.find(UserEntity.class, aId);
|
||||
if (aUser == null) {
|
||||
if (user == null) {
|
||||
throw new EntityNotFoundException("Can't find User for ID "
|
||||
+ aId);
|
||||
}
|
||||
em.merge(aUser);
|
||||
return user;
|
||||
}
|
||||
|
||||
public UserEntity addBoardGameToUser(Long aId, Long aBoardGameId){
|
||||
UserEntity user = em.find(UserEntity.class, aId);
|
||||
BoardGameEntity boardGame = em.find(BoardGameEntity.class, aBoardGameId);
|
||||
if (user == null) {
|
||||
throw new EntityNotFoundException("Can't find User for ID "
|
||||
+ aId);
|
||||
} else if(boardGame == null){
|
||||
throw new EntityNotFoundException("Can't find BoardGame for ID "
|
||||
+ aBoardGameId);
|
||||
}
|
||||
user.addBoardGame(boardGame);
|
||||
boardGame.addUser(user);
|
||||
em.merge(boardGame);
|
||||
em.merge(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -5,12 +5,18 @@
|
||||
*/
|
||||
package pl.myboardgames.user;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToMany;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import pl.myboardgames.boardgame.BoardGameEntity;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Nao
|
||||
@ -20,13 +26,28 @@ import javax.persistence.Table;
|
||||
public class UserEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE)
|
||||
@Column(name = "id", updatable = false, nullable = false)
|
||||
private Long id;
|
||||
private String username;
|
||||
private String description;
|
||||
private String location;
|
||||
|
||||
@ManyToMany(mappedBy = "users")
|
||||
private Set<BoardGameEntity> boardGames = new HashSet<>();
|
||||
|
||||
public Set<BoardGameEntity> getBoardGames() {
|
||||
return boardGames;
|
||||
}
|
||||
|
||||
public void setBoardGames(Set<BoardGameEntity> boardGames) {
|
||||
this.boardGames = boardGames;
|
||||
}
|
||||
|
||||
public void addBoardGame(BoardGameEntity boardGame) {
|
||||
this.boardGames.add(boardGame);
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -15,8 +15,8 @@
|
||||
<property name="use_sql_comments" value = "true"/>
|
||||
<property name="hibernate.generate_statistics" value="true" />
|
||||
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
|
||||
<!--<property name="hibernate.enable_lazy_load_no_trans"-->
|
||||
<!--value="true" />-->
|
||||
<property name="hibernate.enable_lazy_load_no_trans"
|
||||
value="true" />
|
||||
</properties>
|
||||
</persistence-unit>
|
||||
</persistence>
|
@ -49,7 +49,7 @@ public class BoardGameController {
|
||||
@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();
|
||||
return Response.status(200).entity(ret).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@ -60,7 +60,7 @@ public class BoardGameController {
|
||||
BoardGameEntity boardGame = BoardGameMapper.fromDto(aBoardGame);
|
||||
boardGame.setId(aId);
|
||||
BoardGameDto ret = new BoardGameDto(boardGameDatabase.update(aId, boardGame));
|
||||
return Response.status(201).entity(ret).build();
|
||||
return Response.status(200).entity(ret).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,11 @@
|
||||
*/
|
||||
package pl.myboardgames.boardgame;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import pl.myboardgames.user.UserShortDto;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
@ -18,6 +23,8 @@ public class BoardGameDto {
|
||||
private Integer maxPlayers;
|
||||
private Integer playTime;
|
||||
|
||||
private Set<UserShortDto> users = new HashSet<>();
|
||||
|
||||
public BoardGameDto(){
|
||||
|
||||
}
|
||||
@ -29,6 +36,7 @@ public class BoardGameDto {
|
||||
maxPlayers = aBoardGame.getMaxPlayers();
|
||||
playTime = aBoardGame.getPlayTime();
|
||||
id = aBoardGame.getId();
|
||||
users = aBoardGame.getUsers().stream().map(ent -> new UserShortDto(ent)).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
@ -70,5 +78,13 @@ public class BoardGameDto {
|
||||
public void setPlayTime(Integer playTime) {
|
||||
this.playTime = playTime;
|
||||
}
|
||||
|
||||
public Set<UserShortDto> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public void setUsers(Set<UserShortDto> users) {
|
||||
this.users = users;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -13,17 +13,15 @@ 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;
|
||||
@ -33,6 +31,12 @@ public class BoardGameShortDto {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.core.Response;
|
||||
import pl.myboardgames.boardgame.BoardGameShortDto;
|
||||
|
||||
@Path("/user")
|
||||
@RequestScoped
|
||||
@ -24,8 +25,7 @@ public class UserController {
|
||||
@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();
|
||||
return Response.status(200).entity(userDatabase.getAll().stream().map(ent -> new UserShortDto(ent)).collect(Collectors.toList())).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@ -50,7 +50,7 @@ public class UserController {
|
||||
@Produces("application/json; charset=UTF-8")
|
||||
public Response removeUser(@PathParam("id") Long aId) {
|
||||
Boolean ret = userDatabase.remove(aId);
|
||||
return Response.status(201).entity(ret).build();
|
||||
return Response.status(200).entity(ret).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@ -61,7 +61,17 @@ public class UserController {
|
||||
UserEntity user = UserMapper.fromDto(aUser);
|
||||
user.setId(aId);
|
||||
UserDto ret = new UserDto(userDatabase.update(aId,user));
|
||||
return Response.status(201).entity(ret).build();
|
||||
return Response.status(200).entity(ret).build();
|
||||
}
|
||||
|
||||
@PUT
|
||||
@Path("{id}/boardgame")
|
||||
@Consumes("application/json; charset=UTF-8")
|
||||
@Produces("application/json; charset=UTF-8")
|
||||
public Response addBoradGameToUser(BoardGameShortDto aBoardGame, @PathParam("id") Long aId) {
|
||||
Long aBoardGameId = aBoardGame.getId();
|
||||
UserDto ret = new UserDto(userDatabase.addBoardGameToUser(aId,aBoardGameId));
|
||||
return Response.status(200).entity(ret).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,12 @@
|
||||
*/
|
||||
package pl.myboardgames.user;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import pl.myboardgames.boardgame.BoardGameShortDto;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Nao
|
||||
@ -16,6 +22,7 @@ public class UserDto {
|
||||
private String description;
|
||||
private String location;
|
||||
|
||||
private Set<BoardGameShortDto> boardGames = new HashSet<>();
|
||||
|
||||
public UserDto() {
|
||||
|
||||
@ -26,6 +33,8 @@ public class UserDto {
|
||||
id = aUser.getId();
|
||||
description = aUser.getDescription();
|
||||
location = aUser.getLocation();
|
||||
boardGames = aUser.getBoardGames().stream().map(ent -> new BoardGameShortDto(ent)).collect(Collectors.toSet());
|
||||
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
@ -60,4 +69,12 @@ public class UserDto {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public Set<BoardGameShortDto> getBoardGames() {
|
||||
return boardGames;
|
||||
}
|
||||
|
||||
public void setBoardGames(Set<BoardGameShortDto> boardGames) {
|
||||
this.boardGames = boardGames;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -23,6 +23,14 @@ public class UserShortDto {
|
||||
username = aUser.getUsername();
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user