add ICommandService and IQueriesService Implementations
This commit is contained in:
@ -0,0 +1,111 @@
|
||||
package com.iluwatar.cqrs.commandes;
|
||||
|
||||
import org.hibernate.Query;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
import com.iluwatar.cqrs.domain.model.Author;
|
||||
import com.iluwatar.cqrs.domain.model.Book;
|
||||
import com.iluwatar.cqrs.util.HibernateUtil;
|
||||
|
||||
public class CommandServiceImpl implements ICommandService {
|
||||
|
||||
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
|
||||
|
||||
private Author getAuthorByUsername(String username) {
|
||||
Session session = sessionFactory.openSession();
|
||||
Query query = session.createQuery("from Author where username=:username");
|
||||
query.setParameter("username", username);
|
||||
Author author = (Author) query.uniqueResult();
|
||||
session.close();
|
||||
return author;
|
||||
}
|
||||
|
||||
private Book getBookByTitle(String title) {
|
||||
Session session = sessionFactory.openSession();
|
||||
Query query = session.createQuery("from Book where title=:title");
|
||||
query.setParameter("title", title);
|
||||
Book book = (Book) query.uniqueResult();
|
||||
session.close();
|
||||
return book;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void authorCreated(String username, String name, String email) {
|
||||
Author author = new Author(username, name, email);
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
session.save(author);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bookAddedToAuthor(String title, double price, String username) {
|
||||
Author author = getAuthorByUsername(username);
|
||||
Book book = new Book(title, price, author);
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
session.save(book);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void authorNameUpdated(String username, String name) {
|
||||
Author author = getAuthorByUsername(username);
|
||||
author.setName(name);
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
session.update(author);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void authorUsernameUpdated(String oldUsername, String newUsername) {
|
||||
Author author = getAuthorByUsername(oldUsername);
|
||||
author.setUsername(newUsername);
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
session.update(author);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void authorEmailUpdated(String username, String email) {
|
||||
Author author = getAuthorByUsername(username);
|
||||
author.setEmail(email);
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
session.update(author);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bookTitleUpdated(String oldTitle, String newTitle) {
|
||||
Book book = getBookByTitle(oldTitle);
|
||||
book.setTitle(newTitle);
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
session.update(book);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bookPriceUpdated(String title, double price) {
|
||||
Book book = getBookByTitle(title);
|
||||
book.setPrice(price);
|
||||
Session session = sessionFactory.openSession();
|
||||
session.beginTransaction();
|
||||
session.update(book);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package com.iluwatar.cqrs.queries;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.SQLQuery;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.transform.Transformers;
|
||||
|
||||
import com.iluwatar.cqrs.dto.AuthorDTO;
|
||||
import com.iluwatar.cqrs.dto.BookDTO;
|
||||
import com.iluwatar.cqrs.util.HibernateUtil;
|
||||
|
||||
public class QueryServiceImpl implements IQueryService {
|
||||
|
||||
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
|
||||
|
||||
@Override
|
||||
public AuthorDTO getAuthorByUsername(String username) {
|
||||
Session session = sessionFactory.openSession();
|
||||
SQLQuery sqlQuery = session
|
||||
.createSQLQuery("SELECT a.username as \"username\", a.name as \"name\", a.email as \"email\""
|
||||
+ "FROM Author a where a.username=:username");
|
||||
sqlQuery.setParameter("username", username);
|
||||
AuthorDTO authorDTO = (AuthorDTO) sqlQuery.setResultTransformer(Transformers.aliasToBean(AuthorDTO.class))
|
||||
.uniqueResult();
|
||||
session.close();
|
||||
return authorDTO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BookDTO getBook(String title) {
|
||||
Session session = sessionFactory.openSession();
|
||||
SQLQuery sqlQuery = session
|
||||
.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\"" + " FROM Book b where b.title=:title");
|
||||
sqlQuery.setParameter("title", title);
|
||||
BookDTO bookDTO = (BookDTO) sqlQuery.setResultTransformer(Transformers.aliasToBean(BookDTO.class)).uniqueResult();
|
||||
session.close();
|
||||
return bookDTO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<BookDTO> getAuthorBooks(String username) {
|
||||
Session session = sessionFactory.openSession();
|
||||
SQLQuery sqlQuery = session.createSQLQuery("SELECT b.title as \"title\", b.price as \"price\""
|
||||
+ " FROM Author a , Book b where b.author_id = a.id and a.username=:username");
|
||||
sqlQuery.setParameter("username", username);
|
||||
List<BookDTO> bookDTOs = sqlQuery.setResultTransformer(Transformers.aliasToBean(BookDTO.class)).list();
|
||||
session.close();
|
||||
return bookDTOs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BigInteger getAuthorBooksCount(String username) {
|
||||
Session session = sessionFactory.openSession();
|
||||
SQLQuery sqlQuery = session.createSQLQuery(
|
||||
"SELECT count(b.title)" + " FROM Book b, Author a where b.author_id = a.id and a.username=:username");
|
||||
sqlQuery.setParameter("username", username);
|
||||
BigInteger bookcount = (BigInteger) sqlQuery.uniqueResult();
|
||||
session.close();
|
||||
return bookcount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BigInteger getAuthorsCount() {
|
||||
Session session = sessionFactory.openSession();
|
||||
SQLQuery sqlQuery = session.createSQLQuery("SELECT count(id) from Author");
|
||||
BigInteger authorcount = (BigInteger) sqlQuery.uniqueResult();
|
||||
session.close();
|
||||
return authorcount;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user