Skip to content

Commit

Permalink
hw is done, please check
Browse files Browse the repository at this point in the history
  • Loading branch information
Hero284 committed Oct 10, 2024
1 parent 7777544 commit bb7f879
Show file tree
Hide file tree
Showing 12 changed files with 339 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/main/java/core/basesyntax/HibernateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
private static SessionFactory sessionFactory = initSessionFactory();
private static final SessionFactory sessionFactory = initSessionFactory();

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

@Override
public Comment create(Comment entity) {
return null;
Transaction transaction = null;
try (Session session = factory.openSession()) {
transaction = session.beginTransaction();

if (entity.getSmiles() != null) {
for (Smile smile : entity.getSmiles()) {
Smile existingSmile = session.get(Smile.class, smile.getId());
if (existingSmile == null) {
throw new RuntimeException("Smile with id "
+ smile.getId()
+ " does not exist");
}
}
}

session.persist(entity);
transaction.commit();
return entity;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Failed to save comment: " + 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("Failed to get comment with id: " + id);
}
}

@Override
public List<Comment> getAll() {
return null;
try (Session session = factory.openSession()) {
return session.createQuery("FROM Comment", Comment.class).list();
} catch (Exception e) {
throw new RuntimeException("Failed to get all comments", e);
}
}

@Override
public void remove(Comment entity) {

Transaction transaction = null;
try (Session session = factory.openSession()) {
transaction = session.beginTransaction();
session.remove(entity);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Failed to remove comment: " + entity);
}
}
}
63 changes: 59 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,74 @@ 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();
return entity;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Failed to save message: " + entity);
} finally {
if (session != null) {
session.close();
}
}
}

@Override
public Message get(Long id) {
return null;
Session session = null;
try {
session = factory.openSession();
return session.get(Message.class, id);
} catch (Exception e) {
throw new RuntimeException("Failed to get message with id: " + id);
} finally {
if (session != null) {
session.close();
}
}
}

@Override
public List<Message> getAll() {
return null;
Session session = null;
try {
session = factory.openSession();
return session.createQuery("FROM Message", Message.class).list();
} catch (Exception e) {
throw new RuntimeException("Failed to get all messages", e);
} finally {
if (session != null) {
session.close();
}
}
}

@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("Failed to remove message: " + entity);
} finally {
if (session != null) {
session.close();
}
}
}
}
33 changes: 31 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,38 @@ 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();
return entity;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Failed to save message details: " + entity);
} finally {
if (session != null) {
session.close();
}
}
}

@Override
public MessageDetails get(Long id) {
return null;
Session session = null;
try {
session = factory.openSession();
return session.get(MessageDetails.class, id);
} catch (Exception e) {
throw new RuntimeException("Failed to get message details with id: " + id);
} finally {
if (session != null) {
session.close();
}
}
}
}
45 changes: 42 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,53 @@ 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();
return entity;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Failed to save smile: " + entity);
} finally {
if (session != null) {
session.close();
}
}
}

@Override
public Smile get(Long id) {
return null;
Session session = null;
try {
session = factory.openSession();
return session.get(Smile.class, id);
} catch (Exception e) {
throw new RuntimeException("Failed to get smile with id: " + id);
} finally {
if (session != null) {
session.close();
}
}
}

@Override
public List<Smile> getAll() {
return null;
Session session = null;
try {
session = factory.openSession();
return session.createQuery("FROM Smile", Smile.class).list();
} catch (Exception e) {
throw new RuntimeException("Failed to get all smiles", e);
} finally {
if (session != null) {
session.close();
}
}
}
}
63 changes: 59 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,74 @@ 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();
return entity;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Failed to save user: " + entity);
} finally {
if (session != null) {
session.close();
}
}
}

@Override
public User get(Long id) {
return null;
Session session = null;
try {
session = factory.openSession();
return session.get(User.class, id);
} catch (Exception e) {
throw new RuntimeException("Failed to get message with id: " + id);
} finally {
if (session != null) {
session.close();
}
}
}

@Override
public List<User> getAll() {
return null;
Session session = null;
try {
session = factory.openSession();
return session.createQuery("FROM User", User.class).list();
} catch (Exception e) {
throw new RuntimeException("Failed to get all users", e);
} finally {
if (session != null) {
session.close();
}
}
}

@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("Failed to remove user: " + entity);
} finally {
if (session != null) {
session.close();
}
}
}
}
Loading

0 comments on commit bb7f879

Please sign in to comment.