From cdce22968d560aff156d32a97503ae8cf7eb25c4 Mon Sep 17 00:00:00 2001 From: CodeSteak Date: Wed, 6 May 2020 14:10:37 +0200 Subject: [PATCH] Implement proper querying of warehouse slot sums and booking accounts. --- .../ecommerce/components/SlotInitializer.java | 32 +++++++++++++++++++ .../shop/ShopCheckoutController.java | 4 +-- .../ecommerce/entities/warehouse/Slot.java | 17 ++++++++++ .../WarehouseBookingPositionSlotEntry.java | 6 +++- .../BookingAccountEntryRepository.java | 9 +++--- .../repos/warehouse/SlotRepository.java | 17 ++++++++++ ...useBookingPositionSlotEntryRepository.java | 6 ++-- 7 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 prototype/src/main/java/org/hso/ecommerce/components/SlotInitializer.java create mode 100644 prototype/src/main/java/org/hso/ecommerce/entities/warehouse/Slot.java create mode 100644 prototype/src/main/java/org/hso/ecommerce/repos/warehouse/SlotRepository.java diff --git a/prototype/src/main/java/org/hso/ecommerce/components/SlotInitializer.java b/prototype/src/main/java/org/hso/ecommerce/components/SlotInitializer.java new file mode 100644 index 0000000..ce8c39b --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/components/SlotInitializer.java @@ -0,0 +1,32 @@ +package org.hso.ecommerce.components; + +import org.hso.ecommerce.entities.warehouse.Slot; +import org.hso.ecommerce.repos.warehouse.SlotRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +@Component +public class SlotInitializer { + + @Autowired + private final SlotRepository slotRepository = null; + + // TODO: use values form cfg. + private final int NUM_SLOTS = 50; + + @PostConstruct + public void init() { + for (int i = 1; i <= NUM_SLOTS; i++) { + if (!slotRepository.findBySlotNum(i).isPresent()) { + Slot slotAdded = new Slot(); + slotAdded.slotNum = i; + slotRepository.save(slotAdded); + + System.out.println("Added Slot " + i + " to DB"); + } + } + } + +} diff --git a/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopCheckoutController.java b/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopCheckoutController.java index ffdd9f3..ab4827f 100644 --- a/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopCheckoutController.java +++ b/prototype/src/main/java/org/hso/ecommerce/controller/shop/ShopCheckoutController.java @@ -122,14 +122,14 @@ public class ShopCheckoutController { expectedPrice, Address.fromString(address), PaymentMethod.fromCreditCarNumber(cardnumber), - bookingEntryRepository.getByUser(user).orElse(BookingAccountEntry.newUser(user)), + bookingEntryRepository.getByUser(user.id).orElse(BookingAccountEntry.newUser(user)), bookingEntryRepository.getByVat().orElse(BookingAccountEntry.newVat()), bookingEntryRepository.getByMain().orElse(BookingAccountEntry.newMain()) ); for (ShoppingCart.ShoppingCartItem item : shoppingCart.getItems()) { Article article = articleRepository.findById(item.getArticleId()).get(); - action.addArticle(article, item.getAmount(), wbeseRepo.getByArticle(article)); + action.addArticle(article, item.getAmount(), wbeseRepo.getByArticle(article.id)); } CreateOrderAction.Result result = action.finish(); diff --git a/prototype/src/main/java/org/hso/ecommerce/entities/warehouse/Slot.java b/prototype/src/main/java/org/hso/ecommerce/entities/warehouse/Slot.java new file mode 100644 index 0000000..6a56541 --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/entities/warehouse/Slot.java @@ -0,0 +1,17 @@ +package org.hso.ecommerce.entities.warehouse; + +import javax.persistence.*; +import javax.validation.constraints.NotNull; + +@Entity +@Table(name = "warehouse_slots") +public class Slot { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Basic + public long id; + + @NotNull + @Column(unique = true) + public int slotNum; +} diff --git a/prototype/src/main/java/org/hso/ecommerce/entities/warehouse/WarehouseBookingPositionSlotEntry.java b/prototype/src/main/java/org/hso/ecommerce/entities/warehouse/WarehouseBookingPositionSlotEntry.java index d9ca4c6..766398f 100644 --- a/prototype/src/main/java/org/hso/ecommerce/entities/warehouse/WarehouseBookingPositionSlotEntry.java +++ b/prototype/src/main/java/org/hso/ecommerce/entities/warehouse/WarehouseBookingPositionSlotEntry.java @@ -3,6 +3,7 @@ package org.hso.ecommerce.entities.warehouse; import org.hso.ecommerce.entities.shop.Article; import javax.persistence.*; +import javax.validation.constraints.NotNull; @Entity @Table(name = "warehouse_booking_position_entries") @@ -13,6 +14,7 @@ public class WarehouseBookingPositionSlotEntry { @Basic public long id; + @NotNull @ManyToOne public Article article; @@ -20,7 +22,9 @@ public class WarehouseBookingPositionSlotEntry { //public int newSumArticles; public int newSumSlot; - public int slot; + @NotNull + @ManyToOne + public Slot slot; public WarehouseBookingPositionSlotEntry copyAddAmount(int amount) { WarehouseBookingPositionSlotEntry e = new WarehouseBookingPositionSlotEntry(); diff --git a/prototype/src/main/java/org/hso/ecommerce/repos/booking/BookingAccountEntryRepository.java b/prototype/src/main/java/org/hso/ecommerce/repos/booking/BookingAccountEntryRepository.java index 657e34d..d842a3a 100644 --- a/prototype/src/main/java/org/hso/ecommerce/repos/booking/BookingAccountEntryRepository.java +++ b/prototype/src/main/java/org/hso/ecommerce/repos/booking/BookingAccountEntryRepository.java @@ -1,7 +1,6 @@ package org.hso.ecommerce.repos.booking; import org.hso.ecommerce.entities.booking.BookingAccountEntry; -import org.hso.ecommerce.entities.user.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; @@ -11,13 +10,13 @@ import java.util.Optional; @Repository public interface BookingAccountEntryRepository extends JpaRepository { - @Query("SELECT e FROM BookingAccountEntry e WHERE e.userAccount = :user ORDER BY e.id DESC") - Optional getByUser(User user); + @Query(value = "SELECT * FROM booking_account_entries as e WHERE e.user_account_id = :user ORDER BY e.id DESC LIMIT 1", nativeQuery = true) + Optional getByUser(Long user); - @Query("SELECT e FROM BookingAccountEntry e WHERE e.isMainAccount = 1 ORDER BY e.id DESC") + @Query(value = "SELECT * FROM booking_account_entries as e WHERE e.is_main_account = 1 ORDER BY e.id DESC LIMIT 1", nativeQuery = true) Optional getByMain(); - @Query("SELECT e FROM BookingAccountEntry e WHERE e.isVATAccount = 1 ORDER BY e.id DESC") + @Query(value = "SELECT * FROM booking_account_entries as e WHERE e.isvataccount = 1 ORDER BY e.id DESC LIMIT 1", nativeQuery = true) Optional getByVat(); diff --git a/prototype/src/main/java/org/hso/ecommerce/repos/warehouse/SlotRepository.java b/prototype/src/main/java/org/hso/ecommerce/repos/warehouse/SlotRepository.java new file mode 100644 index 0000000..d0f00c0 --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/repos/warehouse/SlotRepository.java @@ -0,0 +1,17 @@ +package org.hso.ecommerce.repos.warehouse; + +import org.hso.ecommerce.entities.warehouse.Slot; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + +@Repository +public interface SlotRepository extends JpaRepository { + + @Query("SELECT s FROM Slot s WHERE s.slotNum = :slotNum") + Optional findBySlotNum(int slotNum); + + +} diff --git a/prototype/src/main/java/org/hso/ecommerce/repos/warehouse/WarehouseBookingPositionSlotEntryRepository.java b/prototype/src/main/java/org/hso/ecommerce/repos/warehouse/WarehouseBookingPositionSlotEntryRepository.java index 839a017..962231a 100644 --- a/prototype/src/main/java/org/hso/ecommerce/repos/warehouse/WarehouseBookingPositionSlotEntryRepository.java +++ b/prototype/src/main/java/org/hso/ecommerce/repos/warehouse/WarehouseBookingPositionSlotEntryRepository.java @@ -1,6 +1,5 @@ package org.hso.ecommerce.repos.warehouse; -import org.hso.ecommerce.entities.shop.Article; import org.hso.ecommerce.entities.warehouse.WarehouseBookingPositionSlotEntry; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; @@ -12,7 +11,8 @@ import java.util.List; public interface WarehouseBookingPositionSlotEntryRepository extends JpaRepository { // TODO this is wrong. revisit. - @Query("SELECT e FROM WarehouseBookingPositionSlotEntry e WHERE e.article = :article ORDER BY e.id DESC") - List getByArticle(Article article); + // @Query("SELECT e FROM WarehouseBookingPositionSlotEntry e, Slot s WHERE e.slot = s AND e.article = :article GROUP BY e.slot.slotNum HAVING max(e.id)") + @Query(value = "Select e.id, e.article_id, e.new_sum_slot, e.slot_id from warehouse_booking_position_entries as e, warehouse_slots as s where e.slot_id = s.id AND e.article_id = :article GROUP BY s.slot_num HAVING max(e.id)", nativeQuery = true) + List getByArticle(long article); }