Skip to content

Commit

Permalink
Fixed issue throwing exception.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nhkhai committed Mar 23, 2024
1 parent 93c8cae commit a0ce106
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Customer, Long> {
// Custom query to find all customers with a certain ID.
List<Customer> findById(long id);
// List<Customer> findById(long id);

// Custom query to find all customers with a certain first name.
List<Customer> findByFirstName(String firstName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Product, Long> {
// Custom query to find all products with a certain ID.
List<Product> findById(long id);
// List<Product> findById(long id);

// Custom query to find all products with a certain category.
List<Product> findByCategory(String category);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Review, Long> {
// Custom query to find all reviews with a certain ID.
List<Review> findById(long id);
// List<Review> findById(long id);

// Custom query to find all reviews with a certain category.
List<Review> findByCategory(String category);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -28,16 +28,14 @@ public Customer createCustomer(Customer customer) {
}

@Override
public Customer getCustomer(long id) {
public Customer getCustomer(Long id) {
// Optional<Customer> 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
Expand All @@ -47,12 +45,10 @@ public ArrayList<Customer> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,16 +27,14 @@ public Product createProduct(Product product) {
}

@Override
public Product getProduct(long id) {
public Product getProduct(Long id) {
// Optional<Product> 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
Expand All @@ -46,12 +44,9 @@ public ArrayList<Product> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -27,16 +27,14 @@ public Review createReview(Review review) {
}

@Override
public Review getReview(long id) {
public Review getReview(Long id) {
// Optional<Review> 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
Expand All @@ -46,11 +44,9 @@ public ArrayList<Review> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
public interface CustomerService {
Customer createCustomer(Customer customer);

Customer getCustomer(long id);
Customer getCustomer(Long id);

ArrayList<Customer> getAllCustomers();

Customer updateCustomer(long id, Customer customer);
Customer updateCustomer(Long id, Customer customer);

void deleteCustomer(long id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
public interface ProductService {
Product createProduct(Product product);

Product getProduct(long id);
Product getProduct(Long id);

ArrayList<Product> getAllProducts();

Product updateProduct(long id, Product product);
Product updateProduct(Long id, Product product);

void deleteProduct(long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
public interface ReviewService {
Review createReview(Review review);

Review getReview(long id);
Review getReview(Long id);

ArrayList<Review> getAllReviews();

Review updateReview(long id, Review review);
Review updateReview(Long id, Review review);

void deleteReview(long id);

Expand Down

0 comments on commit a0ce106

Please sign in to comment.