Skip to content

Commit a99c2c8

Browse files
committed
BAEL-2275: Add OrderItem
1 parent 75ff212 commit a99c2c8

15 files changed

Lines changed: 127 additions & 73 deletions

File tree

ddd/src/main/java/com/baeldung/ddd/layers/DomainLayerApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import org.springframework.context.annotation.PropertySource;
66

77
@SpringBootApplication
8-
@PropertySource(value={"classpath:ddd-layers.properties"})
8+
@PropertySource(value = { "classpath:ddd-layers.properties" })
99
public class DomainLayerApplication {
10-
public static void main(String[] args) {
10+
public static void main(final String[] args) {
1111
SpringApplication.run(DomainLayerApplication.class, args);
1212
}
1313
}

ddd/src/main/java/com/baeldung/ddd/layers/application/OrderController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.springframework.http.MediaType;
1010
import org.springframework.web.bind.annotation.*;
1111

12+
import java.util.UUID;
13+
1214
@RestController
1315
@RequestMapping("/orders")
1416
public class OrderController {
@@ -33,8 +35,8 @@ void addProduct(@PathVariable final ObjectId id, @RequestBody final AddProductRe
3335
}
3436

3537
@DeleteMapping(value = "/{id}/products", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
36-
void deleteProduct(@PathVariable final ObjectId id, @RequestParam final String name) {
37-
orderService.deleteProduct(id, name);
38+
void deleteProduct(@PathVariable final ObjectId id, @RequestParam final UUID productId) {
39+
orderService.deleteProduct(id, productId);
3840
}
3941

4042
@PostMapping("/{id}/complete")

ddd/src/main/java/com/baeldung/ddd/layers/application/request/CreateOrderRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class CreateOrderRequest {
1010
@NotNull private Product product;
1111

1212
@JsonCreator
13-
public CreateOrderRequest(@JsonProperty("product") @NotNull Product product) {
13+
public CreateOrderRequest(@JsonProperty("product") @NotNull final Product product) {
1414
this.product = product;
1515
}
1616

ddd/src/main/java/com/baeldung/ddd/layers/application/response/CreateOrderResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
public class CreateOrderResponse {
66
private final String id;
77

8-
public CreateOrderResponse(String id) {
8+
public CreateOrderResponse(final String id) {
99
this.id = id;
1010
}
1111

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.baeldung.ddd.layers.domain;
2+
3+
class DomainException extends RuntimeException {
4+
DomainException(final String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
package com.baeldung.ddd.layers.domain;
22

3-
import com.baeldung.ddd.layers.domain.exception.DomainException;
43
import org.bson.types.ObjectId;
54

65
import java.math.BigDecimal;
76
import java.util.ArrayList;
87
import java.util.Collections;
98
import java.util.List;
9+
import java.util.UUID;
1010

1111
public class Order {
1212
private final ObjectId id;
1313
private OrderStatus status;
14-
private List<Product> products;
14+
private List<OrderItem> orderItems;
1515
private BigDecimal price;
1616

1717
public Order(final ObjectId id, final Product product) {
1818
this.id = id;
19-
this.products = new ArrayList<>(Collections.singletonList(product));
19+
this.orderItems = new ArrayList<>(Collections.singletonList(new OrderItem(product)));
2020
this.status = OrderStatus.CREATED;
2121
this.price = product.getPrice();
2222
}
@@ -26,29 +26,30 @@ public void complete() {
2626
this.status = OrderStatus.COMPLETED;
2727
}
2828

29-
public void addProduct(final Product product) {
29+
public void addOrder(final Product product) {
3030
validateState();
3131
validateProduct(product);
32-
products.add(product);
32+
orderItems.add(new OrderItem(product));
3333
price = price.add(product.getPrice());
3434
}
3535

36-
public void removeProduct(final String name) {
36+
public void removeOrder(final UUID id) {
3737
validateState();
38-
final Product product = getProduct(name);
39-
products.remove(product);
38+
final OrderItem orderItem = getOrderItem(id);
39+
orderItems.remove(orderItem);
4040

41-
price = price.subtract(product.getPrice());
41+
price = price.subtract(orderItem.getPrice());
4242
}
4343

44-
private Product getProduct(String name) {
45-
return products
44+
private OrderItem getOrderItem(final UUID id) {
45+
return orderItems
4646
.stream()
47-
.filter(product -> product
48-
.getName()
49-
.equals(name))
47+
.filter(orderItem -> orderItem
48+
.getProduct()
49+
.getId()
50+
.equals(id))
5051
.findFirst()
51-
.orElseThrow(() -> new DomainException("Product with " + name + " doesn't exist."));
52+
.orElseThrow(() -> new DomainException("Product with " + id + " doesn't exist."));
5253
}
5354

5455
private void validateState() {
@@ -71,11 +72,11 @@ public OrderStatus getStatus() {
7172
return status;
7273
}
7374

74-
public List<Product> getProducts() {
75-
return Collections.unmodifiableList(products);
76-
}
77-
7875
public BigDecimal getPrice() {
7976
return price;
8077
}
78+
79+
public List<OrderItem> getOrderItems() {
80+
return orderItems;
81+
}
8182
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.ddd.layers.domain;
2+
3+
import java.math.BigDecimal;
4+
import java.util.Objects;
5+
6+
public class OrderItem {
7+
private final Product product;
8+
9+
public OrderItem(final Product product) {
10+
this.product = product;
11+
}
12+
13+
public BigDecimal getPrice() {
14+
return product.getPrice();
15+
}
16+
17+
public Product getProduct() {
18+
return product;
19+
}
20+
21+
@Override
22+
public boolean equals(Object o) {
23+
if (this == o) return true;
24+
if (o == null || getClass() != o.getClass()) return false;
25+
OrderItem orderItem = (OrderItem) o;
26+
return Objects.equals(product, orderItem.product);
27+
}
28+
29+
@Override
30+
public int hashCode() {
31+
return Objects.hash(product);
32+
}
33+
}

ddd/src/main/java/com/baeldung/ddd/layers/domain/Product.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55

66
import java.math.BigDecimal;
77
import java.util.Objects;
8+
import java.util.UUID;
89

910
public class Product {
11+
private final UUID id;
1012
private final BigDecimal price;
1113
private final String name;
1214

1315
@JsonCreator
14-
public Product(@JsonProperty("price") final BigDecimal price, @JsonProperty("name") final String name) {
16+
public Product(@JsonProperty("id") final UUID id, @JsonProperty("price") final BigDecimal price, @JsonProperty("name") final String name) {
17+
this.id = id;
1518
this.price = price;
1619
this.name = name;
1720
}
@@ -24,20 +27,20 @@ public String getName() {
2427
return name;
2528
}
2629

30+
public UUID getId() {
31+
return id;
32+
}
33+
2734
@Override
28-
public boolean equals(final Object o) {
29-
if (this == o) {
30-
return true;
31-
}
32-
if (o == null || getClass() != o.getClass()) {
33-
return false;
34-
}
35-
final Product product = (Product) o;
36-
return Objects.equals(price, product.price) && Objects.equals(name, product.name);
35+
public boolean equals(Object o) {
36+
if (this == o) return true;
37+
if (o == null || getClass() != o.getClass()) return false;
38+
Product product = (Product) o;
39+
return Objects.equals(id, product.id) && Objects.equals(price, product.price) && Objects.equals(name, product.name);
3740
}
3841

3942
@Override
4043
public int hashCode() {
41-
return Objects.hash(price, name);
44+
return Objects.hash(id, price, name);
4245
}
4346
}

ddd/src/main/java/com/baeldung/ddd/layers/domain/exception/DomainException.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

ddd/src/main/java/com/baeldung/ddd/layers/domain/service/DomainOrderService.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import com.baeldung.ddd.layers.domain.repository.OrderRepository;
66
import org.bson.types.ObjectId;
77

8+
import java.util.UUID;
9+
810
public class DomainOrderService implements OrderService {
911

1012
private final OrderRepository orderRepository;
1113

12-
public DomainOrderService(OrderRepository orderRepository) {
14+
public DomainOrderService(final OrderRepository orderRepository) {
1315
this.orderRepository = orderRepository;
1416
}
1517

@@ -22,25 +24,25 @@ public ObjectId createOrder(final Product product) {
2224
}
2325

2426
@Override
25-
public void addProduct(ObjectId id, Product product) {
27+
public void addProduct(final ObjectId id, final Product product) {
2628
final Order order = getOrder(id);
27-
order.addProduct(product);
29+
order.addOrder(product);
2830

2931
orderRepository.save(order);
3032
}
3133

3234
@Override
33-
public void completeOrder(ObjectId id) {
35+
public void completeOrder(final ObjectId id) {
3436
final Order order = getOrder(id);
3537
order.complete();
3638

3739
orderRepository.save(order);
3840
}
3941

4042
@Override
41-
public void deleteProduct(ObjectId id, String name) {
43+
public void deleteProduct(final ObjectId id, final UUID productId) {
4244
final Order order = getOrder(id);
43-
order.removeProduct(name);
45+
order.removeOrder(productId);
4446

4547
orderRepository.save(order);
4648
}

0 commit comments

Comments
 (0)