-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lecture 9, 10 and 11 : Command Handlers
- Loading branch information
1 parent
0ed3f3a
commit edb6b26
Showing
7 changed files
with
199 additions
and
22 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
Initial_Projects/NoBS/src/main/java/com/example/demo/Command.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.example.demo; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
|
||
|
||
public interface Command <E, T> { | ||
// <E, T> = Entity, T is generic type in java | ||
|
||
ResponseEntity <T> execute(E entity); | ||
// we can also name the method handle.. since its a handler.. but execute is also used.. | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
Initial_Projects/NoBS/src/main/java/com/example/demo/Product/Model/UpdateProductCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.example.demo.Product.Model; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class UpdateProductCommand { | ||
private int id; | ||
private Product product; | ||
|
||
public UpdateProductCommand(int id, Product product) { | ||
this.id = id; | ||
this.product = product; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...S/src/main/java/com/example/demo/Product/commandHandlers/CreateProductCommandHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.example.demo.Product.commandHandlers; | ||
|
||
import com.example.demo.Command; | ||
import com.example.demo.Product.Model.Product; | ||
import com.example.demo.Product.ProductRepository; | ||
import io.micrometer.common.util.StringUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
// Lecture 9 : Command Handler | ||
@Service | ||
public class CreateProductCommandHandler implements Command<Product, String> { | ||
|
||
@Autowired // allows us to inject the product repository and access it in the command handler. | ||
private ProductRepository productRepository; | ||
@Override | ||
public ResponseEntity<String> execute(Product product) { | ||
// Validate then save... | ||
checkValidation(product); | ||
|
||
// Validation provides the safety for our app here... | ||
// The validation won't allow to save a wrong product and will throw an exception and stop execution. | ||
|
||
productRepository.save(product); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
private void checkValidation(Product product) { | ||
// name | ||
if (StringUtils.isBlank(product.getName())) { // micrometer utils... check import | ||
throw new RuntimeException("Product name cannot be empty"); | ||
} | ||
// description | ||
if(StringUtils.isBlank(product.getDescription())) { | ||
throw new RuntimeException("Description cannot be empty"); | ||
} | ||
// price | ||
if(product.getPrice() <= 0.0) { | ||
throw new RuntimeException("Price cannot be negative or 0"); | ||
} | ||
// quantity | ||
if(product.getQuantity() <= 0) { | ||
throw new RuntimeException("Quantity cannot be negative or 0"); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...S/src/main/java/com/example/demo/Product/commandHandlers/DeleteProductCommandHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.example.demo.Product.commandHandlers; | ||
|
||
import com.example.demo.Command; | ||
import com.example.demo.Product.Model.Product; | ||
import com.example.demo.Product.ProductRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class DeleteProductCommandHandler implements Command<Integer, ResponseEntity> { | ||
|
||
@Autowired | ||
private ProductRepository productRepository; | ||
@Override | ||
public ResponseEntity<ResponseEntity> execute(Integer id) { | ||
Product product = productRepository.getReferenceById(id); | ||
productRepository.delete(product); | ||
return ResponseEntity.ok().build(); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...S/src/main/java/com/example/demo/Product/commandHandlers/UpdateProductCommandHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.example.demo.Product.commandHandlers; | ||
|
||
import com.example.demo.Command; | ||
import com.example.demo.Product.Model.Product; | ||
import com.example.demo.Product.Model.UpdateProductCommand; | ||
import com.example.demo.Product.ProductRepository; | ||
import io.micrometer.common.util.StringUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class UpdateProductCommandHandler implements Command<UpdateProductCommand, ResponseEntity> { | ||
|
||
@Autowired | ||
private ProductRepository productRepository; | ||
@Override | ||
public ResponseEntity execute(UpdateProductCommand command) { | ||
Product product = command.getProduct(); | ||
// validate product | ||
checkValidation(product); | ||
product.setId(command.getId()); | ||
// If this is not done and the entry does not already exist in the database | ||
// then it will work just like POST command else it will create a new entry. | ||
productRepository.save(product); | ||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
// This method could be abstracted even more as it is being used in multiple classes... | ||
// by making something like another utility class | ||
private void checkValidation(Product product) { | ||
// name | ||
if (StringUtils.isBlank(product.getName())) { // micrometer utils... check import | ||
throw new RuntimeException("Product name cannot be empty"); | ||
} | ||
// description | ||
if(StringUtils.isBlank(product.getDescription())) { | ||
throw new RuntimeException("Description cannot be empty"); | ||
} | ||
// price | ||
if(product.getPrice() <= 0.0) { | ||
throw new RuntimeException("Price cannot be negative or 0"); | ||
} | ||
// quantity | ||
if(product.getQuantity() <= 0) { | ||
throw new RuntimeException("Quantity cannot be negative or 0"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters