Skip to content

Commit

Permalink
Updated implementation for the Review entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
nhkhai committed Mar 21, 2024
1 parent 9af5dc2 commit 6eb1f4f
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
// import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -10,6 +11,7 @@
import jakarta.validation.Valid;

import sg.com.smartinventory.entities.Customer;
// import sg.com.smartinventory.entities.Review;
import sg.com.smartinventory.services.CustomerService;

@RestController
Expand All @@ -32,4 +34,12 @@ public ResponseEntity<Customer> createCustomer(@Valid @RequestBody Customer cust
Customer newCustomer = customerService.createCustomer(customer);
return new ResponseEntity<>(newCustomer, HttpStatus.CREATED);
}

// // Nested route - Add review to customer.
// @PostMapping("/{id}/reviews")
// public ResponseEntity<Review> addReviewToCustomer(@PathVariable Long id,
// @Valid @RequestBody Review review) {
// Review newReview = customerService.addReviewToCustomer(id, review);
// return new ResponseEntity<>(newReview, HttpStatus.CREATED);
// }
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package sg.com.smartinventory.controllers;

import java.util.ArrayList;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -31,4 +37,32 @@ public ResponseEntity<Review> createReview(@Valid @RequestBody Review review) {
Review newReview = reviewService.createReview(review);
return new ResponseEntity<>(newReview, HttpStatus.CREATED);
}

// READ (GET ALL).
@GetMapping("")
public ResponseEntity<ArrayList<Review>> getAllReviews() {
ArrayList<Review> allReviews = reviewService.getAllReviews();
return new ResponseEntity<>(allReviews, HttpStatus.OK);
}

// READ (GET ONE).
@GetMapping("/{id}")
public ResponseEntity<Review> getReview(@PathVariable long id) {
Review foundReview = reviewService.getReview(id);
return new ResponseEntity<>(foundReview, HttpStatus.OK);
}

// UPDATE.
@PutMapping("/{id}")
public ResponseEntity<Review> updateReview(@PathVariable long id, @RequestBody Review review) {
Review updatedReview = reviewService.updateReview(id, review);
return new ResponseEntity<>(updatedReview, HttpStatus.OK);
}

// DELETE.
@DeleteMapping("/{id}")
public ResponseEntity<Review> deleteReview(@PathVariable long id) {
reviewService.deleteReview(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.springframework.stereotype.Service;

import sg.com.smartinventory.entities.Customer;
// import sg.com.smartinventory.entities.Review;
// import sg.com.smartinventory.exceptions.CustomerNotFoundException;
import sg.com.smartinventory.repositories.CustomerRepository;
import sg.com.smartinventory.services.CustomerService;

Expand All @@ -21,4 +23,19 @@ public Customer createCustomer(Customer customer) {

return newCustomer;
}

// @Override
// public Review addReviewToCustomer(long customerId, long productId, Review
// review) {
// // retrieve the customer from the database.
// Customer selectedCustomer =
// customerRepository.findById(customerId).orElseThrow(() -> new
// CustomerNotFoundException(customerId));

// // add the customer to the review
// review.setCustomer(selectedCustomer);

// // save the review to the database
// return reviewRepository.save(review);
// }
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package sg.com.smartinventory.serviceImpls;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import sg.com.smartinventory.entities.Review;
// import sg.com.smartinventory.exceptions.ReviewNotFoundException;
import sg.com.smartinventory.repositories.ReviewRepository;
import sg.com.smartinventory.services.ReviewService;

Expand All @@ -21,4 +25,58 @@ public Review createReview(Review review) {

return newReview;
}

@Override
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);
}

@Override
public ArrayList<Review> getAllReviews() {
List<Review> allReviews = reviewRepository.findAll();
return (ArrayList<Review>) allReviews;
}

@Override
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);

// Update the review retrieved from the database.
reviewToUpdate.setCategory(review.getCategory());
reviewToUpdate.setReviewContent(review.getReviewContent());
reviewToUpdate.setRating(review.getRating());
reviewToUpdate.setCustomerId(review.getCustomerId());
reviewToUpdate.setProductId(review.getProductId());

// Save the updated review back to the database.
return reviewRepository.save(reviewToUpdate);
}

@Override
public void deleteReview(long id) {
reviewRepository.deleteById(id);
}

@Override
public ArrayList<Review> searchCustomerReviews(long customerId) {
List<Review> foundReviews = reviewRepository.findByCustomerId(customerId);
return (ArrayList<Review>) foundReviews;
}

@Override
public ArrayList<Review> searchProductReviews(long productId) {
List<Review> foundReviews = reviewRepository.findByProductId(productId);
return (ArrayList<Review>) foundReviews;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package sg.com.smartinventory.services;

import sg.com.smartinventory.entities.Customer;
// import sg.com.smartinventory.entities.Review;

public interface CustomerService {
Customer createCustomer(Customer customer);

// Review addReviewToCustomer(long customerId, long productId, Review review);
}
14 changes: 14 additions & 0 deletions src/main/java/sg/com/smartinventory/services/ReviewService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package sg.com.smartinventory.services;

import java.util.ArrayList;

import sg.com.smartinventory.entities.Review;

public interface ReviewService {
Review createReview(Review review);

Review getReview(long id);

ArrayList<Review> getAllReviews();

Review updateReview(long id, Review review);

void deleteReview(long id);

ArrayList<Review> searchCustomerReviews(long customerId);

ArrayList<Review> searchProductReviews(long productId);
}

0 comments on commit 6eb1f4f

Please sign in to comment.