Skip to content

Commit

Permalink
DataProcessingException added, repo modified
Browse files Browse the repository at this point in the history
  • Loading branch information
ChabVlad committed Sep 6, 2024
1 parent 463a515 commit f189a30
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/books")
@RequestMapping("/books")
public class BookController {
private final BookService bookService;
private final BookMapper bookMapper;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package project.bookstore.exception;

public class DataProcessingException extends RuntimeException {
public DataProcessingException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ public class EntityNotFoundException extends RuntimeException {
public EntityNotFoundException(String message, Throwable cause) {
super(message, cause);
}

public EntityNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package project.bookstore.repository;

import java.util.List;
import java.util.Optional;
import project.bookstore.model.Book;

public interface BookRepository {
Book save(Book book);

List<Book> findAll();

Book getBookById(Long id);
Optional<Book> findBookById(Long id);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package project.bookstore.repository.impl;

import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import java.util.List;
import java.util.Optional;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;
import project.bookstore.exception.EntityNotFoundException;
import project.bookstore.exception.DataProcessingException;
import project.bookstore.model.Book;
import project.bookstore.repository.BookRepository;

Expand Down Expand Up @@ -41,25 +39,20 @@ public Book save(Book book) {
@Override
public List<Book> findAll() {
try (Session session = sessionFactory.openSession()) {
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Book> criteriaQuery = criteriaBuilder.createQuery(Book.class);
Root<Book> root = criteriaQuery.from(Book.class);
return session.createQuery(criteriaQuery).getResultList();
return session.createQuery("FROM Book", Book.class).getResultList();
} catch (Exception e) {
throw new EntityNotFoundException("Can't found books ", e);
throw new DataProcessingException("Can't found books ", e);
}
}

@Override
public Book getBookById(Long id) {
public Optional<Book> findBookById(Long id) {
try (Session session = sessionFactory.openSession()) {
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
CriteriaQuery<Book> criteriaQuery = criteriaBuilder.createQuery(Book.class);
Root<Book> root = criteriaQuery.from(Book.class);
criteriaQuery.where(criteriaBuilder.equal(root.get("id"), id));
return session.createQuery(criteriaQuery).getSingleResult();
return Optional.ofNullable(session.createQuery(
"FROM Book WHERE id = :id", Book.class)
.setParameter("id", id).uniqueResult());
} catch (Exception e) {
throw new EntityNotFoundException("Can't found book by id: " + id, e);
throw new DataProcessingException("Can't found book by id: " + id, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.stereotype.Service;
import project.bookstore.dto.BookDto;
import project.bookstore.dto.CreateBookRequestDto;
import project.bookstore.exception.EntityNotFoundException;
import project.bookstore.mapper.BookMapper;
import project.bookstore.model.Book;
import project.bookstore.repository.BookRepository;
Expand All @@ -29,6 +30,7 @@ public List<BookDto> findAll() {

@Override
public BookDto getBookById(Long id) {
return bookMapper.toDto(bookRepository.getBookById(id));
return bookMapper.toDto(bookRepository.findBookById(id).orElseThrow(
() -> new EntityNotFoundException("Book not found by id: " + id)));
}
}

0 comments on commit f189a30

Please sign in to comment.