Skip to content

Commit

Permalink
Optimize commit & delete reservations
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkopp committed Mar 20, 2024
1 parent 1e9e8d7 commit 4537aae
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package de.unistuttgart.t2.modulith.inventory;

import de.unistuttgart.t2.modulith.inventory.repository.*;
import de.unistuttgart.t2.modulith.inventory.repository.InventoryItem;
import de.unistuttgart.t2.modulith.inventory.repository.InventoryProductMapper;
import de.unistuttgart.t2.modulith.inventory.repository.InventoryRepository;
import de.unistuttgart.t2.modulith.inventory.repository.ReservationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
* Manages the inventory and the reservations.
Expand All @@ -22,12 +28,9 @@
public class InventoryService {

private final InventoryRepository inventoryRepository;
private final ReservationRepository reservationRepository;

public InventoryService(@Autowired InventoryRepository inventoryRepository,
@Autowired ReservationRepository reservationRepository) {
public InventoryService(@Autowired InventoryRepository inventoryRepository) {
this.inventoryRepository = inventoryRepository;
this.reservationRepository = reservationRepository;
}

/**
Expand Down Expand Up @@ -79,13 +82,6 @@ public void commitReservations(String sessionId) {
item.commitReservation(sessionId);
}
inventoryRepository.saveAll(items);

List<Reservation> reservations = reservationRepository.findAll();
for (Reservation reservation : reservations) {
if (reservation.getUserId().equals(sessionId)) {
reservationRepository.delete(reservation);
}
}
}

/**
Expand All @@ -97,16 +93,8 @@ public void deleteReservations(String sessionId) {
List<InventoryItem> items = inventoryRepository.findAll();
for (InventoryItem item : items) {
item.deleteReservation(sessionId);

}
inventoryRepository.saveAll(items);

List<Reservation> reservations = reservationRepository.findAll();
for (Reservation reservation : reservations) {
if (reservation.getUserId().equals(sessionId)) {
reservationRepository.delete(reservation);
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class InventoryItem {
@JsonProperty("price")
private final double price;

@OneToMany(mappedBy = "item", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@OneToMany(mappedBy = "item", cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true)
@JsonProperty("reservations")
private final List<Reservation> reservations;

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ t2:
logging:
level:
de.unistuttgart.t2: DEBUG
org.hibernate.SQL: DEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import de.unistuttgart.t2.modulith.inventory.repository.InventoryItem;
import de.unistuttgart.t2.modulith.inventory.repository.InventoryProductMapper;
import de.unistuttgart.t2.modulith.inventory.repository.InventoryRepository;
import de.unistuttgart.t2.modulith.inventory.repository.ReservationRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -28,14 +27,11 @@ public class InventoryServiceTests {
@Mock
InventoryRepository productRepository;

@Mock
ReservationRepository reservationRepository;

InventoryService inventoryService;

@BeforeEach
public void setup() {
inventoryService = new InventoryService(productRepository, reservationRepository);
inventoryService = new InventoryService(productRepository);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,12 @@ public class OrderConfirmIntegrationTests {
public void setup() throws InsufficientUnitsAvailableException {

// Spying on InventoryService, CartService and OrderService is needed to be able to throw exceptions for specific test cases
this.inventoryService = spy(new InventoryService(inventoryRepository, reservationRepository));
this.inventoryService = spy(new InventoryService(inventoryRepository));
this.cartService = spy(new CartService(cartRepository));
this.orderService = spy(new OrderService(cartService, inventoryService, paymentService, orderRepository));

orderRepository.deleteAll();
cartRepository.deleteAll();
reservationRepository.deleteAll();
inventoryRepository.deleteAll();

InventoryItem savedInventoryItem = inventoryRepository.save(
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ spring:
logging:
level:
ROOT: INFO
org.hibernate.SQL: DEBUG
de.unistuttgart.t2.modulith: DEBUG

# org.springframework.orm.jpa: TRACE
# org.springframework.transaction: TRACE

Expand Down

0 comments on commit 4537aae

Please sign in to comment.