task(module-web,module-ejb): add address and CRUD for the UserEntity

This commit is contained in:
Damian Pierzchalski 2018-12-15 23:08:13 +01:00
parent 10e636d034
commit 7ecd940ab4
6 changed files with 92 additions and 19 deletions

View File

@ -1,10 +1,10 @@
package pl.myboardgames;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@ -26,7 +26,12 @@ public class UserDao {
}
public UserEntity getById(Long aId) {
return em.find(UserEntity.class, aId);
UserEntity user = em.find(UserEntity.class, aId);
if (user == null) {
throw new EntityNotFoundException("Can't find User for ID "
+ aId);
}
return user;
}
public UserEntity add(UserEntity aUser) {
@ -34,6 +39,25 @@ public class UserDao {
return aUser;
}
public Boolean remove(Long aId) {
UserEntity user = em.find(UserEntity.class, aId);
if (user == null) {
throw new EntityNotFoundException("Can't find User for ID "
+ aId);
}
em.remove(user);
return true;
}
public UserEntity update(Long aId, UserEntity aUser){
UserEntity user = em.find(UserEntity.class, aId);
if (aUser == null) {
throw new EntityNotFoundException("Can't find User for ID "
+ aId);
}
em.merge(aUser);
return user;
}
}

View File

@ -25,7 +25,7 @@ public class UserEntity {
@Column(name = "id", updatable = false, nullable = false)
private Long id;
private String username;
private String address;
public Long getId() {
return id;
@ -35,6 +35,10 @@ public class UserEntity {
return username;
}
public String getAddress() {
return address;
}
public void setId(Long id) {
this.id = id;
}
@ -43,6 +47,10 @@ public class UserEntity {
this.username = username;
}
public void setAddress(String address) {
this.address = address;
}
}

View File

@ -4,8 +4,10 @@ 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;
@ -43,6 +45,25 @@ public class UserController {
return Response.status(201).entity(ret).build();
}
@DELETE
@Path("{id}")
@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();
}
@PUT
@Path("{id}")
@Consumes("application/json; charset=UTF-8")
@Produces("application/json; charset=UTF-8")
public Response updateUser(UserDto aUser, @PathParam("id") Long aId) {
UserEntity user = UserMapper.fromDto(aUser);
user.setId(aId);
UserDto ret = new UserDto(userDatabase.update(aId,user));
return Response.status(201).entity(ret).build();
}
}

View File

@ -5,7 +5,6 @@
*/
package pl.myboardgames;
/**
*
* @author Nao
@ -14,18 +13,27 @@ public class UserDto {
private Long id;
private String username;
private String address;
public UserDto() {
}
public UserDto(String username) {
this.username = username;
}
public UserDto(UserEntity aUser) {
this.username = aUser.getUsername();
this.id = aUser.getId();
username = aUser.getUsername();
id = aUser.getId();
}
public Long getId() {
return id;
}
public String getUsername() {
return username;
}
public String getAddress() {
return address;
}
public void setId(Long id) {
@ -36,13 +44,8 @@ public class UserDto {
this.username = username;
}
public Long getId() {
return id;
}
public String getUsername() {
return username;
public void setAddress(String address) {
this.address = address;
}
}

View File

@ -22,6 +22,15 @@ class UserMapper {
return ret;
}
public static UserEntity fromShortDto(UserShortDto aUser) {
UserEntity ret = new UserEntity();
ret.setUsername(aUser.getUsername());
ret.setId(aUser.getId());
return ret;
}

View File

@ -13,10 +13,12 @@ public class UserShortDto {
private Long id;
private String username;
private String address;
public UserShortDto(UserEntity aUser) {
id = aUser.getId();
username = aUser.getUsername();
address = aUser.getUsername();
}
public Long getId() {
@ -27,4 +29,10 @@ public class UserShortDto {
return username;
}
public String getAddress() {
return address;
}
}