Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

done #938

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

done #938

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/core/basesyntax/dao/CommentDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
import core.basesyntax.model.Comment;

public interface CommentDao extends GenericDao<Comment> {

}
56 changes: 52 additions & 4 deletions src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
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 {
public CommentDaoImpl(SessionFactory sessionFactory) {
Expand All @@ -12,21 +14,67 @@ public CommentDaoImpl(SessionFactory sessionFactory) {

@Override
public Comment create(Comment entity) {
return null;
Session session = null;
Transaction transaction = null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
session.persist(entity);
transaction.commit();
} catch (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't add comment to DataBase", e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public Comment get(Long id) {
return null;
Comment comment;
try (Session session = factory.openSession()) {
comment = session.get(Comment.class, id);
} catch (RuntimeException e) {
throw new RuntimeException("Can't get comment", e);
}
return comment;
}

@Override
public List<Comment> getAll() {
return null;
List<Comment> comments;
try (Session session = factory.openSession()) {
comments = session.createQuery("SELECT c FROM Comment c", Comment.class)
.getResultList();
} catch (RuntimeException e) {
throw new RuntimeException("Can't get all comments", e);
}
return comments;
}

@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 (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't remove comment from DataBase", e);
} finally {
if (session != null) {
session.close();
}
}
}
}
55 changes: 51 additions & 4 deletions src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -12,21 +14,66 @@ 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 (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't add message to DataBase", e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public Message get(Long id) {
return null;
Message message;
try (Session session = factory.openSession()) {
message = session.get(Message.class, id);
} catch (RuntimeException e) {
throw new RuntimeException("Can't get message", e);
}
return message;
}

@Override
public List<Message> getAll() {
return null;
List<Message> messages;
try (Session session = factory.openSession()) {
messages = session.createQuery("FROM Message", Message.class).getResultList();
} catch (RuntimeException e) {
throw new RuntimeException("Can't get all message", e);
}
return messages;
}

@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 (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't remove message from DataBase", e);
} finally {
if (session != null) {
session.close();
}
}
}
}
29 changes: 27 additions & 2 deletions src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't add messageDetails to DataBase", e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public MessageDetails get(Long id) {
return null;
MessageDetails messageDetails;
try (Session session = factory.openSession()) {
messageDetails = session.get(MessageDetails.class, id);
} catch (RuntimeException e) {
throw new RuntimeException("Can't get messageDetails", e);
}
return messageDetails;
}
}
37 changes: 34 additions & 3 deletions src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -12,16 +14,45 @@ 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 (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't add smile to DataBase", e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public Smile get(Long id) {
return null;
Smile smile;
try (Session session = factory.openSession()) {
smile = session.get(Smile.class, id);
} catch (RuntimeException e) {
throw new RuntimeException("Can't get smile", e);
}
return smile;
}

@Override
public List<Smile> getAll() {
return null;
List<Smile> smiles;
try (Session session = factory.openSession()) {
smiles = session.createQuery("FROM Smile", Smile.class).getResultList();
} catch (RuntimeException e) {
throw new RuntimeException("Can't get all smiles", e);
}
return smiles;
}
}
55 changes: 51 additions & 4 deletions src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -12,21 +14,66 @@ 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 (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't add User to DataBase", e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public User get(Long id) {
return null;
User user;
try (Session session = factory.openSession()) {
user = session.get(User.class, id);
} catch (RuntimeException e) {
throw new RuntimeException("Can't get user", e);
}
return user;
}

@Override
public List<User> getAll() {
return null;
List<User> users;
try (Session session = factory.openSession()) {
users = session.createQuery("FROM User", User.class).getResultList();
} catch (RuntimeException e) {
throw new RuntimeException("Can't get all user", e);
}
return users;
}

@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 (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't remove user from DataBase", e);
} finally {
if (session != null) {
session.close();
}
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/core/basesyntax/model/Comment.java
Original file line number Diff line number Diff line change
@@ -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<Smile> smiles;

public Long getId() {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/model/Message.java
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
Loading
Loading