-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathRegisterProductCommandExecutor.java
More file actions
67 lines (56 loc) · 1.87 KB
/
Copy pathRegisterProductCommandExecutor.java
File metadata and controls
67 lines (56 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package commerce.commandmodel;
import java.math.BigDecimal;
import java.net.URI;
import java.time.LocalDateTime;
import java.util.UUID;
import java.util.function.Consumer;
import commerce.Product;
import commerce.command.RegisterProductCommand;
import static java.time.ZoneOffset.UTC;
public class RegisterProductCommandExecutor {
private final Consumer<Product> saveProduct;
public RegisterProductCommandExecutor(Consumer<Product> saveProduct) {
this.saveProduct = saveProduct;
}
public void execute(
UUID productId,
UUID sellerId,
RegisterProductCommand command
) {
validateCommand(command);
Product product = createProduct(productId, sellerId, command);
saveProduct(product);
}
private static void validateCommand(RegisterProductCommand command) {
if (command.priceAmount().compareTo(BigDecimal.ZERO) < 0) {
throw new InvalidCommandException();
}
}
private static boolean isValidUri(String value) {
try {
URI uri = URI.create(value);
return uri.getHost() != null;
} catch (IllegalArgumentException exception) {
return false;
}
}
private static Product createProduct(
UUID productId,
UUID sellerId,
RegisterProductCommand command
) {
var product = new Product();
product.setId(productId);
product.setSellerId(sellerId);
product.setName(command.name());
product.setImageUri(command.imageUri());
product.setDescription(command.description());
product.setPriceAmount(command.priceAmount());
product.setStockQuantity(command.stockQuantity());
product.setRegisteredTimeUtc(LocalDateTime.now(UTC));
return product;
}
private void saveProduct(Product product) {
saveProduct.accept(product);
}
}