fix(module-web,module-ejb): add suppport to get and add user from database

This commit is contained in:
Damian Pierzchalski 2018-12-14 22:27:12 +01:00
parent c707b84ab5
commit 10e636d034
4 changed files with 37 additions and 15 deletions

View File

@ -1,38 +1,36 @@
package pl.myboardgames;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
public class UserDao {
private Set<UserEntity> users;
@PersistenceContext
EntityManager em;
public UserDao() {
users = new HashSet<>();
UserEntity user = new UserEntity();
user.setUsername("test");
user.setId(new Long(0));
users.add(user);
}
public Set<UserEntity>getAll() {
Query query = em.createQuery("SELECT u FROM UserEntity u", UserEntity.class);
Set<UserEntity> users = new HashSet<UserEntity>(query.getResultList());
return users;
}
public UserEntity getById(Long aId) {
return users.stream().filter(user -> user.getId().equals(aId)).findFirst().get();
return em.find(UserEntity.class, aId);
}
public UserEntity add(UserEntity aUser) {
aUser.setId(Long.valueOf(users.size()+1));
users.add(aUser);
em.persist(aUser);
return aUser;
}

View File

@ -5,9 +5,11 @@
*/
package pl.myboardgames;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
@ -15,10 +17,12 @@ import javax.persistence.Id;
*/
@Entity
@Table( name = "users" )
public class UserEntity {
@Id
@GeneratedValue
@Column(name = "id", updatable = false, nullable = false)
private Long id;
private String username;

View File

@ -5,7 +5,6 @@
*/
package pl.myboardgames;
import java.util.List;
/**
*
@ -15,16 +14,29 @@ public class UserDto {
private Long id;
private String username;
public UserDto() {
}
public UserDto(String username) {
this.username = username;
}
public UserDto(UserEntity aUser) {
this.username = aUser.getUsername();
this.id = aUser.getId();
}
public void setId(Long id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public Long getId() {
return id;
}

View File

@ -19,4 +19,12 @@ public class UserShortDto {
username = aUser.getUsername();
}
public Long getId() {
return id;
}
public String getUsername() {
return username;
}
}