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

solve the task #986

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/main/java/core/basesyntax/dao/MessageDetailsDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import core.basesyntax.model.MessageDetails;

public interface MessageDetailsDao {
MessageDetails create(MessageDetails entity);
MessageDetails create(MessageDetails messageDetails);

MessageDetails get(Long id);
}
63 changes: 58 additions & 5 deletions src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Comment> 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);
}
}
}
65 changes: 60 additions & 5 deletions src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Message> 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);
}
}
}
45 changes: 42 additions & 3 deletions src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
47 changes: 43 additions & 4 deletions src/main/java/core/basesyntax/dao/impl/SmileDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Smile> 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);
}
}
}
Loading
Loading