[ BAEL-2275 ] : Use UUID instead of ObjectId.
This commit is contained in:
parent
fd2d9b1477
commit
406d6ac927
|
@ -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.response.CreateOrderResponse;
|
||||
import com.baeldung.ddd.layers.domain.service.OrderService;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.MediaType;
|
||||
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)
|
||||
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)
|
||||
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());
|
||||
}
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/complete")
|
||||
void completeOrder(@PathVariable final ObjectId id) {
|
||||
void completeOrder(@PathVariable final UUID id) {
|
||||
orderService.completeOrder(id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,15 +1,15 @@
|
|||
package com.baeldung.ddd.layers.application.response;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
import java.util.UUID;
|
||||
|
||||
public class CreateOrderResponse {
|
||||
private final String id;
|
||||
private final UUID id;
|
||||
|
||||
public CreateOrderResponse(final String id) {
|
||||
public CreateOrderResponse(final UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package com.baeldung.ddd.layers.domain;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
@ -9,12 +7,12 @@ import java.util.List;
|
|||
import java.util.UUID;
|
||||
|
||||
public class Order {
|
||||
private final ObjectId id;
|
||||
private UUID id;
|
||||
private OrderStatus status;
|
||||
private List<OrderItem> orderItems;
|
||||
private BigDecimal price;
|
||||
|
||||
public Order(final ObjectId id, final Product product) {
|
||||
public Order(final UUID id, final Product product) {
|
||||
this.id = id;
|
||||
this.orderItems = new ArrayList<>(Collections.singletonList(new OrderItem(product)));
|
||||
this.status = OrderStatus.CREATED;
|
||||
|
@ -63,7 +61,7 @@ public class Order {
|
|||
}
|
||||
}
|
||||
|
||||
public ObjectId getId() {
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
|
@ -78,4 +76,7 @@ public class Order {
|
|||
public List<OrderItem> getOrderItems() {
|
||||
return Collections.unmodifiableList(orderItems);
|
||||
}
|
||||
|
||||
private Order() {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,8 +5,8 @@ import java.util.Objects;
|
|||
import java.util.UUID;
|
||||
|
||||
public class OrderItem {
|
||||
private final UUID productId;
|
||||
private final BigDecimal price;
|
||||
private UUID productId;
|
||||
private BigDecimal price;
|
||||
|
||||
public OrderItem(final Product product) {
|
||||
this.productId = product.getId();
|
||||
|
@ -21,6 +21,9 @@ public class OrderItem {
|
|||
return price;
|
||||
}
|
||||
|
||||
private OrderItem() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
package com.baeldung.ddd.layers.domain.repository;
|
||||
|
||||
import com.baeldung.ddd.layers.domain.Order;
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface OrderRepository {
|
||||
Optional<Order> findById(ObjectId id);
|
||||
Optional<Order> findById(UUID id);
|
||||
|
||||
void save(Order order);
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ package com.baeldung.ddd.layers.domain.service;
|
|||
import com.baeldung.ddd.layers.domain.Order;
|
||||
import com.baeldung.ddd.layers.domain.Product;
|
||||
import com.baeldung.ddd.layers.domain.repository.OrderRepository;
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
|
@ -16,15 +15,15 @@ public class DomainOrderService implements OrderService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public ObjectId createOrder(final Product product) {
|
||||
final Order order = new Order(ObjectId.get(), product);
|
||||
public UUID createOrder(final Product product) {
|
||||
final Order order = new Order(UUID.randomUUID(), product);
|
||||
orderRepository.save(order);
|
||||
|
||||
return order.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addProduct(final ObjectId id, final Product product) {
|
||||
public void addProduct(final UUID id, final Product product) {
|
||||
final Order order = getOrder(id);
|
||||
order.addOrder(product);
|
||||
|
||||
|
@ -32,7 +31,7 @@ public class DomainOrderService implements OrderService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void completeOrder(final ObjectId id) {
|
||||
public void completeOrder(final UUID id) {
|
||||
final Order order = getOrder(id);
|
||||
order.complete();
|
||||
|
||||
|
@ -40,14 +39,14 @@ public class DomainOrderService implements OrderService {
|
|||
}
|
||||
|
||||
@Override
|
||||
public void deleteProduct(final ObjectId id, final UUID productId) {
|
||||
public void deleteProduct(final UUID id, final UUID productId) {
|
||||
final Order order = getOrder(id);
|
||||
order.removeOrder(productId);
|
||||
|
||||
orderRepository.save(order);
|
||||
}
|
||||
|
||||
private Order getOrder(ObjectId id) {
|
||||
private Order getOrder(UUID id) {
|
||||
return orderRepository
|
||||
.findById(id)
|
||||
.orElseThrow(() -> new RuntimeException("Order with given id doesn't exist"));
|
||||
|
|
|
@ -1,16 +1,15 @@
|
|||
package com.baeldung.ddd.layers.domain.service;
|
||||
|
||||
import com.baeldung.ddd.layers.domain.Product;
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -2,11 +2,11 @@ package com.baeldung.ddd.layers.infrastracture.repository;
|
|||
|
||||
import com.baeldung.ddd.layers.domain.Order;
|
||||
import com.baeldung.ddd.layers.domain.repository.OrderRepository;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
public class MongoDbOrderRepository implements OrderRepository {
|
||||
|
@ -19,7 +19,7 @@ public class MongoDbOrderRepository implements OrderRepository {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Optional<Order> findById(final ObjectId id) {
|
||||
public Optional<Order> findById(final UUID id) {
|
||||
return orderRepository.findById(id);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
package com.baeldung.ddd.layers.infrastracture.repository;
|
||||
|
||||
import com.baeldung.ddd.layers.domain.Order;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Repository
|
||||
public interface SpringDataOrderRepository extends MongoRepository<Order, ObjectId> {
|
||||
public interface SpringDataOrderRepository extends MongoRepository<Order, UUID> {
|
||||
}
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
package com.baeldung.ddd.layers.domain;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.UUID;
|
||||
|
||||
public class OrderProvider {
|
||||
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() {
|
||||
|
|
|
@ -4,7 +4,6 @@ import com.baeldung.ddd.layers.domain.Order;
|
|||
import com.baeldung.ddd.layers.domain.OrderProvider;
|
||||
import com.baeldung.ddd.layers.domain.Product;
|
||||
import com.baeldung.ddd.layers.domain.repository.OrderRepository;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
|
@ -33,7 +32,7 @@ class DomainOrderServiceUnitTest {
|
|||
void shouldCreateOrder_thenSaveIt() {
|
||||
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));
|
||||
assertNotNull(id);
|
||||
|
@ -54,7 +53,7 @@ class DomainOrderServiceUnitTest {
|
|||
@Test
|
||||
void shouldAddProduct_thenThrowException() {
|
||||
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());
|
||||
|
||||
final Executable executable = () -> tested.addProduct(id, product);
|
||||
|
|
Loading…
Reference in New Issue