Skip to content

Commit

Permalink
That wasn`t so easy how i think)
Browse files Browse the repository at this point in the history
  • Loading branch information
geter8 committed Apr 12, 2024
1 parent 7777544 commit 7e193cd
Show file tree
Hide file tree
Showing 11 changed files with 347 additions and 17 deletions.
65 changes: 62 additions & 3 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,12 @@

import core.basesyntax.dao.CommentDao;
import core.basesyntax.model.Comment;
import jakarta.persistence.EntityNotFoundException;
import java.util.ArrayList;
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 +16,76 @@ 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 create to DB" + entity.toString(),e);
} finally {
if (session != null) {
session.close();

}
}
return entity;
}

@Override
public Comment get(Long id) {
return null;
try (Session session = factory.openSession()) {
Comment comment = session.get(Comment.class,id);
if (comment != null) {
return comment;
} else {
throw new EntityNotFoundException("Can`t find by id in Db " + id);
}
} catch (RuntimeException e) {
throw new RuntimeException("Can't get from DB comment with id = " + id, e);

}
}

@Override
public List<Comment> getAll() {
return null;
try (Session session = factory.openSession()) {
List<Comment> comments = session.createQuery("from Comment").list();
if (comments != null) {
return comments;
} else {
return new ArrayList<>();
}
} catch (RuntimeException e) {
throw new RuntimeException("Can`t get from DB all comments", 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 (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Cant remove from Db" + entity.toString(),e);

} finally {
if (session != null) {
session.close();
}
}

}
}
62 changes: 59 additions & 3 deletions src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import core.basesyntax.dao.MessageDao;
import core.basesyntax.model.Message;
import jakarta.persistence.EntityNotFoundException;
import java.util.ArrayList;
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 +16,73 @@ 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 create massage in to the DB",e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public Message get(Long id) {
return null;
try (Session session = factory.openSession()) {
Message message = session.get(Message.class,id);
if (message != null) {
return message;
} else {
throw new EntityNotFoundException("Can`t find by id" + id);
}
} catch (RuntimeException e) {
throw new RuntimeException("Can`t get from DB" + id,e);
}
}

@Override
public List<Message> getAll() {
return null;
try (Session session = factory.openSession()) {
List<Message> entities = session.createQuery("from Message").list();
if (entities != null) {
return entities;
} else {
return new ArrayList<>();
}
} catch (RuntimeException e) {
throw new RuntimeException("Can't get from DB all messages ", 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 (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can`t delete from DB" + entity.toString(),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 create to DB" + entity,e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public MessageDetails get(Long id) {
return null;
try (Session session = factory.openSession()) {
MessageDetails entity = session.get(MessageDetails.class, id);
return entity;

} catch (RuntimeException e) {
throw new RuntimeException("Can't get from DB messageDetails with id = " + id, e);
}
}
}
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 @@ -2,8 +2,12 @@

import core.basesyntax.dao.SmileDao;
import core.basesyntax.model.Smile;
import jakarta.persistence.EntityNotFoundException;
import java.util.ArrayList;
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 +16,51 @@ 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("Cant create on DB", e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public Smile get(Long id) {
return null;
try (Session session = factory.openSession()) {
Smile smile = session.get(Smile.class,id);
if (smile != null) {
return smile;
} else {
throw new EntityNotFoundException("Can`t found id in DB");
}
} catch (RuntimeException e) {
throw new RuntimeException("can`t get id in db" + id,e);
}
}

@Override
public List<Smile> getAll() {
return null;
try (Session session = factory.openSession()) {
List<Smile> smiles = session.createQuery("from Smile").list();
if (smiles != null) {
return smiles;
} else {
return new ArrayList<>();
}
} catch (RuntimeException e) {
throw new RuntimeException("Can't get from DB all smiles", e);
}
}
}
60 changes: 56 additions & 4 deletions src/main/java/core/basesyntax/dao/impl/UserDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import core.basesyntax.dao.UserDao;
import core.basesyntax.model.User;
import jakarta.persistence.EntityNotFoundException;
import java.util.ArrayList;
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 +16,69 @@ 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) {
throw new RuntimeException("Can`t create user" + entity,e);
} finally {
if (session != null) {
session.close();
}
}
return entity;
}

@Override
public User get(Long id) {
return null;
try (Session session = factory.openSession()) {
User user = session.get(User.class,id);
if (user != null) {
return user;
} else {
throw new EntityNotFoundException("Can`t find id in DB");
}
} catch (RuntimeException e) {
throw new RuntimeException("Can`t find id in DB" + id, e);
}
}

@Override
public List<User> getAll() {
return null;
try (Session session = factory.openSession()) {
List<User> users = session.createQuery("from User").list();
if (users != null) {
return users;
} else {
return new ArrayList<>();
}
} catch (RuntimeException e) {
throw new RuntimeException("Can't get from DB all users", 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 (RuntimeException e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't remove from DB user = " + entity.toString(), e);
} finally {
if (session != null) {
session.close();
}
}
}
}
Loading

0 comments on commit 7e193cd

Please sign in to comment.