From 39b5e2829d50fc5d362ebd503cdd715bf1f263cb Mon Sep 17 00:00:00 2001 From: Evhen Timofeiev Date: Thu, 9 May 2024 20:37:49 +0300 Subject: [PATCH] solve the task --- .../basesyntax/dao/MessageDetailsDao.java | 2 +- .../basesyntax/dao/impl/CommentDaoImpl.java | 63 ++++++++++++++++-- .../basesyntax/dao/impl/MessageDaoImpl.java | 65 +++++++++++++++++-- .../dao/impl/MessageDetailsDaoImpl.java | 45 ++++++++++++- .../basesyntax/dao/impl/SmileDaoImpl.java | 47 ++++++++++++-- .../core/basesyntax/dao/impl/UserDaoImpl.java | 65 +++++++++++++++++-- .../java/core/basesyntax/model/Comment.java | 15 +++++ .../java/core/basesyntax/model/Message.java | 23 +++++++ .../core/basesyntax/model/MessageDetails.java | 18 +++++ .../java/core/basesyntax/model/Smile.java | 7 +- src/main/java/core/basesyntax/model/User.java | 22 +++++++ src/main/resources/hibernate.cfg.xml | 14 ++-- 12 files changed, 356 insertions(+), 30 deletions(-) diff --git a/src/main/java/core/basesyntax/dao/MessageDetailsDao.java b/src/main/java/core/basesyntax/dao/MessageDetailsDao.java index 556d63c9d..af78c2f9a 100644 --- a/src/main/java/core/basesyntax/dao/MessageDetailsDao.java +++ b/src/main/java/core/basesyntax/dao/MessageDetailsDao.java @@ -3,7 +3,7 @@ import core.basesyntax.model.MessageDetails; public interface MessageDetailsDao { - MessageDetails create(MessageDetails entity); + MessageDetails create(MessageDetails messageDetails); MessageDetails get(Long id); } diff --git a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java index 5176c951f..c485d131d 100644 --- a/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java @@ -3,30 +3,83 @@ import core.basesyntax.dao.CommentDao; import core.basesyntax.model.Comment; import java.util.List; +import org.hibernate.Session; import org.hibernate.SessionFactory; +import org.hibernate.Transaction; public class CommentDaoImpl extends AbstractDao implements CommentDao { + private Session session; + private Transaction transaction; + public CommentDaoImpl(SessionFactory sessionFactory) { super(sessionFactory); } @Override - public Comment create(Comment entity) { - return null; + public Comment create(Comment comment) { + checkComment(comment); + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(comment); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can`t add Comment to database! " + comment); + } finally { + if (session != null) { + session.close(); + } + } + return comment; } @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("Failed to retrieve " + + "Comment from the database with id " + id, e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createQuery("FROM Comment", Comment.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException("Failed to retrieve comments from the database", e); + } } @Override - public void remove(Comment entity) { + public void remove(Comment comment) { + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.remove(comment); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Cant remove Comment: " + comment); + } finally { + if (session != null) { + session.close(); + } + } + } + private void checkComment(Comment comment) { + if (comment == null) { + throw new RuntimeException("Comment cannot be null! "); + } + if (comment.getContent() == null || comment.getContent().isEmpty()) { + throw new RuntimeException("Comment content cannot be empty or null! " + comment); + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java index 9b899fa95..23b807edd 100644 --- a/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java @@ -3,30 +3,85 @@ 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 { + private Session session; + private Transaction transaction; + public MessageDaoImpl(SessionFactory sessionFactory) { super(sessionFactory); } @Override - public Message create(Message entity) { - return null; + public Message create(Message message) { + checkMessage(message); + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(message); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can`t add Message to database!" + message, e); + } finally { + if (session != null) { + session.close(); + } + } + return message; } @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("Failed to retrieve " + + "Message from the database with id " + id, e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createQuery("FROM Message", Message.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException("Failed to retrieve messages " + + "from the database", e); + } } @Override - public void remove(Message entity) { + public void remove(Message message) { + checkMessage(message); + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.remove(message); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Cant remove message!" + message, e); + } finally { + if (session != null) { + session.close(); + } + } + } + private void checkMessage(Message message) { + if (message == null) { + throw new RuntimeException("Message cannot be null! "); + } + if (message.getContent() == null || message.getContent().isEmpty()) { + throw new RuntimeException("Message cannot be empty or null! " + message); + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java index fb96d35aa..c20c15ed8 100644 --- a/src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java @@ -2,20 +2,59 @@ 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 { + private Session session; + private Transaction transaction; + public MessageDetailsDaoImpl(SessionFactory sessionFactory) { super(sessionFactory); } @Override - public MessageDetails create(MessageDetails entity) { - return null; + public MessageDetails create(MessageDetails messageDetails) { + checkMessageDetails(messageDetails); + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(messageDetails); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can`t add message details to database!" + + messageDetails, e); + } finally { + if (session != null) { + session.close(); + } + } + return messageDetails; } @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("Failed to retrieve " + + "message details from the database with id " + id, e); + } + } + + private void checkMessageDetails(MessageDetails messageDetails) { + if (messageDetails == null) { + throw new RuntimeException("Message details cannot be null! "); + } + if (messageDetails.getSender() == null + || messageDetails.getSender().isEmpty() + || messageDetails.getSentTime() == null) { + throw new RuntimeException("Message details cannot be empty or null! " + + messageDetails); + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java index 559248cda..e5a6531de 100644 --- a/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java @@ -3,25 +3,64 @@ 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 { + private Session session; + private Transaction transaction; + public SmileDaoImpl(SessionFactory sessionFactory) { super(sessionFactory); } @Override - public Smile create(Smile entity) { - return null; + public Smile create(Smile smile) { + checkSmile(smile); + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(smile); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can`t add Smile to database! " + smile); + } finally { + if (session != null) { + session.close(); + } + } + return smile; } @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("Failed to retrieve " + + "Smile from the database with id " + id, e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createQuery("FROM Smile", Smile.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException("Failed to retrieve Smiles from the database", e); + } + } + + private void checkSmile(Smile smile) { + if (smile == null) { + throw new RuntimeException("Smile cannot be null!"); + } + if (smile.getValue() == null || smile.getValue().isEmpty()) { + throw new RuntimeException("Smile value cannot be empty or null! " + smile); + } } } diff --git a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java index 338d8248b..b8fe151fb 100644 --- a/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java +++ b/src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java @@ -3,30 +3,85 @@ 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 { + private Session session; + private Transaction transaction; + public UserDaoImpl(SessionFactory sessionFactory) { super(sessionFactory); } @Override - public User create(User entity) { - return null; + public User create(User user) { + checkUser(user); + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.persist(user); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Can`t add User to database!" + user, e); + } finally { + if (session != null) { + session.close(); + } + } + return user; } @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("Failed to retrieve " + + "User from the database with id " + id, e); + } } @Override public List getAll() { - return null; + try (Session session = factory.openSession()) { + return session.createQuery("FROM User", User.class).getResultList(); + } catch (Exception e) { + throw new RuntimeException("Failed to retrieve users " + + "from the database", e); + } } @Override - public void remove(User entity) { + public void remove(User user) { + checkUser(user); + try { + session = factory.openSession(); + transaction = session.beginTransaction(); + session.remove(user); + transaction.commit(); + } catch (Exception e) { + if (transaction != null) { + transaction.rollback(); + } + throw new RuntimeException("Cant remove User!" + user, e); + } finally { + if (session != null) { + session.close(); + } + } + } + private void checkUser(User user) { + if (user == null) { + throw new RuntimeException("User cannot be null! "); + } + if (user.getUsername() == null || user.getUsername().isEmpty()) { + throw new RuntimeException("User name cannot be empty or null! " + user); + } } } diff --git a/src/main/java/core/basesyntax/model/Comment.java b/src/main/java/core/basesyntax/model/Comment.java index 9fba0c920..469d791de 100644 --- a/src/main/java/core/basesyntax/model/Comment.java +++ b/src/main/java/core/basesyntax/model/Comment.java @@ -1,15 +1,21 @@ package core.basesyntax.model; +import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +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 private List smiles; public Long getId() { @@ -35,4 +41,13 @@ public List getSmiles() { public void setSmiles(List smiles) { this.smiles = smiles; } + + @Override + public String toString() { + return "Comment{" + + "id=" + id + + ", content='" + content + '\'' + + ", smiles=" + smiles + + '}'; + } } diff --git a/src/main/java/core/basesyntax/model/Message.java b/src/main/java/core/basesyntax/model/Message.java index 672b5d485..f73a6ebd4 100644 --- a/src/main/java/core/basesyntax/model/Message.java +++ b/src/main/java/core/basesyntax/model/Message.java @@ -1,8 +1,21 @@ 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.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}) private MessageDetails messageDetails; public Long getId() { @@ -28,4 +41,14 @@ public MessageDetails getMessageDetails() { public void setMessageDetails(MessageDetails messageDetails) { this.messageDetails = messageDetails; } + + @Override + public String toString() { + return "Message{" + + "id=" + id + + ", content='" + content + '\'' + + ", messageDetails=" + messageDetails + + '}'; + } + } diff --git a/src/main/java/core/basesyntax/model/MessageDetails.java b/src/main/java/core/basesyntax/model/MessageDetails.java index 185023dd5..19b9d6d1b 100644 --- a/src/main/java/core/basesyntax/model/MessageDetails.java +++ b/src/main/java/core/basesyntax/model/MessageDetails.java @@ -1,8 +1,17 @@ package core.basesyntax.model; +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; private LocalDateTime sentTime; @@ -30,4 +39,13 @@ public LocalDateTime getSentTime() { public void setSentTime(LocalDateTime sentTime) { this.sentTime = sentTime; } + + @Override + public String toString() { + return "MessageDetails{" + + "id=" + id + + ", sender='" + sender + '\'' + + ", sentTime=" + sentTime + + '}'; + } } diff --git a/src/main/java/core/basesyntax/model/Smile.java b/src/main/java/core/basesyntax/model/Smile.java index 4395eb0f7..bd8e34d8b 100644 --- a/src/main/java/core/basesyntax/model/Smile.java +++ b/src/main/java/core/basesyntax/model/Smile.java @@ -4,9 +4,11 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.Table; @Entity -public class Smile { +@Table(name = "smiles") +public final class Smile { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @@ -39,7 +41,8 @@ public void setValue(String value) { public String toString() { return "Smile{" + "id=" + id - + ", value='" + value + '\'' + + ", value='" + value + + '\'' + '}'; } } diff --git a/src/main/java/core/basesyntax/model/User.java b/src/main/java/core/basesyntax/model/User.java index 0141eef00..c1955c097 100644 --- a/src/main/java/core/basesyntax/model/User.java +++ b/src/main/java/core/basesyntax/model/User.java @@ -1,10 +1,23 @@ package core.basesyntax.model; +import jakarta.persistence.CascadeType; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +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; private String username; + @OneToMany(cascade = CascadeType.PERSIST,fetch = FetchType.EAGER) private List comments; public Long getId() { @@ -30,4 +43,13 @@ public List getComments() { public void setComments(List comments) { this.comments = comments; } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", username='" + username + '\'' + + ", comments=" + comments + + '}'; + } } diff --git a/src/main/resources/hibernate.cfg.xml b/src/main/resources/hibernate.cfg.xml index cba4560a4..f5510542d 100644 --- a/src/main/resources/hibernate.cfg.xml +++ b/src/main/resources/hibernate.cfg.xml @@ -5,14 +5,18 @@ - org.hibernate.dialect.MySQL8Dialect - jdbc:mysql://localhost/cinema?serverTimezone=UTC + org.hibernate.dialect.MySQLDialect + jdbc:mysql://localhost:3306/cinema com.mysql.cj.jdbc.Driver - ma_admin - 1234 + root + timofeev true update - + + + + +