Skip to content

Commit

Permalink
project is ready for the code review
Browse files Browse the repository at this point in the history
  • Loading branch information
misterSokor committed Jun 6, 2024
1 parent 7777544 commit 6d50e2f
Show file tree
Hide file tree
Showing 17 changed files with 486 additions and 28 deletions.
Empty file added logs/app.log
Empty file.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
</properties>

<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.23.1</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package core.basesyntax;

import core.basesyntax.dao.MessageDao;
import core.basesyntax.dao.UserDao;
import core.basesyntax.dao.impl.MessageDaoImpl;
import core.basesyntax.dao.impl.SmileDaoImpl;
import core.basesyntax.dao.impl.UserDaoImpl;
import core.basesyntax.model.Comment;
import core.basesyntax.model.Message;
import core.basesyntax.model.MessageDetails;
import core.basesyntax.model.Smile;
import core.basesyntax.model.User;
import java.time.LocalDateTime;
import java.util.List;
import org.hibernate.SessionFactory;

public class Main {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
SmileDaoImpl smileDao = new SmileDaoImpl(sessionFactory);

Smile smile = new Smile();
smile.setValue("happy :)");
Comment comment = new Comment();
comment.setContent("This is a comment!");
List<Smile> smiles = List.of(smile);
for (Smile oneSmile : smiles) {
oneSmile.setComment(comment);
}
comment.setSmiles(smiles);

UserDao userDao = new UserDaoImpl(sessionFactory);
User user = new User();
user.setUsername("John");
user.setComments(List.of(comment));
userDao.create(user);

MessageDetails messageDetails = new MessageDetails();
messageDetails.setSender(user.getUsername());
messageDetails.setSentTime(LocalDateTime.now());

MessageDao messageDao = new MessageDaoImpl(sessionFactory);
Message message = new Message();
message.setContent("Stay calm!");
message.setMessageDetails(messageDetails);
List<Message> allMessages = messageDao.getAll();
for (Message text : allMessages) {
System.out.println(text);
}
messageDao.create(message);

User userFromDb = userDao.get(5L);
userDao.remove(userFromDb);
}
}
1 change: 0 additions & 1 deletion src/main/java/core/basesyntax/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
import core.basesyntax.model.User;

public interface UserDao extends GenericDao<User> {

}
74 changes: 68 additions & 6 deletions src/main/java/core/basesyntax/dao/impl/CommentDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,93 @@

import core.basesyntax.dao.CommentDao;
import core.basesyntax.model.Comment;
import core.basesyntax.model.Smile;
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 {
private final SmileDaoImpl smileDao = new SmileDaoImpl(
factory.openSession().getSessionFactory());

public CommentDaoImpl(SessionFactory sessionFactory) {
super(sessionFactory);
}

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

List<Smile> smiles = new ArrayList<>();
for (Smile smile : comment.getSmiles()) {
Smile smileInDb = smileDao.get(smile.getId());
if (smileInDb == null) {
throw new RuntimeException("Smile entity does not exist in the database");
} else {
Smile managedSmile = (Smile) session.merge(smileInDb);
smiles.add(managedSmile);
}
}
comment.setSmiles(smiles);
session.persist(comment);

transaction.commit();
return comment;
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't insert Comment ", e);
} finally {
if (session != null) {
session.close();
}
}
}

@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("Can't get Comment by id: " + id, e);
}
}

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

@Override
public void remove(Comment entity) {

public void remove(Comment comment) {
Session session = null;
Transaction transaction = null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
session.remove(comment);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't delete Comment ", e);
} finally {
if (session != null) {
session.close();
}
}
}
}
52 changes: 48 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,63 @@ 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("Can't insert Message entity", 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 RuntimeException("Can't get Message entity by id: " + id, e);
}
}

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

@Override
public void remove(Message entity) {

Session session = null;
Transaction transaction = null;
try {
session = factory.openSession();
transaction = session.beginTransaction();
session.delete(entity);
transaction.commit();
} catch (Exception e) {
if (transaction != null) {
transaction.rollback();
}
throw new RuntimeException("Can't delete Message entity", e);
} finally {
if (session != null) {
session.close();
}
}
}
}
27 changes: 25 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,32 @@ 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("Can't insert MessageDetails entity", 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 RuntimeException("Can't get MessageDetails entity by id: " + id, e);
}
}
}
40 changes: 37 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,48 @@ public SmileDaoImpl(SessionFactory sessionFactory) {

@Override
public Smile create(Smile entity) {
return null;
if (entity == null) {
throw new IllegalArgumentException("Entity cannot be 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("Can't insert Smile entity", e);
} finally {
if (session != null) {
session.close();
}
}
}

@Override
public Smile get(Long id) {
return null;
try (Session session = factory.openSession()) {
if (id == null) {
throw new IllegalArgumentException("Id cannot be null");
}
return session.get(Smile.class, id);
} catch (Exception e) {
throw new RuntimeException("Can't get Smile entity by id: " + id, e);
}
}

@Override
public List<Smile> getAll() {
return null;
String query = "SELECT * FROM smiles";
try (Session session = factory.openSession()) {
return session.createNativeQuery(query, Smile.class).getResultList();
} catch (Exception e) {
throw new RuntimeException("Can't get all Smile entities", e);
}
}
}
Loading

0 comments on commit 6d50e2f

Please sign in to comment.