impl tracking ID

This commit is contained in:
Philipp 2020-06-10 17:03:59 +02:00
parent f671baf7f0
commit ebeba72278
6 changed files with 40 additions and 35 deletions

View File

@ -1,4 +1,4 @@
package org.hso.ecommerce.action.shop;
package org.hso.ecommerce.api;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
@ -19,8 +19,8 @@ import java.util.*;
public class RestServiceForDelivery {
@Autowired
private RestTemplateBuilder restTemplateBuilder;
private final RestTemplateBuilder restTemplateBuilder = null;
public String getDeliveryID(CustomerOrder customerOrder)
{
@ -61,20 +61,11 @@ public class RestServiceForDelivery {
HttpEntity<?> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, String.class);
ObjectMapper mapper = new ObjectMapper();
JsonNode node = null;
try {
node = mapper.readTree(response.getBody());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
ResponseEntity<CustomerOrderController.DeliveryData> response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, CustomerOrderController.DeliveryData.class);
if (response.getStatusCode() == HttpStatus.OK)
{
return new CustomerOrderController.DeliveryData(node.get("status").asText(),node.get("estimatedArrival").asText());
return response.getBody();
} else {
return null;
}

View File

@ -3,9 +3,12 @@ package org.hso.ecommerce.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import java.util.Arrays;
@SpringBootApplication
@ComponentScan(basePackages = {"org.hso.ecommerce"})
@EntityScan("org.hso.ecommerce")
@ -13,6 +16,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

View File

@ -2,21 +2,21 @@ package org.hso.ecommerce.controller.intern.customers;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.Session;
import org.hso.ecommerce.action.shop.RestServiceForDelivery;
import org.hso.ecommerce.api.RestServiceForDelivery;
import org.hso.ecommerce.entities.shop.CustomerOrder;
import org.hso.ecommerce.repos.shop.CustomerOderRepository;
import org.hso.ecommerce.repos.user.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.data.jpa.repository.Query;
import org.springframework.format.datetime.DateFormatterRegistrar;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.transaction.Transaction;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
@ -28,6 +28,8 @@ public class CustomerOrderController {
@Autowired
private final CustomerOderRepository customerOrderRepository = null;
@Autowired
private final RestServiceForDelivery restServiceForDelivery = null;
@GetMapping("")
public String internCustomerOrder(Model model) {
@ -61,12 +63,21 @@ public class CustomerOrderController {
if(customerOrder.deliveredAt == null)
{
DeliveryData deliveryData = new RestServiceForDelivery(new RestTemplateBuilder()).getDeliveryData(UUID.fromString(customerOrder.trackingId));
DeliveryData deliveryData = restServiceForDelivery.getDeliveryData(UUID.fromString(customerOrder.trackingId));
if(deliveryData.isDelivered())
{
customerOrderRepository.updateUserDeliveredAt(customerOrder.id, Timestamp.valueOf(deliveryData.estimatedArrival));
System.out.println(Timestamp.valueOf(deliveryData.estimatedArrival));
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
try {
calendar.setTime(simpleDateFormat.parse(deliveryData.estimatedArrival));
} catch (ParseException e) {
e.printStackTrace();
}
customerOrderRepository.updateUserDeliveredAt(customerOrder.id, new Timestamp(calendar.getTimeInMillis()));
}
return deliveryData;
@ -78,17 +89,16 @@ public class CustomerOrderController {
public static class DeliveryData
{
@JsonProperty("status")
private String status;
@JsonProperty("estimatedArrival")
private String estimatedArrival;
private final String status;
private final String estimatedArrival;
private boolean isDelivered;
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public DeliveryData(String status, String estimatedArrival) {
this.status = status;
System.out.println(status);
this.estimatedArrival = estimatedArrival;
isDelivered = status.equals("Lieferung erfolgreich");
}

View File

@ -43,9 +43,4 @@ public class CustomerOrder {
public int totalNetCent;
public int totalGrossCent;
public int totalVatCent;
public String getEstimatedArrival() {
//TODO: get estimated arrival from api
return "TODO TODO TODO";
}
}

View File

@ -5,6 +5,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@ -19,6 +20,7 @@ public interface CustomerOderRepository extends JpaRepository<CustomerOrder, Lon
@Query("SELECT co FROM CustomerOrder co ORDER BY co.created DESC")
List<CustomerOrder> getAllOrders();
@Transactional
@Modifying(clearAutomatically = true)
@Query("UPDATE CustomerOrder co SET co.deliveredAt = :newDeliveredAt WHERE co.id = :customerOrderID")
int updateUserDeliveredAt(long customerOrderID, java.sql.Timestamp newDeliveredAt);

View File

@ -35,8 +35,11 @@
<tr>
<th>Lieferstatus</th>
<td th:text="${deliveryData.getStatus()}"></td>
<tr th:if="${order.deliveredAt == null && order.trackingId!=null}">Vorraussichtliche Ankunft: <span th:text="${deliveryData.getEstimatedArrival()}" /></tr>
<tr th:if="${order.deliveredAt != null && order.trackingId!=null}"><b>Angekommen</b> Ankunft: <span th:text="${deliveryData.getEstimatedArrival()}" /></tr>
</tr>
<tr>
<td></td>
<td th:if="${order.deliveredAt == null && order.trackingId!=null}">Vorraussichtliche Ankunft: <span th:text="${deliveryData.getEstimatedArrival()}" /></td>
<td th:if="${order.deliveredAt != null && order.trackingId!=null}"><b>Angekommen</b> Ankunft: <span th:text="${deliveryData.getEstimatedArrival()}" /></td>
</tr>
<tr>
<th>Sendeverfolgungsnummer</th>