[ BAEL-2275 ] : Use UUID instead of ObjectId.

This commit is contained in:
Lukasz Rys 2019-11-30 23:11:02 +01:00
parent fd2d9b1477
commit 406d6ac927
11 changed files with 41 additions and 42 deletions

View File

@ -4,7 +4,6 @@ import com.baeldung.ddd.layers.application.request.AddProductRequest;
import com.baeldung.ddd.layers.application.request.CreateOrderRequest; import com.baeldung.ddd.layers.application.request.CreateOrderRequest;
import com.baeldung.ddd.layers.application.response.CreateOrderResponse; import com.baeldung.ddd.layers.application.response.CreateOrderResponse;
import com.baeldung.ddd.layers.domain.service.OrderService; import com.baeldung.ddd.layers.domain.service.OrderService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -24,23 +23,23 @@ public class OrderController {
@PostMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) @PostMapping(produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
CreateOrderResponse createOrder(@RequestBody final CreateOrderRequest createOrderRequest) { CreateOrderResponse createOrder(@RequestBody final CreateOrderRequest createOrderRequest) {
final ObjectId id = orderService.createOrder(createOrderRequest.getProduct()); final UUID id = orderService.createOrder(createOrderRequest.getProduct());
return new CreateOrderResponse(id.toString()); return new CreateOrderResponse(id);
} }
@PostMapping(value = "/{id}/products", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) @PostMapping(value = "/{id}/products", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
void addProduct(@PathVariable final ObjectId id, @RequestBody final AddProductRequest addProductRequest) { void addProduct(@PathVariable final UUID id, @RequestBody final AddProductRequest addProductRequest) {
orderService.addProduct(id, addProductRequest.getProduct()); orderService.addProduct(id, addProductRequest.getProduct());
} }
@DeleteMapping(value = "/{id}/products", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE) @DeleteMapping(value = "/{id}/products", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
void deleteProduct(@PathVariable final ObjectId id, @RequestParam final UUID productId) { void deleteProduct(@PathVariable final UUID id, @RequestParam final UUID productId) {
orderService.deleteProduct(id, productId); orderService.deleteProduct(id, productId);
} }
@PostMapping("/{id}/complete") @PostMapping("/{id}/complete")
void completeOrder(@PathVariable final ObjectId id) { void completeOrder(@PathVariable final UUID id) {
orderService.completeOrder(id); orderService.completeOrder(id);
} }
} }

View File

@ -1,15 +1,15 @@
package com.baeldung.ddd.layers.application.response; package com.baeldung.ddd.layers.application.response;
import org.bson.types.ObjectId; import java.util.UUID;
public class CreateOrderResponse { public class CreateOrderResponse {
private final String id; private final UUID id;
public CreateOrderResponse(final String id) { public CreateOrderResponse(final UUID id) {
this.id = id; this.id = id;
} }
public String getId() { public UUID getId() {
return id; return id;
} }
} }

View File

@ -1,7 +1,5 @@
package com.baeldung.ddd.layers.domain; package com.baeldung.ddd.layers.domain;
import org.bson.types.ObjectId;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -9,12 +7,12 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
public class Order { public class Order {
private final ObjectId id; private UUID id;
private OrderStatus status; private OrderStatus status;
private List<OrderItem> orderItems; private List<OrderItem> orderItems;
private BigDecimal price; private BigDecimal price;
public Order(final ObjectId id, final Product product) { public Order(final UUID id, final Product product) {
this.id = id; this.id = id;
this.orderItems = new ArrayList<>(Collections.singletonList(new OrderItem(product))); this.orderItems = new ArrayList<>(Collections.singletonList(new OrderItem(product)));
this.status = OrderStatus.CREATED; this.status = OrderStatus.CREATED;
@ -63,7 +61,7 @@ public class Order {
} }
} }
public ObjectId getId() { public UUID getId() {
return id; return id;
} }
@ -78,4 +76,7 @@ public class Order {
public List<OrderItem> getOrderItems() { public List<OrderItem> getOrderItems() {
return Collections.unmodifiableList(orderItems); return Collections.unmodifiableList(orderItems);
} }
private Order() {
}
} }

View File

@ -5,8 +5,8 @@ import java.util.Objects;
import java.util.UUID; import java.util.UUID;
public class OrderItem { public class OrderItem {
private final UUID productId; private UUID productId;
private final BigDecimal price; private BigDecimal price;
public OrderItem(final Product product) { public OrderItem(final Product product) {
this.productId = product.getId(); this.productId = product.getId();
@ -21,6 +21,9 @@ public class OrderItem {
return price; return price;
} }
private OrderItem() {
}
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if (this == o) return true; if (this == o) return true;

View File

@ -1,12 +1,12 @@
package com.baeldung.ddd.layers.domain.repository; package com.baeldung.ddd.layers.domain.repository;
import com.baeldung.ddd.layers.domain.Order; import com.baeldung.ddd.layers.domain.Order;
import org.bson.types.ObjectId;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
public interface OrderRepository { public interface OrderRepository {
Optional<Order> findById(ObjectId id); Optional<Order> findById(UUID id);
void save(Order order); void save(Order order);
} }

View File

@ -3,7 +3,6 @@ package com.baeldung.ddd.layers.domain.service;
import com.baeldung.ddd.layers.domain.Order; import com.baeldung.ddd.layers.domain.Order;
import com.baeldung.ddd.layers.domain.Product; import com.baeldung.ddd.layers.domain.Product;
import com.baeldung.ddd.layers.domain.repository.OrderRepository; import com.baeldung.ddd.layers.domain.repository.OrderRepository;
import org.bson.types.ObjectId;
import java.util.UUID; import java.util.UUID;
@ -16,15 +15,15 @@ public class DomainOrderService implements OrderService {
} }
@Override @Override
public ObjectId createOrder(final Product product) { public UUID createOrder(final Product product) {
final Order order = new Order(ObjectId.get(), product); final Order order = new Order(UUID.randomUUID(), product);
orderRepository.save(order); orderRepository.save(order);
return order.getId(); return order.getId();
} }
@Override @Override
public void addProduct(final ObjectId id, final Product product) { public void addProduct(final UUID id, final Product product) {
final Order order = getOrder(id); final Order order = getOrder(id);
order.addOrder(product); order.addOrder(product);
@ -32,7 +31,7 @@ public class DomainOrderService implements OrderService {
} }
@Override @Override
public void completeOrder(final ObjectId id) { public void completeOrder(final UUID id) {
final Order order = getOrder(id); final Order order = getOrder(id);
order.complete(); order.complete();
@ -40,14 +39,14 @@ public class DomainOrderService implements OrderService {
} }
@Override @Override
public void deleteProduct(final ObjectId id, final UUID productId) { public void deleteProduct(final UUID id, final UUID productId) {
final Order order = getOrder(id); final Order order = getOrder(id);
order.removeOrder(productId); order.removeOrder(productId);
orderRepository.save(order); orderRepository.save(order);
} }
private Order getOrder(ObjectId id) { private Order getOrder(UUID id) {
return orderRepository return orderRepository
.findById(id) .findById(id)
.orElseThrow(() -> new RuntimeException("Order with given id doesn't exist")); .orElseThrow(() -> new RuntimeException("Order with given id doesn't exist"));

View File

@ -1,16 +1,15 @@
package com.baeldung.ddd.layers.domain.service; package com.baeldung.ddd.layers.domain.service;
import com.baeldung.ddd.layers.domain.Product; import com.baeldung.ddd.layers.domain.Product;
import org.bson.types.ObjectId;
import java.util.UUID; import java.util.UUID;
public interface OrderService { public interface OrderService {
ObjectId createOrder(final Product product); UUID createOrder(Product product);
void addProduct(ObjectId id, Product product); void addProduct(UUID id, Product product);
void completeOrder(ObjectId id); void completeOrder(UUID id);
void deleteProduct(ObjectId id, UUID productId); void deleteProduct(UUID id, UUID productId);
} }

View File

@ -2,11 +2,11 @@ package com.baeldung.ddd.layers.infrastracture.repository;
import com.baeldung.ddd.layers.domain.Order; import com.baeldung.ddd.layers.domain.Order;
import com.baeldung.ddd.layers.domain.repository.OrderRepository; import com.baeldung.ddd.layers.domain.repository.OrderRepository;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
@Component @Component
public class MongoDbOrderRepository implements OrderRepository { public class MongoDbOrderRepository implements OrderRepository {
@ -19,7 +19,7 @@ public class MongoDbOrderRepository implements OrderRepository {
} }
@Override @Override
public Optional<Order> findById(final ObjectId id) { public Optional<Order> findById(final UUID id) {
return orderRepository.findById(id); return orderRepository.findById(id);
} }

View File

@ -1,10 +1,11 @@
package com.baeldung.ddd.layers.infrastracture.repository; package com.baeldung.ddd.layers.infrastracture.repository;
import com.baeldung.ddd.layers.domain.Order; import com.baeldung.ddd.layers.domain.Order;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.UUID;
@Repository @Repository
public interface SpringDataOrderRepository extends MongoRepository<Order, ObjectId> { public interface SpringDataOrderRepository extends MongoRepository<Order, UUID> {
} }

View File

@ -1,13 +1,11 @@
package com.baeldung.ddd.layers.domain; package com.baeldung.ddd.layers.domain;
import org.bson.types.ObjectId;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.UUID; import java.util.UUID;
public class OrderProvider { public class OrderProvider {
public static Order getCreatedOrder() { public static Order getCreatedOrder() {
return new Order(ObjectId.get(), new Product(UUID.randomUUID(), BigDecimal.TEN, "productName")); return new Order(UUID.randomUUID(), new Product(UUID.randomUUID(), BigDecimal.TEN, "productName"));
} }
public static Order getCompletedOrder() { public static Order getCompletedOrder() {

View File

@ -4,7 +4,6 @@ import com.baeldung.ddd.layers.domain.Order;
import com.baeldung.ddd.layers.domain.OrderProvider; import com.baeldung.ddd.layers.domain.OrderProvider;
import com.baeldung.ddd.layers.domain.Product; import com.baeldung.ddd.layers.domain.Product;
import com.baeldung.ddd.layers.domain.repository.OrderRepository; import com.baeldung.ddd.layers.domain.repository.OrderRepository;
import org.bson.types.ObjectId;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable; import org.junit.jupiter.api.function.Executable;
@ -33,7 +32,7 @@ class DomainOrderServiceUnitTest {
void shouldCreateOrder_thenSaveIt() { void shouldCreateOrder_thenSaveIt() {
final Product product = new Product(UUID.randomUUID(), BigDecimal.TEN, "productName"); final Product product = new Product(UUID.randomUUID(), BigDecimal.TEN, "productName");
final ObjectId id = tested.createOrder(product); final UUID id = tested.createOrder(product);
verify(orderRepository).save(any(Order.class)); verify(orderRepository).save(any(Order.class));
assertNotNull(id); assertNotNull(id);
@ -54,7 +53,7 @@ class DomainOrderServiceUnitTest {
@Test @Test
void shouldAddProduct_thenThrowException() { void shouldAddProduct_thenThrowException() {
final Product product = new Product(UUID.randomUUID(), BigDecimal.TEN, "test"); final Product product = new Product(UUID.randomUUID(), BigDecimal.TEN, "test");
final ObjectId id = ObjectId.get(); final UUID id = UUID.randomUUID();
when(orderRepository.findById(id)).thenReturn(Optional.empty()); when(orderRepository.findById(id)).thenReturn(Optional.empty());
final Executable executable = () -> tested.addProduct(id, product); final Executable executable = () -> tested.addProduct(id, product);