generated from mate-academy/jv-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7777544
commit 0f879c7
Showing
12 changed files
with
271 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 47 additions & 6 deletions
53
src/main/java/core/basesyntax/dao/impl/MessageDaoImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,73 @@ | ||
package core.basesyntax.dao.impl; | ||
|
||
import core.basesyntax.dao.MessageDao; | ||
import core.basesyntax.exception.DataProcessingException; | ||
import core.basesyntax.model.Message; | ||
import java.util.List; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.Transaction; | ||
import org.hibernate.query.Query; | ||
|
||
public class MessageDaoImpl extends AbstractDao implements MessageDao { | ||
public MessageDaoImpl(SessionFactory sessionFactory) { | ||
super(sessionFactory); | ||
} | ||
|
||
@Override | ||
public Message create(Message entity) { | ||
return null; | ||
public Message create(Message message) { | ||
Transaction transaction = null; | ||
Session session = null; | ||
try { | ||
session = factory.openSession(); | ||
transaction = session.beginTransaction(); | ||
session.persist(message); | ||
transaction.commit(); | ||
return message; | ||
} catch (Exception e) { | ||
if (transaction != null) { | ||
transaction.rollback(); | ||
} | ||
throw new DataProcessingException("Can't write new message", e); | ||
} finally { | ||
if (session != null) { | ||
session.close(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Message get(Long id) { | ||
return null; | ||
try (Session session = factory.openSession()) { | ||
return session.get(Message.class, id); | ||
} catch (Exception e) { | ||
throw new DataProcessingException("Can't get message by id: " + id, e); | ||
} | ||
} | ||
|
||
@Override | ||
public List<Message> getAll() { | ||
return null; | ||
try (Session session = factory.openSession()) { | ||
String hql = "FROM Message"; | ||
Query<Message> fromComment = session.createQuery(hql, Message.class); | ||
return fromComment.getResultList(); | ||
} catch (Exception e) { | ||
throw new DataProcessingException("Can't get messages ", e); | ||
} | ||
} | ||
|
||
@Override | ||
public void remove(Message entity) { | ||
|
||
public void remove(Message message) { | ||
Transaction transaction = null; | ||
try (Session session = factory.openSession()) { | ||
transaction = session.beginTransaction(); | ||
session.remove(message); | ||
transaction.commit(); | ||
} catch (Exception e) { | ||
if (transaction != null) { | ||
transaction.rollback(); | ||
} | ||
throw new DataProcessingException("Can't remove message, everyone will see that=)", e); | ||
} | ||
} | ||
} |
30 changes: 27 additions & 3 deletions
30
src/main/java/core/basesyntax/dao/impl/MessageDetailsDaoImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,45 @@ | ||
package core.basesyntax.dao.impl; | ||
|
||
import core.basesyntax.dao.MessageDetailsDao; | ||
import core.basesyntax.exception.DataProcessingException; | ||
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) { | ||
super(sessionFactory); | ||
} | ||
|
||
@Override | ||
public MessageDetails create(MessageDetails entity) { | ||
return null; | ||
public MessageDetails create(MessageDetails messageDetails) { | ||
Session session = null; | ||
Transaction transaction = null; | ||
try { | ||
session = factory.openSession(); | ||
transaction = session.beginTransaction(); | ||
session.persist(messageDetails); | ||
transaction.commit(); | ||
return messageDetails; | ||
} catch (Exception e) { | ||
if (transaction != null) { | ||
transaction.rollback(); | ||
} | ||
throw new DataProcessingException("Can`t create new messageDetails", e); | ||
} finally { | ||
if (session != null) { | ||
session.close(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public MessageDetails get(Long id) { | ||
return null; | ||
try (Session session = factory.openSession()) { | ||
return session.get(MessageDetails.class, id); | ||
} catch (Exception e) { | ||
throw new DataProcessingException("Can`t get smile with this ID: " + id); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,58 @@ | ||
package core.basesyntax.dao.impl; | ||
|
||
import core.basesyntax.dao.SmileDao; | ||
import core.basesyntax.exception.DataProcessingException; | ||
import core.basesyntax.model.Smile; | ||
import java.util.List; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.Transaction; | ||
import org.hibernate.query.Query; | ||
|
||
public class SmileDaoImpl extends AbstractDao implements SmileDao { | ||
public SmileDaoImpl(SessionFactory sessionFactory) { | ||
super(sessionFactory); | ||
} | ||
|
||
@Override | ||
public Smile create(Smile entity) { | ||
return null; | ||
public Smile create(Smile smile) { | ||
Session session = null; | ||
Transaction transaction = null; | ||
try { | ||
session = factory.openSession(); | ||
transaction = session.beginTransaction(); | ||
session.persist(smile); | ||
transaction.commit(); | ||
return smile; | ||
} catch (Exception e) { | ||
if (transaction != null) { | ||
transaction.rollback(); | ||
} | ||
throw new DataProcessingException("Can`t create new smile"); | ||
} finally { | ||
if (session != null) { | ||
session.close(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Smile get(Long id) { | ||
return null; | ||
try (Session session = factory.openSession()) { | ||
return session.get(Smile.class, id); | ||
} catch (Exception e) { | ||
throw new DataProcessingException("Can`t get smile with this ID: " + id); | ||
} | ||
} | ||
|
||
@Override | ||
public List<Smile> getAll() { | ||
return null; | ||
try (Session session = factory.openSession()) { | ||
String hql = "FROM Smile"; | ||
Query<Smile> fromSmile = session.createQuery(hql, Smile.class); | ||
return fromSmile.getResultList(); | ||
} catch (Exception e) { | ||
throw new DataProcessingException("Can`t get smiles", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,77 @@ | ||
package core.basesyntax.dao.impl; | ||
|
||
import core.basesyntax.dao.UserDao; | ||
import core.basesyntax.exception.DataProcessingException; | ||
import core.basesyntax.model.User; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import org.hibernate.Session; | ||
import org.hibernate.SessionFactory; | ||
import org.hibernate.Transaction; | ||
import org.hibernate.query.Query; | ||
|
||
public class UserDaoImpl extends AbstractDao implements UserDao { | ||
public UserDaoImpl(SessionFactory sessionFactory) { | ||
super(sessionFactory); | ||
} | ||
|
||
@Override | ||
public User create(User entity) { | ||
return null; | ||
public User create(User user) { | ||
Transaction transaction = null; | ||
Session session = null; | ||
try { | ||
session = factory.openSession(); | ||
transaction = session.beginTransaction(); | ||
session.persist(user); | ||
transaction.commit(); | ||
return user; | ||
} catch (Exception e) { | ||
if (transaction != null) { | ||
transaction.rollback(); | ||
} | ||
throw new DataProcessingException("Can't create new user", e); | ||
} finally { | ||
if (session != null) { | ||
session.close(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public User get(Long id) { | ||
return null; | ||
try (Session session = factory.openSession()) { | ||
return session.get(User.class, id); | ||
} catch (Exception e) { | ||
throw new DataProcessingException("Can't get message by id: " + id, e); | ||
} | ||
} | ||
|
||
@Override | ||
public List<User> getAll() { | ||
return null; | ||
try (Session session = factory.openSession()) { | ||
String hql = "FROM Comment"; | ||
Query<User> fromUser = session.createQuery(hql, User.class); | ||
if (fromUser != null) { | ||
return fromUser.getResultList(); | ||
} | ||
return new ArrayList<>(); | ||
} catch (Exception e) { | ||
throw new DataProcessingException("Can't get comments ", e); | ||
} | ||
} | ||
|
||
@Override | ||
public void remove(User entity) { | ||
|
||
public void remove(User user) { | ||
Transaction transaction = null; | ||
try (Session session = factory.openSession()) { | ||
transaction = session.beginTransaction(); | ||
session.remove(user); | ||
transaction.commit(); | ||
} catch (Exception e) { | ||
if (transaction != null) { | ||
transaction.rollback(); | ||
} | ||
throw new DataProcessingException("Can't remove user", e); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/core/basesyntax/exception/DataProcessingException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package core.basesyntax.exception; | ||
|
||
public class DataProcessingException extends RuntimeException { | ||
public DataProcessingException(String message) { | ||
super(message); | ||
} | ||
|
||
public DataProcessingException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
Oops, something went wrong.