Skip to content

Commit

Permalink
Merge pull request #22 from kiblykat/sis-41b
Browse files Browse the repository at this point in the history
SIS-41 completed finally.
  • Loading branch information
nhkhai authored Mar 29, 2024
2 parents b7ad79b + aac6df2 commit 0b1f3e3
Show file tree
Hide file tree
Showing 10 changed files with 516 additions and 410 deletions.
82 changes: 74 additions & 8 deletions ThunderClient/EndpointTests.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"client": "Thunder Client",
"collectionName": "SmartInventory",
"dateExported": "2024-03-29T06:44:41.033Z",
"dateExported": "2024-03-29T15:11:15.315Z",
"version": "1.1",
"folders": [
{
Expand All @@ -17,6 +17,13 @@
"containerId": "",
"created": "2024-03-29T06:21:13.542Z",
"sortNum": 20000
},
{
"_id": "0db7fba9-c06d-462f-a621-ce608d52ea78",
"name": "Review",
"containerId": "",
"created": "2024-03-29T12:28:07.289Z",
"sortNum": 30000
}
],
"requests": [
Expand Down Expand Up @@ -143,22 +150,27 @@
"colId": "e34b9201-d6f7-4614-a21c-21c245c1032e",
"containerId": "605122de-fb92-469e-ae99-ed47c68b3d96",
"name": "Add Review to Customer",
"url": "http://localhost:9090/customers/{id}/reviews",
"url": "http://localhost:9090/customers/{id}/products/{productId}/reviews",
"method": "POST",
"sortNum": 80000,
"created": "2024-03-29T06:19:27.794Z",
"modified": "2024-03-29T06:19:27.794Z",
"modified": "2024-03-29T15:08:54.672Z",
"headers": [],
"params": [
{
"name": "id",
"value": "1",
"isPath": true
},
{
"name": "productId",
"value": "5",
"isPath": true
}
],
"body": {
"type": "json",
"raw": "{\n \"category\": \"Books\",\n \"reviewContent\": \"Expected more from the ending, felt rushed.\",\n \"rating\": 3 ,\n \"productId\": 1 \n}",
"raw": "{\n \"category\": \"Books\",\n \"reviewContent\": \"Expected more from the ending, felt rushed.\",\n \"rating\": 4\n}",
"form": []
},
"tests": []
Expand Down Expand Up @@ -196,6 +208,20 @@
"params": [],
"tests": []
},
{
"_id": "2540b4c8-bce7-44d1-8a4e-b020e48cf099",
"colId": "e34b9201-d6f7-4614-a21c-21c245c1032e",
"containerId": "0db7fba9-c06d-462f-a621-ce608d52ea78",
"name": "Get All Reviews",
"url": "http://localhost:9090/reviews",
"method": "GET",
"sortNum": 100000,
"created": "2024-03-29T12:28:07.290Z",
"modified": "2024-03-29T12:31:37.627Z",
"headers": [],
"params": [],
"tests": []
},
{
"_id": "55a70043-6bf7-451d-b62b-8ac0a1b825f6",
"colId": "e34b9201-d6f7-4614-a21c-21c245c1032e",
Expand All @@ -205,12 +231,32 @@
"method": "GET",
"sortNum": 110000,
"created": "2024-03-29T06:27:57.526Z",
"modified": "2024-03-29T06:32:00.602Z",
"modified": "2024-03-29T09:29:04.154Z",
"headers": [],
"params": [
{
"name": "id",
"value": "7",
"value": "6",
"isPath": true
}
],
"tests": []
},
{
"_id": "20641995-f278-4f9a-9617-e551ed4b932d",
"colId": "e34b9201-d6f7-4614-a21c-21c245c1032e",
"containerId": "0db7fba9-c06d-462f-a621-ce608d52ea78",
"name": "Get 1 Review",
"url": "http://localhost:9090/reviews/{id}",
"method": "GET",
"sortNum": 110000,
"created": "2024-03-29T12:28:07.291Z",
"modified": "2024-03-29T14:09:12.966Z",
"headers": [],
"params": [
{
"name": "id",
"value": "2",
"isPath": true
}
],
Expand Down Expand Up @@ -265,12 +311,12 @@
"method": "PUT",
"sortNum": 130000,
"created": "2024-03-29T06:28:09.610Z",
"modified": "2024-03-29T06:38:03.934Z",
"modified": "2024-03-29T09:29:21.073Z",
"headers": [],
"params": [
{
"name": "id",
"value": "7",
"value": "6",
"isPath": true
}
],
Expand Down Expand Up @@ -300,6 +346,26 @@
}
],
"tests": []
},
{
"_id": "fb7039d9-56e5-4883-995c-d06a7eea82b0",
"colId": "e34b9201-d6f7-4614-a21c-21c245c1032e",
"containerId": "0db7fba9-c06d-462f-a621-ce608d52ea78",
"name": "Delete Product",
"url": "http://localhost:9090/reviews/{id}",
"method": "DELETE",
"sortNum": 140000,
"created": "2024-03-29T12:28:07.295Z",
"modified": "2024-03-29T12:38:39.979Z",
"headers": [],
"params": [
{
"name": "id",
"value": "9",
"isPath": true
}
],
"tests": []
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,71 +28,72 @@
@RestController
@RequestMapping("/customers")
public class CustomerController {
private CustomerService customerService;

// @Autowired
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}

// CREATE.
@PostMapping("")
public ResponseEntity<Customer> createCustomer(@Valid @RequestBody Customer customer) {
// if(bindingResult.hasErrors()) {
// return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
// }

Customer newCustomer = customerService.createCustomer(customer);
return new ResponseEntity<>(newCustomer, HttpStatus.CREATED);
}

@PostMapping("/{id}/reviews")
public ResponseEntity<Review> 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<ArrayList<Customer>> getAllCustomers() {
ArrayList<Customer> allCustomers = customerService.getAllCustomers();
return new ResponseEntity<>(allCustomers, HttpStatus.OK);
}

// READ (id)
@GetMapping("/{id}")
public ResponseEntity<Customer> getCustomer(@PathVariable long id) {
Customer foundCustomer = customerService.getCustomer(id);
return new ResponseEntity<>(foundCustomer, HttpStatus.OK);
}

// READ (by name CONTAINS)
@GetMapping("/search")
public ResponseEntity<ArrayList<Customer>> searchCustomer(@RequestParam String firstName) {
ArrayList<Customer> customers = customerService.searchCustomer(firstName);
return new ResponseEntity<>(customers, HttpStatus.OK);
}

// UPDATE
@PutMapping("/{id}")
public ResponseEntity<Customer> updateCustomer(@PathVariable long id, @RequestBody Customer customer) {
Customer updatedCustomer = customerService.updateCustomer(id, customer);
return new ResponseEntity<>(updatedCustomer, HttpStatus.OK);
}

// DELETE
@DeleteMapping("/{id}")
public ResponseEntity<HttpStatus> deleteCustomer(@PathVariable long id) {
customerService.deleteCustomer(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

// // 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);
private CustomerService customerService;

// @Autowired
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}

// CREATE.
@PostMapping("")
public ResponseEntity<Customer> createCustomer(@Valid @RequestBody Customer customer) {
// if(bindingResult.hasErrors()) {
// return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
// }

Customer newCustomer = customerService.createCustomer(customer);
return new ResponseEntity<>(newCustomer, HttpStatus.CREATED);
}

@PostMapping("/{id}/products/{productId}/reviews")
public ResponseEntity<Review> addReviewToCustomer(@PathVariable long id, @PathVariable long productId,
@RequestBody Review review) {
// TODO: process POST request
Review newReview = customerService.addReviewToCustomer(id, productId, review);
return new ResponseEntity<>(newReview, HttpStatus.OK);
}

// READ (all)
@GetMapping("")
public ResponseEntity<ArrayList<Customer>> getAllCustomers() {
ArrayList<Customer> allCustomers = customerService.getAllCustomers();
return new ResponseEntity<>(allCustomers, HttpStatus.OK);
}

// READ (id)
@GetMapping("/{id}")
public ResponseEntity<Customer> getCustomer(@PathVariable long id) {
Customer foundCustomer = customerService.getCustomer(id);
return new ResponseEntity<>(foundCustomer, HttpStatus.OK);
}

// READ (by name CONTAINS)
@GetMapping("/search")
public ResponseEntity<ArrayList<Customer>> searchCustomer(@RequestParam String firstName) {
ArrayList<Customer> customers = customerService.searchCustomer(firstName);
return new ResponseEntity<>(customers, HttpStatus.OK);
}

// UPDATE
@PutMapping("/{id}")
public ResponseEntity<Customer> updateCustomer(@PathVariable long id, @RequestBody Customer customer) {
Customer updatedCustomer = customerService.updateCustomer(id, customer);
return new ResponseEntity<>(updatedCustomer, HttpStatus.OK);
}

// DELETE
@DeleteMapping("/{id}")
public ResponseEntity<HttpStatus> deleteCustomer(@PathVariable long id) {
customerService.deleteCustomer(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

// // 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);
// }
}
21 changes: 19 additions & 2 deletions src/main/java/sg/com/smartinventory/entities/Customer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package sg.com.smartinventory.entities;

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

import jakarta.persistence.CascadeType;
Expand All @@ -12,7 +13,7 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;

import jakarta.persistence.Transient;
import jakarta.validation.constraints.Digits;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
Expand Down Expand Up @@ -61,9 +62,25 @@ public class Customer {
private String email;

@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)

private List<Review> reviews;

@Transient
private List<Product> reviewedProducts;

public List<Product> getReviewedProducts() {
if (this.reviews == null) {
return new ArrayList<>(); // return empty list if no reviews
}

if (reviewedProducts == null) {
reviewedProducts = new ArrayList<>();
for (Review review : this.reviews) {
reviewedProducts.add(review.getProduct());
}
}
return reviewedProducts;
}

public Customer() {
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/sg/com/smartinventory/entities/Product.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package sg.com.smartinventory.entities;

import java.util.List;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -59,6 +55,9 @@ public class Product {
// @JoinColumn(name = "product_id", referencedColumnName = "id")
// private List<Review> reviews;

// @OneToMany(mappedBy = "product", cascade = CascadeType.ALL)
// private List<Review> reviews;

public Product() {
}

Expand All @@ -74,4 +73,5 @@ public Product(String category, String name, String description, double price, i
}

// @ManyToOne Customer -> Many Products can be linked to 1 Customer

}
Loading

0 comments on commit 0b1f3e3

Please sign in to comment.