From 6d42d40055251eaa5fa46489ed9650010b9e53c4 Mon Sep 17 00:00:00 2001 From: kiblykat Date: Sun, 24 Mar 2024 23:00:14 +0800 Subject: [PATCH] SIS-35: added /search and customer/{id}/reviews endpoints --- .../controllers/CustomerController.java | 18 ++++++++-- .../com/smartinventory/entities/Customer.java | 16 ++------- .../repositories/CustomerRepository.java | 2 +- .../serviceImpls/CustomerServiceImpl.java | 35 ++++++++++++++----- .../services/CustomerService.java | 5 +++ 5 files changed, 52 insertions(+), 24 deletions(-) diff --git a/src/main/java/sg/com/smartinventory/controllers/CustomerController.java b/src/main/java/sg/com/smartinventory/controllers/CustomerController.java index 19ed27c..3c0735c 100644 --- a/src/main/java/sg/com/smartinventory/controllers/CustomerController.java +++ b/src/main/java/sg/com/smartinventory/controllers/CustomerController.java @@ -15,6 +15,7 @@ import jakarta.validation.Valid; import sg.com.smartinventory.entities.Customer; +import sg.com.smartinventory.entities.Review; // import sg.com.smartinventory.entities.Review; import sg.com.smartinventory.services.CustomerService; @@ -45,6 +46,13 @@ public ResponseEntity createCustomer(@Valid @RequestBody Customer cust return new ResponseEntity<>(newCustomer, HttpStatus.CREATED); } + @PostMapping("/{id}/reviews") + public ResponseEntity addReviewToCustomer(@PathVariable long id, @RequestBody Review review) { + // TODO: process POST request + Review newReview = customerService.addReviewToCustomer(id, review); + return new ResponseEntity<>(newReview, HttpStatus.OK); + } + // READ (all) @GetMapping("") public ResponseEntity> getAllCustomers() { @@ -59,10 +67,16 @@ public ResponseEntity getCustomer(@PathVariable long id) { return new ResponseEntity<>(foundCustomer, HttpStatus.OK); } + // READ (by name CONTAINS) + @GetMapping("/search") + public ResponseEntity> searchCustomer(@RequestParam String firstName) { + ArrayList customers = customerService.searchCustomer(firstName); + return new ResponseEntity<>(customers, HttpStatus.OK); + } + // UPDATE @PutMapping("/{id}") public ResponseEntity updateCustomer(@PathVariable long id, @RequestBody Customer customer) { - // TODO: process PUT request Customer updatedCustomer = customerService.updateCustomer(id, customer); return new ResponseEntity<>(updatedCustomer, HttpStatus.OK); } @@ -71,7 +85,7 @@ public ResponseEntity updateCustomer(@PathVariable long id, @RequestBo @DeleteMapping("/{id}") public ResponseEntity deleteCustomer(@PathVariable long id) { customerService.deleteCustomer(id); - return new ResponseEntity<>(HttpStatus.NOT_FOUND); + return new ResponseEntity<>(HttpStatus.NO_CONTENT); } // // Nested route - Add review to customer. diff --git a/src/main/java/sg/com/smartinventory/entities/Customer.java b/src/main/java/sg/com/smartinventory/entities/Customer.java index 1e20a2b..4e1a5bb 100644 --- a/src/main/java/sg/com/smartinventory/entities/Customer.java +++ b/src/main/java/sg/com/smartinventory/entities/Customer.java @@ -60,25 +60,15 @@ public class Customer { @Column(name = "email") private String email; - // Uni-directional One to Many mapping -> One Customer (Parent) can have many - // Reviews (Child). The extra column 'customer_id' will be created on the many - // side of the relationship, that is, in the Review table. The cascade attribute - // is set to CascadeType.ALL to cascade all operations (e.g., save, update, - // delete) to the associated Review entities. The @JoinColumn annotation is used - // to specify the foreign key column (customer_id) in the Review table that - // establishes the relationship. Thus, it specifies the foreign key column in - // the child entity’s table that refers to the parent entity. The ‘mappedBy’ - // attribute is not used in a unidirectional relationship as it’s specific to - // bidirectional relationships. - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) - @JoinColumn(name = "customer_id", referencedColumnName = "id") + @OneToMany(mappedBy = "customer") private List reviews; public Customer() { } // Define Constructor for DataLoader. - public Customer(String firstName, String lastName, String country, String address, int postalCode, int phoneNumber, + public Customer(String firstName, String lastName, String country, String address, int postalCode, + int phoneNumber, String email) { this(); diff --git a/src/main/java/sg/com/smartinventory/repositories/CustomerRepository.java b/src/main/java/sg/com/smartinventory/repositories/CustomerRepository.java index 50c89f1..7c4a062 100644 --- a/src/main/java/sg/com/smartinventory/repositories/CustomerRepository.java +++ b/src/main/java/sg/com/smartinventory/repositories/CustomerRepository.java @@ -14,7 +14,7 @@ public interface CustomerRepository extends JpaRepository { // List findById(long id); // Custom query to find all customers with a certain first name. - List findByFirstName(String firstName); + List findByFirstNameContaining(String firstName); // Custom query to find all customers with a certain last name. List findByLastName(String lastName); diff --git a/src/main/java/sg/com/smartinventory/serviceImpls/CustomerServiceImpl.java b/src/main/java/sg/com/smartinventory/serviceImpls/CustomerServiceImpl.java index b6b2f15..9e5c903 100644 --- a/src/main/java/sg/com/smartinventory/serviceImpls/CustomerServiceImpl.java +++ b/src/main/java/sg/com/smartinventory/serviceImpls/CustomerServiceImpl.java @@ -6,35 +6,41 @@ import org.springframework.stereotype.Service; import sg.com.smartinventory.entities.Customer; +import sg.com.smartinventory.entities.Review; // import sg.com.smartinventory.entities.Review; import sg.com.smartinventory.exceptions.CustomerNotFoundException; import sg.com.smartinventory.repositories.CustomerRepository; +import sg.com.smartinventory.repositories.ReviewRepository; import sg.com.smartinventory.services.CustomerService; @Service public class CustomerServiceImpl implements CustomerService { private CustomerRepository customerRepository; + private ReviewRepository reviewRepository; // @Autowired - public CustomerServiceImpl(CustomerRepository customerRepository) { + public CustomerServiceImpl(CustomerRepository customerRepository, ReviewRepository reviewRepository) { this.customerRepository = customerRepository; + this.reviewRepository = reviewRepository; } + // - - - POST METHODS @Override public Customer createCustomer(Customer customer) { Customer newCustomer = customerRepository.save(customer); - return newCustomer; } + public Review addReviewToCustomer(long id, Review review) { + Customer customer = customerRepository.findById(id).orElseThrow(() -> new CustomerNotFoundException(id)); + review.setCustomer(customer); + Review newReview = reviewRepository.save(review); + return newReview; + } + + // - - - GET METHODS @Override 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)); } @@ -44,6 +50,14 @@ public ArrayList getAllCustomers() { return (ArrayList) allCustomers; } + // searches for first name that contains search term + @Override + public ArrayList searchCustomer(String firstName) { + List customers = customerRepository.findByFirstNameContaining(firstName); + return (ArrayList) customers; + } + + // - - - PUT METHODS @Override public Customer updateCustomer(Long id, Customer customer) { // Retrieve the customer from the database. @@ -63,8 +77,13 @@ public Customer updateCustomer(Long id, Customer customer) { return customerRepository.save(customerToUpdate); } + // - - - DELETE METHODS @Override public void deleteCustomer(long id) { + // findById method allows deleteCustomer to throw exception if no id of + // such type is avail to be deleted i.e response will be 404 (Expected) instead + // of 204, to tell client that resource not found + customerRepository.findById(id).orElseThrow(() -> new CustomerNotFoundException(id)); customerRepository.deleteById(id); } diff --git a/src/main/java/sg/com/smartinventory/services/CustomerService.java b/src/main/java/sg/com/smartinventory/services/CustomerService.java index 60d536c..59bff4a 100644 --- a/src/main/java/sg/com/smartinventory/services/CustomerService.java +++ b/src/main/java/sg/com/smartinventory/services/CustomerService.java @@ -4,12 +4,17 @@ import sg.com.smartinventory.entities.Customer; // import sg.com.smartinventory.entities.Review; +import sg.com.smartinventory.entities.Review; public interface CustomerService { Customer createCustomer(Customer customer); + Review addReviewToCustomer(long id, Review review); + Customer getCustomer(Long id); + ArrayList searchCustomer(String name); + ArrayList getAllCustomers(); Customer updateCustomer(Long id, Customer customer);