From 0831718c5da61f34e4406a966a75cc0fd33a99bb Mon Sep 17 00:00:00 2001 From: Yuliia Date: Wed, 24 Jul 2024 18:19:02 +0300 Subject: [PATCH] implemented DAO methods --- .../basesyntax/dao/impl/CommentDaoImpl.java | 83 ++++++++++++++++++- .../basesyntax/dao/impl/MessageDaoImpl.java | 53 +++++++++++- .../dao/impl/MessageDetailsDaoImpl.java | 29 ++++++- .../basesyntax/dao/impl/SmileDaoImpl.java | 35 +++++++- .../core/basesyntax/dao/impl/UserDaoImpl.java | 54 +++++++++++- .../java/core/basesyntax/model/Comment.java | 17 ++++ .../java/core/basesyntax/model/Message.java | 23 +++++ .../core/basesyntax/model/MessageDetails.java | 20 +++++ .../java/core/basesyntax/model/Smile.java | 2 + src/main/java/core/basesyntax/model/User.java | 26 ++++++ 10 files changed, 325 insertions(+), 17 deletions(-) diff --git a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java index 5176c951f..1feae1113 100644 --- a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java @@ -2,8 +2,11 @@ import core.basesyntax.dao.CommentDao; import core.basesyntax.model.Comment; +import core.basesyntax.model.Smile; import java.util.List; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; public class CommentDaoImpl extends AbstractDao implements CommentDao { public CommentDaoImpl(SessionFactory sessionFactory) { @@ -12,21 +15,93 @@ public CommentDaoImpl(SessionFactory sessionFactory) { @Override public Comment create(Comment entity) { - return null; + isEntityNull(entity); + areSmilesPersisted(entity); + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't save comment to DB; comment: " + + entity, e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; + } + + private void isEntityNull(Comment entity) { + if (entity == null) { + throw new IllegalArgumentException("You can't save null comment."); + } + } + + private void areSmilesPersisted(Comment entity) { + List smiles = entity.getSmiles(); + if (smiles != null) { + for (Smile smile : smiles) { + if (smile != null && smile.getId() == null) { + throw new IllegalArgumentException("Can't save comment to DB if the smile " + + "is not stored in DB. " + entity); + } + } + } + } + + private void isCommentNull(Comment entity) { + if (entity == null) { + throw new RuntimeException("Comment is null: " + entity); + } } @Override public Comment get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(Comment.class, id); + } catch (Exception e) { + throw new RuntimeException("Can't get comment by id: " + + id, e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createQuery("from Comment", Comment.class) + .list(); + } catch (Exception e) { + throw new RuntimeException("Can't get list of all comments from DB", + e); + } } @Override public void remove(Comment entity) { - + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.remove(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't remove comment from DB: " + + entity, e); + } finally { + if (session != null) { + session.close(); + } + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java index 9b899fa95..e384991d4 100644 --- a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java @@ -3,7 +3,9 @@ import core.basesyntax.dao.MessageDao; import core.basesyntax.model.Message; import java.util.List; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; public class MessageDaoImpl extends AbstractDao implements MessageDao { public MessageDaoImpl(SessionFactory sessionFactory) { @@ -12,21 +14,64 @@ public MessageDaoImpl(SessionFactory sessionFactory) { @Override public Message create(Message entity) { - return null; + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't add message to DB; message: " + + entity, e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @Override public Message get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(Message.class, id); + } catch (Exception e) { + throw new RuntimeException("Can't get message by id: " + + id, e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createQuery("from Message", Message.class).list(); + } catch (Exception e) { + throw new RuntimeException("Can't get list of all messages from DB", e); + } } @Override public void remove(Message entity) { - + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.remove(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't remove message from DB: ", e); + } finally { + if (session != null) { + session.close(); + } + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java index fb96d35aa..7933ceeb6 100644 --- a/src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java @@ -2,7 +2,9 @@ import core.basesyntax.dao.MessageDetailsDao; import core.basesyntax.model.MessageDetails; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; public class MessageDetailsDaoImpl extends AbstractDao implements MessageDetailsDao { public MessageDetailsDaoImpl(SessionFactory sessionFactory) { @@ -11,11 +13,34 @@ public MessageDetailsDaoImpl(SessionFactory sessionFactory) { @Override public MessageDetails create(MessageDetails entity) { - return null; + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't add Message Details to DB; Message Details: " + + entity, e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @Override public MessageDetails get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(MessageDetails.class, id); + } catch (Exception e) { + throw new RuntimeException("Can't get Message Details by id: " + + id, e); + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java index 559248cda..8ce9f3c92 100644 --- a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java @@ -3,7 +3,9 @@ import core.basesyntax.dao.SmileDao; import core.basesyntax.model.Smile; import java.util.List; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; public class SmileDaoImpl extends AbstractDao implements SmileDao { public SmileDaoImpl(SessionFactory sessionFactory) { @@ -12,16 +14,43 @@ public SmileDaoImpl(SessionFactory sessionFactory) { @Override public Smile create(Smile entity) { - return null; + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't add Smile to DB; Smile: " + + entity, e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @Override public Smile get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(Smile.class, id); + } catch (Exception e) { + throw new RuntimeException("Can't get Smile by id: " + + id, e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createQuery("from Smile", Smile.class).list(); + } catch (Exception e) { + throw new RuntimeException("Can't get list of all Smiles from DB", e); + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java index 338d8248b..3940b84e9 100644 --- a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java @@ -3,7 +3,9 @@ import core.basesyntax.dao.UserDao; import core.basesyntax.model.User; import java.util.List; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; public class UserDaoImpl extends AbstractDao implements UserDao { public UserDaoImpl(SessionFactory sessionFactory) { @@ -12,21 +14,65 @@ public UserDaoImpl(SessionFactory sessionFactory) { @Override public User create(User entity) { - return null; + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't add User to DB; User: " + + entity, e); + } finally { + if (session != null) { + session.close(); + } + } + return entity; } @Override public User get(Long id) { - return null; + try (Session session = factory.openSession()) { + return session.get(User.class, id); + } catch (Exception e) { + throw new RuntimeException("Can't get User by id: " + + id, e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createQuery("from User", User.class).list(); + } catch (Exception e) { + throw new RuntimeException("Can't get list of all Users from DB", e); + } } @Override public void remove(User entity) { - + Session session = null; + Transaction transaction = null; + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.remove(entity); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can't remove User from DB: " + + entity, e); + } finally { + if (session != null) { + session.close(); + } + } } } diff --git a/src/main/java/core/basesyntax/model/Comment.java b/src/main/java/core/basesyntax/model/Comment.java index 9fba0c920..0cebea836 100644 --- a/src/main/java/core/basesyntax/model/Comment.java +++ b/src/main/java/core/basesyntax/model/Comment.java @@ -1,15 +1,23 @@ package core.basesyntax.model; +import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; import java.util.List; +@Entity +@Table(name = "comments") public class Comment { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String content; + @OneToMany + @JoinColumn(name = "comment_id") private List smiles; public Long getId() { @@ -35,4 +43,13 @@ public List getSmiles() { public void setSmiles(List smiles) { this.smiles = smiles; } + + @Override + public String toString() { + return "Comment { " + + "id: " + id + + "content: " + content + + "list of smiles: " + smiles + '\'' + + "} "; + } } diff --git a/src/main/java/core/basesyntax/model/Message.java b/src/main/java/core/basesyntax/model/Message.java index 672b5d485..de7abb900 100644 --- a/src/main/java/core/basesyntax/model/Message.java +++ b/src/main/java/core/basesyntax/model/Message.java @@ -1,8 +1,22 @@ package core.basesyntax.model; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToOne; +import jakarta.persistence.Table; + +@Entity +@Table(name = "messages") public class Message { + @Id@GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String content; + @OneToOne(cascade = {CascadeType.PERSIST, CascadeType.REMOVE}) + @JoinColumn(name = "message_details_id") private MessageDetails messageDetails; public Long getId() { @@ -28,4 +42,13 @@ public MessageDetails getMessageDetails() { public void setMessageDetails(MessageDetails messageDetails) { this.messageDetails = messageDetails; } + + @Override + public String toString() { + return "Message { " + + "id: " + id + + "content: " + content + + "message details: " + messageDetails + '\'' + + "} "; + } } diff --git a/src/main/java/core/basesyntax/model/MessageDetails.java b/src/main/java/core/basesyntax/model/MessageDetails.java index 185023dd5..1085c23e2 100644 --- a/src/main/java/core/basesyntax/model/MessageDetails.java +++ b/src/main/java/core/basesyntax/model/MessageDetails.java @@ -1,10 +1,21 @@ package core.basesyntax.model; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Table; import java.time.LocalDateTime; +@Entity +@Table(name = "message_details") public class MessageDetails { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String sender; + @Column(name = "sent_time") private LocalDateTime sentTime; public Long getId() { @@ -30,4 +41,13 @@ public LocalDateTime getSentTime() { public void setSentTime(LocalDateTime sentTime) { this.sentTime = sentTime; } + + @Override + public String toString() { + return "Message details { " + + "id: " + id + + "sender: " + sender + + "sent time: " + sentTime + '\'' + + "} "; + } } diff --git a/src/main/java/core/basesyntax/model/Smile.java b/src/main/java/core/basesyntax/model/Smile.java index 4395eb0f7..9218c0f8d 100644 --- a/src/main/java/core/basesyntax/model/Smile.java +++ b/src/main/java/core/basesyntax/model/Smile.java @@ -4,8 +4,10 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.Table; @Entity +@Table(name = "smiles") public class Smile { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) diff --git a/src/main/java/core/basesyntax/model/User.java b/src/main/java/core/basesyntax/model/User.java index 0141eef00..7a52ab557 100644 --- a/src/main/java/core/basesyntax/model/User.java +++ b/src/main/java/core/basesyntax/model/User.java @@ -1,10 +1,27 @@ package core.basesyntax.model; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; import java.util.List; +@Entity +@Table(name = "users") public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @Column(name = "user_name") private String username; + @OneToMany(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER) + @JoinColumn(name = "user_id") private List comments; public Long getId() { @@ -30,4 +47,13 @@ public List getComments() { public void setComments(List comments) { this.comments = comments; } + + @Override + public String toString() { + return "User {" + + "id=" + id + + ", username='" + username + + "comments: " + comments + '\'' + + '}'; + } }