From a6a2adc7dbc7036f88815d3c469402160753e413 Mon Sep 17 00:00:00 2001 From: CodeSteak Date: Sun, 21 Jun 2020 16:49:35 +0200 Subject: [PATCH] implement Config for Supplier and Delivery Service --- .../ecommerce/api/RestServiceForDelivery.java | 12 ++++-- .../components/SupplierInitializer.java | 42 +++++++++++++++++++ .../repos/supplier/SupplierRepository.java | 12 ++++-- 3 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 prototype/src/main/java/org/hso/ecommerce/components/SupplierInitializer.java diff --git a/prototype/src/main/java/org/hso/ecommerce/api/RestServiceForDelivery.java b/prototype/src/main/java/org/hso/ecommerce/api/RestServiceForDelivery.java index e259cd3..99ff5cb 100644 --- a/prototype/src/main/java/org/hso/ecommerce/api/RestServiceForDelivery.java +++ b/prototype/src/main/java/org/hso/ecommerce/api/RestServiceForDelivery.java @@ -1,5 +1,6 @@ package org.hso.ecommerce.api; +import org.hso.ecommerce.app.config.AppSettings; import org.hso.ecommerce.entities.shop.CustomerOrder; import org.hso.ecommerce.uimodel.DeliveryData; import org.hso.ecommerce.uimodel.DeliveryDataEnum; @@ -20,11 +21,16 @@ public class RestServiceForDelivery { @Autowired private final RestTemplateBuilder restTemplateBuilder = null; - private final String DELIVERY_IP_ADDRESS = "http://[::1]:8082"; + @Autowired + private final AppSettings settings = null; + + private String getDeliveryEndpoint() { + return settings.getParcelServiceApiURL(); + } public String getDeliveryID(CustomerOrder customerOrder) throws ResourceAccessException { - String url = DELIVERY_IP_ADDRESS + "/newDelivery"; + String url = getDeliveryEndpoint() + "/newDelivery"; RestTemplate restTemplate = restTemplateBuilder.build(); @@ -49,7 +55,7 @@ public class RestServiceForDelivery { public DeliveryData getDeliveryData(String trackingID) { - String url = DELIVERY_IP_ADDRESS + "/status"; + String url = getDeliveryEndpoint() + "/status"; RestTemplate restTemplate = restTemplateBuilder.build(); diff --git a/prototype/src/main/java/org/hso/ecommerce/components/SupplierInitializer.java b/prototype/src/main/java/org/hso/ecommerce/components/SupplierInitializer.java new file mode 100644 index 0000000..c057426 --- /dev/null +++ b/prototype/src/main/java/org/hso/ecommerce/components/SupplierInitializer.java @@ -0,0 +1,42 @@ +package org.hso.ecommerce.components; + +import org.hso.ecommerce.app.config.AppSettings; +import org.hso.ecommerce.app.config.YAMLData; +import org.hso.ecommerce.entities.supplier.Supplier; +import org.hso.ecommerce.repos.supplier.SupplierRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.util.Optional; + +@Component +public class SupplierInitializer { + + @Autowired + private final SupplierRepository supplierRepository = null; + + @Autowired + private final AppSettings appSettings = null; + + @PostConstruct + public void init() { + for (YAMLData.Supplier cfg : appSettings.getSuppliers()) { + Optional sup = supplierRepository.findByUuid(cfg.id); + + Supplier supplier; + if (sup.isPresent()) { + supplier = sup.get(); + supplier.name = cfg.name; + supplier.apiUrl = cfg.apiURL; + } else { + supplier = new Supplier(); + supplier.uuid = cfg.id; + supplier.apiUrl = cfg.apiURL; + supplier.name = cfg.name; + } + supplierRepository.save(supplier); + } + } + +} diff --git a/prototype/src/main/java/org/hso/ecommerce/repos/supplier/SupplierRepository.java b/prototype/src/main/java/org/hso/ecommerce/repos/supplier/SupplierRepository.java index 14b3a75..24c444d 100644 --- a/prototype/src/main/java/org/hso/ecommerce/repos/supplier/SupplierRepository.java +++ b/prototype/src/main/java/org/hso/ecommerce/repos/supplier/SupplierRepository.java @@ -1,19 +1,23 @@ package org.hso.ecommerce.repos.supplier; -import java.util.List; - import org.hso.ecommerce.entities.supplier.Supplier; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.List; +import java.util.Optional; + @Repository public interface SupplierRepository extends JpaRepository { - + @Query("SELECT a FROM Supplier a") List findAll(); - + @Query("SELECT a FROM Supplier a WHERE a.id = :supplierId") Supplier findSupplierById(@Param("supplierId") long supplierId); + + @Query("SELECT a FROM Supplier a WHERE a.uuid = :uuid") + Optional findByUuid(@Param("uuid") String uuid); } \ No newline at end of file