Skip to content

Commit

Permalink
Optimize get products from inventory database
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkopp committed Mar 19, 2024
1 parent 2f095bd commit 1e9e8d7
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.*;

/**
* Manages the inventory and the reservations.
Expand Down Expand Up @@ -56,6 +54,20 @@ public Optional<Product> getSingleProduct(String productId) {
return InventoryProductMapper.toProduct(inventoryItem);
}

/**
* Get the products with the given productIds from the inventory.
* <p>
* If there are either no products with the given sessionId, or the retrieval of the products failed, an empty collection
* is returned.
*
* @param productIds collection of product ids to be retrieved
* @return products if product ids exists
*/
public List<Product> getProducts(Collection<String> productIds) {
List<InventoryItem> inventoryItems = inventoryRepository.findAllById(productIds);
return inventoryItems.stream().map(InventoryProductMapper::toProduct).toList();
}

/**
* commit reservations associated with given sessionId.
*
Expand Down
17 changes: 6 additions & 11 deletions src/main/java/de/unistuttgart/t2/modulith/order/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

/**
* Creates and updates orders.
Expand Down Expand Up @@ -132,23 +132,18 @@ public String confirmOrder(String sessionId, String cardNumber, String cardOwner
/**
* Calculates the total of a users cart.
* <p>
* Depends on the cart module to get the cart content and depends on the inventory module to get the price per
* unit.
* Depends on the cart module to get the cart content and
* depends on the inventory module to get the price for each product.
*
* @param sessionId identifies the session to get total for
* @return the total money to pay for products in the cart
*/
private double getTotal(String sessionId) {
CartContent cart = cartService.getCart(sessionId).orElse(new CartContent());

double total = 0;

for (String productId : cart.getProductIds()) {
Optional<Product> product = inventoryService.getSingleProduct(productId);
if (product.isEmpty()) {
return 0;
}
total += product.get().getPrice() * cart.getUnits(productId);
List<Product> products = inventoryService.getProducts(cart.getProductIds());
for (Product product : products) {
total += product.getPrice() * cart.getUnits(product.getId());
}
return total;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,10 @@ public List<Product> getProductsInCart(String sessionId) {
if (cart.isPresent()) {
CartContent cartContent = cart.get();

for (String productId : cartContent.getProductIds()) {
inventoryService.getSingleProduct(productId).ifPresent(p -> {
p.setUnits(cartContent.getUnits(productId));
results.add(p);
});
List<Product> products = inventoryService.getProducts(cartContent.getProductIds());
for (Product product : products) {
product.setUnits(cartContent.getUnits(product.getId()));
results.add(product);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/test/java/de/unistuttgart/t2/modulith/TestData.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public static Optional<Product> inventoryResponse() {
return Optional.of(new Product(productId, "name", "description", 5, price));
}

public static List<Product> inventoryResponseOneProductInList() {
Product product1 = inventoryResponse().get();
return new ArrayList<>(List.of(product1));
}

public static Optional<Product> anotherInventoryResponse() {
return Optional.of(new Product(anotherProductId, "name2", "description2", 5, anotherPrice));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ public void getSingleProduct() {
assertEquals(1.0, product.getPrice());
}

@Test
public void getProducts() {
// setup inventory response
List<Product> allProducts = inventoryResponseAllProducts();
List<InventoryItem> inventoryItems = allProducts.stream().map(InventoryProductMapper::toInventoryItem).toList();
List<String> ids = inventoryItems.stream().map(InventoryItem::getId).toList();
when(productRepository.findAllById(ids)).thenReturn(inventoryItems);

// execute
List<Product> products = inventoryService.getProducts(ids);

// assert
assertNotNull(products);
assertEquals(2, products.size());
assertEquals(productId, products.get(0).getId());
assertEquals(anotherProductId, products.get(1).getId());
}

@Test
public void makeReservation() throws InsufficientUnitsAvailableException {
// setup inventory response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import de.unistuttgart.t2.modulith.cart.CartService;
import de.unistuttgart.t2.modulith.cart.repository.CartItem;
import de.unistuttgart.t2.modulith.cart.repository.CartRepository;
import de.unistuttgart.t2.modulith.inventory.InventoryService;
import de.unistuttgart.t2.modulith.inventory.InsufficientUnitsAvailableException;
import de.unistuttgart.t2.modulith.inventory.InventoryService;
import de.unistuttgart.t2.modulith.inventory.repository.InventoryItem;
import de.unistuttgart.t2.modulith.inventory.repository.InventoryRepository;
import de.unistuttgart.t2.modulith.inventory.repository.ReservationRepository;
Expand Down Expand Up @@ -114,7 +114,7 @@ public void confirmOrder_CalledMultipleTimes_ErrorIsThrown() throws Exception {
public void confirmOrder_CalculatingTotalFails_OrderIsNotPlaced() throws PaymentFailedException {

// setup
when(inventoryService.getSingleProduct(productId)).thenThrow(new RuntimeException("runtime error"));
when(inventoryService.getProducts(anyCollection())).thenThrow(new RuntimeException("runtime error"));

// execute
Exception actualException = assertThrows(Exception.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public void confirmOrder() throws Exception {

// Setup mocks
when(cartService.getCart(sessionId)).thenReturn(cartResponse());
when(inventoryService.getSingleProduct(productId)).thenReturn(inventoryResponse());
when(inventoryService.getProducts(cartResponse().get().getProductIds()))
.thenReturn(inventoryResponseOneProductInList());

// execute
String id = orderService.confirmOrder(sessionId, "cardNumber", "cardOwner", "checksum");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void beforeEach() {
public void confirmOrderSucceeds() throws Exception {

when(cartService.getCart(sessionId)).thenReturn(cartResponse());
when(inventoryService.getSingleProduct(productId)).thenReturn(inventoryResponse());
when(inventoryService.getProducts(anyCollection())).thenReturn(inventoryResponseOneProductInList());
when(orderRepository.save(any())).thenReturn(new OrderItem(sessionId));

orderService.confirmOrder(sessionId, "cardNumber", "cardOwner", "checksum");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ public void deleteItemFromCart() {
public void getProductsInCart() {
// setup
when(cartService.getCart(sessionId)).thenReturn(cartResponse());
when(inventoryService.getSingleProduct(productId)).thenReturn(inventoryResponse());
when(inventoryService.getProducts(cartResponse().get().getProductIds()))
.thenReturn(inventoryResponseOneProductInList());

// execute
List<Product> result = service.getProductsInCart(sessionId);
Expand Down

0 comments on commit 1e9e8d7

Please sign in to comment.