From 10e636d0346aa2a5002afcdc04bf92a266ab0504 Mon Sep 17 00:00:00 2001 From: Damian Pierzchalski Date: Fri, 14 Dec 2018 22:27:12 +0100 Subject: [PATCH] fix(module-web,module-ejb): add suppport to get and add user from database --- .../main/java/pl/myboardgames/UserDao.java | 22 +++++++++---------- .../main/java/pl/myboardgames/UserEntity.java | 4 ++++ .../main/java/pl/myboardgames/UserDto.java | 18 ++++++++++++--- .../java/pl/myboardgames/UserShortDto.java | 8 +++++++ 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/module-ejb/src/main/java/pl/myboardgames/UserDao.java b/module-ejb/src/main/java/pl/myboardgames/UserDao.java index 397c0f6..961dace 100644 --- a/module-ejb/src/main/java/pl/myboardgames/UserDao.java +++ b/module-ejb/src/main/java/pl/myboardgames/UserDao.java @@ -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 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 SetgetAll() { + Query query = em.createQuery("SELECT u FROM UserEntity u", UserEntity.class); + Set users = new HashSet(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; } diff --git a/module-ejb/src/main/java/pl/myboardgames/UserEntity.java b/module-ejb/src/main/java/pl/myboardgames/UserEntity.java index 394262d..484f743 100644 --- a/module-ejb/src/main/java/pl/myboardgames/UserEntity.java +++ b/module-ejb/src/main/java/pl/myboardgames/UserEntity.java @@ -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; diff --git a/module-web/src/main/java/pl/myboardgames/UserDto.java b/module-web/src/main/java/pl/myboardgames/UserDto.java index 3053c9a..8ff5452 100644 --- a/module-web/src/main/java/pl/myboardgames/UserDto.java +++ b/module-web/src/main/java/pl/myboardgames/UserDto.java @@ -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; } diff --git a/module-web/src/main/java/pl/myboardgames/UserShortDto.java b/module-web/src/main/java/pl/myboardgames/UserShortDto.java index 9401e94..6361a06 100644 --- a/module-web/src/main/java/pl/myboardgames/UserShortDto.java +++ b/module-web/src/main/java/pl/myboardgames/UserShortDto.java @@ -19,4 +19,12 @@ public class UserShortDto { username = aUser.getUsername(); } + public Long getId() { + return id; + } + + public String getUsername() { + return username; + } + }