|
1 | 1 | package com.example.demo.Product; |
2 | 2 |
|
3 | 3 | import com.example.demo.Product.Model.Product; |
| 4 | +import com.example.demo.Product.Model.UpdateProductCommand; |
| 5 | +import com.example.demo.Product.commandHandlers.CreateProductCommandHandler; |
| 6 | +import com.example.demo.Product.commandHandlers.DeleteProductCommandHandler; |
| 7 | +import com.example.demo.Product.commandHandlers.UpdateProductCommandHandler; |
4 | 8 | import com.example.demo.Product.queryHandlers.GetAllProductsQueryHandler; |
5 | 9 | import com.example.demo.Product.queryHandlers.GetProductQueryHandler; |
6 | 10 | import org.apache.coyote.Response; |
@@ -75,39 +79,67 @@ public ResponseEntity<Product> getProduct(@PathVariable Integer id) { |
75 | 79 | return getProductQueryHandler.execute(id); |
76 | 80 | } |
77 | 81 |
|
78 | | - // Lecture - 5: POST, PUT and DELETE MAPPING |
| 82 | +// // Lecture - 5: POST, PUT and DELETE MAPPING |
| 83 | +// @PostMapping |
| 84 | +// // @RequestBody will tell that in the HTTP request body look for a product. |
| 85 | +// public ResponseEntity createProduct(@RequestBody Product product) { |
| 86 | +// // Accept a product through JSON |
| 87 | +// // Convert it to a Product |
| 88 | +// // Save it in the database |
| 89 | +// productRepository.save(product); // Currently we do not have any data validation. |
| 90 | +// return ResponseEntity.ok().build(); // This wont return anything in the body for now. Only returns a 200 |
| 91 | +// // response code. |
| 92 | +// } |
| 93 | + |
| 94 | + // Lecture 9 : Command Handler |
| 95 | + @Autowired |
| 96 | + private CreateProductCommandHandler createProductCommandHandler; |
79 | 97 | @PostMapping |
80 | 98 | // @RequestBody will tell that in the HTTP request body look for a product. |
81 | | - public ResponseEntity createProduct(@RequestBody Product product) { |
82 | | - // Accept a product through JSON |
83 | | - // Convert it to a Product |
84 | | - // Save it in the database |
85 | | - productRepository.save(product); // Currently we do not have any data validation. |
86 | | - return ResponseEntity.ok().build(); // This wont return anything in the body for now. Only returns a 200 |
87 | | - // response code. |
| 99 | + public ResponseEntity<String> createProduct(@RequestBody Product product) { |
| 100 | + return createProductCommandHandler.execute(product); |
88 | 101 | } |
89 | 102 |
|
90 | 103 | // PUT is kind of a combination of GET AND POST. |
| 104 | +// @PutMapping("/{id}") |
| 105 | +// public ResponseEntity updateProduct(@PathVariable Integer id, @RequestBody Product product) { |
| 106 | +// // We would require the id to update the product |
| 107 | +// // Again we are not doing any validation here. |
| 108 | +// // We are doing this id thing manually just to see. We could have passed the ID |
| 109 | +// // in the product itself. |
| 110 | +// product.setId(id); // Helps set the id so that retrieval / update becomes easy. |
| 111 | +// productRepository.save(product); // updates the given product for the id set above. |
| 112 | +// return ResponseEntity.ok().build(); |
| 113 | +// } |
| 114 | + |
| 115 | + // Lecture 10 : Command Handler 2 |
| 116 | + @Autowired |
| 117 | + UpdateProductCommandHandler updateProductCommandHandler; |
91 | 118 | @PutMapping("/{id}") |
92 | 119 | public ResponseEntity updateProduct(@PathVariable Integer id, @RequestBody Product product) { |
93 | | - // We would require the id to update the product |
94 | | - // Again we are not doing any validation here. |
95 | | - // We are doing this id thing manually just to see. We could have passed the ID |
96 | | - // in the product itself. |
97 | | - product.setId(id); // Helps set the id so that retrieval / update becomes easy. |
98 | | - productRepository.save(product); // updates the given product for the id set above. |
99 | | - return ResponseEntity.ok().build(); |
| 120 | + UpdateProductCommand updateProductCommand = new UpdateProductCommand(id, product); |
| 121 | + return updateProductCommandHandler.execute(updateProductCommand); |
100 | 122 | } |
101 | 123 |
|
| 124 | +// @DeleteMapping("/{id}") |
| 125 | +// public ResponseEntity deleteProduct(@PathVariable Integer id) { |
| 126 | +// Product product = productRepository.findById(id).get(); |
| 127 | +// productRepository.delete(product); |
| 128 | +// |
| 129 | +// // OR.. |
| 130 | +// // productRepository.deleteById(id); |
| 131 | +// |
| 132 | +// return ResponseEntity.ok().build(); |
| 133 | +// } |
| 134 | + |
| 135 | + // Lecture 11 : Command Handler 3 |
| 136 | + @Autowired |
| 137 | + private DeleteProductCommandHandler deleteProductCommandHandler; |
102 | 138 | @DeleteMapping("/{id}") |
103 | 139 | public ResponseEntity deleteProduct(@PathVariable Integer id) { |
104 | | - Product product = productRepository.findById(id).get(); |
105 | | - productRepository.delete(product); |
106 | | - |
107 | | - // OR.. |
108 | | - // productRepository.deleteById(id); |
109 | | - |
110 | | - return ResponseEntity.ok().build(); |
| 140 | + // Not fully done yet as we have not handled the Optional (nullable) yet... in case we don't find the product with the given id. |
| 141 | + // It'd be done in further classes with custom exception... |
| 142 | + return deleteProductCommandHandler.execute(id); |
111 | 143 | } |
112 | 144 |
|
113 | 145 | } |
|
0 commit comments