From a0ce1066949123e667b74dc586a3ca1ca3572791 Mon Sep 17 00:00:00 2001 From: "nhkhai@gmail.com" Date: Sun, 24 Mar 2024 01:43:22 +0800 Subject: [PATCH] Fixed issue throwing exception. Removed overwritten findById(). FPA implementation takes in id of type ID, so for this case we have to use the type Long, which maps to the correct database type instead of primitive long. --- .../repositories/CustomerRepository.java | 2 +- .../repositories/ProductRepository.java | 2 +- .../repositories/ReviewRepository.java | 2 +- .../serviceImpls/CustomerServiceImpl.java | 16 ++++++---------- .../serviceImpls/ProductServiceImpl.java | 15 +++++---------- .../serviceImpls/ReviewServiceImpl.java | 14 +++++--------- .../smartinventory/services/CustomerService.java | 4 ++-- .../smartinventory/services/ProductService.java | 4 ++-- .../smartinventory/services/ReviewService.java | 4 ++-- 9 files changed, 25 insertions(+), 38 deletions(-) diff --git a/src/main/java/sg/com/smartinventory/repositories/CustomerRepository.java b/src/main/java/sg/com/smartinventory/repositories/CustomerRepository.java index 19a2d75..50c89f1 100644 --- a/src/main/java/sg/com/smartinventory/repositories/CustomerRepository.java +++ b/src/main/java/sg/com/smartinventory/repositories/CustomerRepository.java @@ -11,7 +11,7 @@ // We can also create custom queries using the JPA criteria API by simply creating a method with a certain naming convention specified by the query creation mechanism. public interface CustomerRepository extends JpaRepository { // Custom query to find all customers with a certain ID. - List findById(long id); + // List findById(long id); // Custom query to find all customers with a certain first name. List findByFirstName(String firstName); diff --git a/src/main/java/sg/com/smartinventory/repositories/ProductRepository.java b/src/main/java/sg/com/smartinventory/repositories/ProductRepository.java index ada6ddd..ce18b85 100644 --- a/src/main/java/sg/com/smartinventory/repositories/ProductRepository.java +++ b/src/main/java/sg/com/smartinventory/repositories/ProductRepository.java @@ -11,7 +11,7 @@ // We can also create custom queries using the JPA criteria API by simply creating a method with a certain naming convention specified by the query creation mechanism. public interface ProductRepository extends JpaRepository { // Custom query to find all products with a certain ID. - List findById(long id); + // List findById(long id); // Custom query to find all products with a certain category. List findByCategory(String category); diff --git a/src/main/java/sg/com/smartinventory/repositories/ReviewRepository.java b/src/main/java/sg/com/smartinventory/repositories/ReviewRepository.java index 6675f29..be12b8d 100644 --- a/src/main/java/sg/com/smartinventory/repositories/ReviewRepository.java +++ b/src/main/java/sg/com/smartinventory/repositories/ReviewRepository.java @@ -11,7 +11,7 @@ // We can also create custom queries using the JPA criteria API by simply creating a method with a certain naming convention specified by the query creation mechanism. public interface ReviewRepository extends JpaRepository { // Custom query to find all reviews with a certain ID. - List findById(long id); + // List findById(long id); // Custom query to find all reviews with a certain category. List findByCategory(String category); diff --git a/src/main/java/sg/com/smartinventory/serviceImpls/CustomerServiceImpl.java b/src/main/java/sg/com/smartinventory/serviceImpls/CustomerServiceImpl.java index f5e469d..b6b2f15 100644 --- a/src/main/java/sg/com/smartinventory/serviceImpls/CustomerServiceImpl.java +++ b/src/main/java/sg/com/smartinventory/serviceImpls/CustomerServiceImpl.java @@ -7,7 +7,7 @@ import sg.com.smartinventory.entities.Customer; // import sg.com.smartinventory.entities.Review; -// import sg.com.smartinventory.exceptions.CustomerNotFoundException; +import sg.com.smartinventory.exceptions.CustomerNotFoundException; import sg.com.smartinventory.repositories.CustomerRepository; import sg.com.smartinventory.services.CustomerService; @@ -28,16 +28,14 @@ public Customer createCustomer(Customer customer) { } @Override - public Customer getCustomer(long id) { + public Customer getCustomer(Long id) { // Optional optionalCustomer = customerRepository.findById(id); // if(optionalCustomer.isPresent()) { // Customer foundCustomer = optionalCustomer.get(); // return foundCustomer; // } // throw new CustomerNotFoundException(id); - // return customerRepository.findById(id).orElseThrow(() -> new - // CustomerNotFoundException(id)); - return customerRepository.findById(id).get(0); + return customerRepository.findById(id).orElseThrow(() -> new CustomerNotFoundException(id)); } @Override @@ -47,12 +45,10 @@ public ArrayList getAllCustomers() { } @Override - public Customer updateCustomer(long id, Customer customer) { + public Customer updateCustomer(Long id, Customer customer) { // Retrieve the customer from the database. - // Customer customerToUpdate = customerRepository.findById(id).orElseThrow(() -> - // new - // CustomerNotFoundException(id)); - Customer customerToUpdate = customerRepository.findById(id).get(0); + Customer customerToUpdate = customerRepository.findById(id) + .orElseThrow(() -> new CustomerNotFoundException(id)); // Update the customer retrieved from the database. customerToUpdate.setFirstName(customer.getFirstName()); diff --git a/src/main/java/sg/com/smartinventory/serviceImpls/ProductServiceImpl.java b/src/main/java/sg/com/smartinventory/serviceImpls/ProductServiceImpl.java index a18727f..c1abeeb 100644 --- a/src/main/java/sg/com/smartinventory/serviceImpls/ProductServiceImpl.java +++ b/src/main/java/sg/com/smartinventory/serviceImpls/ProductServiceImpl.java @@ -6,7 +6,7 @@ import org.springframework.stereotype.Service; import sg.com.smartinventory.entities.Product; -// import sg.com.smartinventory.exceptions.ProductNotFoundException; +import sg.com.smartinventory.exceptions.ProductNotFoundException; import sg.com.smartinventory.repositories.ProductRepository; import sg.com.smartinventory.services.ProductService; @@ -27,16 +27,14 @@ public Product createProduct(Product product) { } @Override - public Product getProduct(long id) { + public Product getProduct(Long id) { // Optional optionalProduct = productRepository.findById(id); // if(optionalProduct.isPresent()) { // Product foundProduct = optionalProduct.get(); // return foundProduct; // } // throw new ProductNotFoundException(id); - // return productRepository.findById(id).orElseThrow(() -> new - // ProductNotFoundException(id)); - return productRepository.findById(id).get(0); + return productRepository.findById(id).orElseThrow(() -> new ProductNotFoundException(id)); } @Override @@ -46,12 +44,9 @@ public ArrayList getAllProducts() { } @Override - public Product updateProduct(long id, Product product) { + public Product updateProduct(Long id, Product product) { // Retrieve the product from the database. - // Product productToUpdate = productRepository.findById(id).orElseThrow(() -> - // new - // ProductNotFoundException(id)); - Product productToUpdate = productRepository.findById(id).get(0); + Product productToUpdate = productRepository.findById(id).orElseThrow(() -> new ProductNotFoundException(id)); // Update the product retrieved from the database. productToUpdate.setCategory(product.getCategory()); diff --git a/src/main/java/sg/com/smartinventory/serviceImpls/ReviewServiceImpl.java b/src/main/java/sg/com/smartinventory/serviceImpls/ReviewServiceImpl.java index 29addfb..8c51149 100644 --- a/src/main/java/sg/com/smartinventory/serviceImpls/ReviewServiceImpl.java +++ b/src/main/java/sg/com/smartinventory/serviceImpls/ReviewServiceImpl.java @@ -6,7 +6,7 @@ import org.springframework.stereotype.Service; import sg.com.smartinventory.entities.Review; -// import sg.com.smartinventory.exceptions.ReviewNotFoundException; +import sg.com.smartinventory.exceptions.ReviewNotFoundException; import sg.com.smartinventory.repositories.ReviewRepository; import sg.com.smartinventory.services.ReviewService; @@ -27,16 +27,14 @@ public Review createReview(Review review) { } @Override - public Review getReview(long id) { + public Review getReview(Long id) { // Optional optionalReview = reviewRepository.findById(id); // if(optionalReview.isPresent()) { // Review foundReview = optionalReview.get(); // return foundReview; // } // throw new ReviewNotFoundException(id); - // return reviewRepository.findById(id).orElseThrow(() -> new - // ReviewNotFoundException(id)); - return reviewRepository.findById(id).get(0); + return reviewRepository.findById(id).orElseThrow(() -> new ReviewNotFoundException(id)); } @Override @@ -46,11 +44,9 @@ public ArrayList getAllReviews() { } @Override - public Review updateReview(long id, Review review) { + public Review updateReview(Long id, Review review) { // Retrieve the review from the database. - // Review reviewToUpdate = reviewRepository.findById(id).orElseThrow(() -> new - // ReviewNotFoundException(id)); - Review reviewToUpdate = reviewRepository.findById(id).get(0); + Review reviewToUpdate = reviewRepository.findById(id).orElseThrow(() -> new ReviewNotFoundException(id)); // Update the review retrieved from the database. reviewToUpdate.setCategory(review.getCategory()); diff --git a/src/main/java/sg/com/smartinventory/services/CustomerService.java b/src/main/java/sg/com/smartinventory/services/CustomerService.java index 38c7bae..60d536c 100644 --- a/src/main/java/sg/com/smartinventory/services/CustomerService.java +++ b/src/main/java/sg/com/smartinventory/services/CustomerService.java @@ -8,11 +8,11 @@ public interface CustomerService { Customer createCustomer(Customer customer); - Customer getCustomer(long id); + Customer getCustomer(Long id); ArrayList getAllCustomers(); - Customer updateCustomer(long id, Customer customer); + Customer updateCustomer(Long id, Customer customer); void deleteCustomer(long id); diff --git a/src/main/java/sg/com/smartinventory/services/ProductService.java b/src/main/java/sg/com/smartinventory/services/ProductService.java index ad915be..9db88ab 100644 --- a/src/main/java/sg/com/smartinventory/services/ProductService.java +++ b/src/main/java/sg/com/smartinventory/services/ProductService.java @@ -7,11 +7,11 @@ public interface ProductService { Product createProduct(Product product); - Product getProduct(long id); + Product getProduct(Long id); ArrayList getAllProducts(); - Product updateProduct(long id, Product product); + Product updateProduct(Long id, Product product); void deleteProduct(long id); } \ No newline at end of file diff --git a/src/main/java/sg/com/smartinventory/services/ReviewService.java b/src/main/java/sg/com/smartinventory/services/ReviewService.java index de37da9..3c34e09 100644 --- a/src/main/java/sg/com/smartinventory/services/ReviewService.java +++ b/src/main/java/sg/com/smartinventory/services/ReviewService.java @@ -7,11 +7,11 @@ public interface ReviewService { Review createReview(Review review); - Review getReview(long id); + Review getReview(Long id); ArrayList getAllReviews(); - Review updateReview(long id, Review review); + Review updateReview(Long id, Review review); void deleteReview(long id);