|
| 1 | +package com.iluwatar.cqrs.commandes; |
| 2 | + |
| 3 | +import org.hibernate.Query; |
| 4 | +import org.hibernate.Session; |
| 5 | +import org.hibernate.SessionFactory; |
| 6 | + |
| 7 | +import com.iluwatar.cqrs.domain.model.Author; |
| 8 | +import com.iluwatar.cqrs.domain.model.Book; |
| 9 | +import com.iluwatar.cqrs.util.HibernateUtil; |
| 10 | + |
| 11 | +public class CommandServiceImpl implements ICommandService { |
| 12 | + |
| 13 | + private SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); |
| 14 | + |
| 15 | + private Author getAuthorByUsername(String username) { |
| 16 | + Session session = sessionFactory.openSession(); |
| 17 | + Query query = session.createQuery("from Author where username=:username"); |
| 18 | + query.setParameter("username", username); |
| 19 | + Author author = (Author) query.uniqueResult(); |
| 20 | + session.close(); |
| 21 | + return author; |
| 22 | + } |
| 23 | + |
| 24 | + private Book getBookByTitle(String title) { |
| 25 | + Session session = sessionFactory.openSession(); |
| 26 | + Query query = session.createQuery("from Book where title=:title"); |
| 27 | + query.setParameter("title", title); |
| 28 | + Book book = (Book) query.uniqueResult(); |
| 29 | + session.close(); |
| 30 | + return book; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void authorCreated(String username, String name, String email) { |
| 35 | + Author author = new Author(username, name, email); |
| 36 | + Session session = sessionFactory.openSession(); |
| 37 | + session.beginTransaction(); |
| 38 | + session.save(author); |
| 39 | + session.getTransaction().commit(); |
| 40 | + session.close(); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void bookAddedToAuthor(String title, double price, String username) { |
| 45 | + Author author = getAuthorByUsername(username); |
| 46 | + Book book = new Book(title, price, author); |
| 47 | + Session session = sessionFactory.openSession(); |
| 48 | + session.beginTransaction(); |
| 49 | + session.save(book); |
| 50 | + session.getTransaction().commit(); |
| 51 | + session.close(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void authorNameUpdated(String username, String name) { |
| 56 | + Author author = getAuthorByUsername(username); |
| 57 | + author.setName(name); |
| 58 | + Session session = sessionFactory.openSession(); |
| 59 | + session.beginTransaction(); |
| 60 | + session.update(author); |
| 61 | + session.getTransaction().commit(); |
| 62 | + session.close(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void authorUsernameUpdated(String oldUsername, String newUsername) { |
| 67 | + Author author = getAuthorByUsername(oldUsername); |
| 68 | + author.setUsername(newUsername); |
| 69 | + Session session = sessionFactory.openSession(); |
| 70 | + session.beginTransaction(); |
| 71 | + session.update(author); |
| 72 | + session.getTransaction().commit(); |
| 73 | + session.close(); |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public void authorEmailUpdated(String username, String email) { |
| 79 | + Author author = getAuthorByUsername(username); |
| 80 | + author.setEmail(email); |
| 81 | + Session session = sessionFactory.openSession(); |
| 82 | + session.beginTransaction(); |
| 83 | + session.update(author); |
| 84 | + session.getTransaction().commit(); |
| 85 | + session.close(); |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void bookTitleUpdated(String oldTitle, String newTitle) { |
| 91 | + Book book = getBookByTitle(oldTitle); |
| 92 | + book.setTitle(newTitle); |
| 93 | + Session session = sessionFactory.openSession(); |
| 94 | + session.beginTransaction(); |
| 95 | + session.update(book); |
| 96 | + session.getTransaction().commit(); |
| 97 | + session.close(); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void bookPriceUpdated(String title, double price) { |
| 102 | + Book book = getBookByTitle(title); |
| 103 | + book.setPrice(price); |
| 104 | + Session session = sessionFactory.openSession(); |
| 105 | + session.beginTransaction(); |
| 106 | + session.update(book); |
| 107 | + session.getTransaction().commit(); |
| 108 | + session.close(); |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments