impl trackingID for the UserController

This commit is contained in:
Philipp Schweizer 2020-06-13 21:59:18 +02:00
parent 1dd4b5cfa5
commit 0ab185f143
7 changed files with 115 additions and 92 deletions

View File

@ -1,7 +1,7 @@
package org.hso.ecommerce.api;
import org.hso.ecommerce.controller.intern.customers.CustomerOrderController;
import org.hso.ecommerce.entities.shop.CustomerOrder;
import org.hso.ecommerce.uiModel.DeliveryData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.*;
@ -46,7 +46,7 @@ public class RestServiceForDelivery {
}
}
public CustomerOrderController.DeliveryData getDeliveryData(UUID trackingID) {
public DeliveryData getDeliveryData(UUID trackingID) {
String url = "http://[::1]:8082/status";
@ -59,21 +59,21 @@ public class RestServiceForDelivery {
HttpEntity<?> entity = new HttpEntity<>(headers);
ResponseEntity<CustomerOrderController.DeliveryData> response;
ResponseEntity<DeliveryData> response;
try
{
response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, CustomerOrderController.DeliveryData.class);
response = restTemplate.exchange(builder.toUriString(), HttpMethod.GET, entity, DeliveryData.class);
if (response.getStatusCode() == HttpStatus.OK)
{
return response.getBody();
} else {
return new CustomerOrderController.DeliveryData("Postserver gerade nicht erreichbar","--:--:----");
return new DeliveryData("DHL-Server ist gerade nicht erreichbar","--:--:----");
}
}
catch (ResourceAccessException e)
{
return new CustomerOrderController.DeliveryData("Postserver gerade nicht erreichbar","--:--:----");
return new DeliveryData("DHL-Server gerade nicht erreichbar","--:--:----");
}
}

View File

@ -1,10 +1,12 @@
package org.hso.ecommerce.controller;
import org.hso.ecommerce.action.user.UpdateUserSettingsAction;
import org.hso.ecommerce.api.RestServiceForDelivery;
import org.hso.ecommerce.entities.shop.CustomerOrder;
import org.hso.ecommerce.entities.user.User;
import org.hso.ecommerce.repos.shop.CustomerOrderRepository;
import org.hso.ecommerce.repos.user.UserRepository;
import org.hso.ecommerce.uiModel.DeliveryData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
@ -16,6 +18,9 @@ import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Controller
@RequestMapping("/user")
@ -27,6 +32,9 @@ public class UserController {
@Autowired
private final CustomerOrderRepository customerOrderRepository = null;
@Autowired
private final RestServiceForDelivery restServiceForDelivery = null;
@GetMapping("/")
public String user() {
return "redirect:/user/settings";
@ -44,11 +52,16 @@ public class UserController {
}
@GetMapping("/orders/")
public String userOrdeers(HttpSession session,
public String userOrders(HttpSession session,
Model model
) {
List<CustomerOrder> orders = customerOrderRepository.getOrdersByUserId((long) session.getAttribute("userId"));
model.addAttribute("orders", orders);
List<CustomerOrder> orders = customerOrderRepository.getAllOrders();
Map<CustomerOrder, DeliveryData> customerOrderDeliveryDataMap = orders.stream().collect
(Collectors.toMap
(Function.identity(), c -> DeliveryData.getDeliveryDataFromCustomerOrder(c,customerOrderRepository, restServiceForDelivery)));
model.addAttribute("orderDeliveryDataMap", customerOrderDeliveryDataMap);
return "user/orders/index";
}

View File

@ -1,9 +1,9 @@
package org.hso.ecommerce.controller.intern.customers;
import com.fasterxml.jackson.annotation.JsonCreator;
import org.hso.ecommerce.api.RestServiceForDelivery;
import org.hso.ecommerce.entities.shop.CustomerOrder;
import org.hso.ecommerce.repos.shop.CustomerOrderRepository;
import org.hso.ecommerce.uiModel.DeliveryData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
@ -11,9 +11,6 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -33,7 +30,7 @@ public class CustomerOrderController {
Map<CustomerOrder, DeliveryData> customerOrderDeliveryDataMap = orders.stream().collect
(Collectors.toMap
(Function.identity(), this::getDeliveryDataFromCustomerOrder));
(Function.identity(), c-> DeliveryData.getDeliveryDataFromCustomerOrder(c,customerOrderRepository,restServiceForDelivery)));
model.addAttribute("orderDeliveryDataMap", customerOrderDeliveryDataMap);
return "intern/customerOrders/index";
@ -44,73 +41,11 @@ public class CustomerOrderController {
@PathVariable("id") String id
) {
CustomerOrder order = customerOrderRepository.findById(Long.parseLong(id)).get();
DeliveryData deliveryData = getDeliveryDataFromCustomerOrder(order);
DeliveryData deliveryData = DeliveryData.getDeliveryDataFromCustomerOrder(order, customerOrderRepository,restServiceForDelivery);
model.addAttribute("order", order);
model.addAttribute("deliveryData", deliveryData);
return "intern/customerOrders/id";
}
private DeliveryData getDeliveryDataFromCustomerOrder(CustomerOrder customerOrder)
{
if(customerOrder.trackingId == null)
return new CustomerOrderController.DeliveryData("Bestellung wurde elektronisch angekündigt","");
if(customerOrder.deliveredAt == null)
{
DeliveryData deliveryData = restServiceForDelivery.getDeliveryData(UUID.fromString(customerOrder.trackingId));
if(deliveryData.isDelivered())
{
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
try {
calendar.setTime(simpleDateFormat.parse(deliveryData.estimatedArrival));
} catch (ParseException e) {
e.printStackTrace();
}
customerOrder.deliveredAt = new Timestamp(calendar.getTimeInMillis());
customerOrderRepository.save(customerOrder);
}
return deliveryData;
}
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
return new CustomerOrderController.DeliveryData("Lieferung erfolgreich",formatter.format(customerOrder.deliveredAt));
}
public static class DeliveryData
{
private final String status;
private final String estimatedArrival;
private boolean isDelivered;
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public DeliveryData(String status, String estimatedArrival) {
this.status = status;
this.estimatedArrival = estimatedArrival;
isDelivered = status.equals("Lieferung erfolgreich");
}
public boolean isDelivered() {
return isDelivered;
}
public String getStatus() {
return status;
}
public String getEstimatedArrival() {
return estimatedArrival;
}
}
}

View File

@ -118,3 +118,4 @@ public class CustomersIndexController {
return "/intern/customers/id";
}
}

View File

@ -56,9 +56,4 @@ public class CustomerOrder {
public String formatDeliveredAt(){
return new SimpleDateFormat("dd.MM.yyyy HH:mm").format(deliveredAt);
}
public String getEstimatedArrival() {
//TODO: get estimated arrival from api
return "TODO TODO TODO";
}
}

View File

@ -0,0 +1,75 @@
package org.hso.ecommerce.uiModel;
import com.fasterxml.jackson.annotation.JsonCreator;
import org.hso.ecommerce.api.RestServiceForDelivery;
import org.hso.ecommerce.entities.shop.CustomerOrder;
import org.hso.ecommerce.repos.shop.CustomerOrderRepository;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;
public class DeliveryData
{
private final String status;
private final String estimatedArrival;
private boolean isDelivered;
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public DeliveryData(String status, String estimatedArrival) {
this.status = status;
this.estimatedArrival = estimatedArrival;
isDelivered = status.equals("Lieferung erfolgreich");
}
public static DeliveryData getDeliveryDataFromCustomerOrder(CustomerOrder customerOrder, CustomerOrderRepository customerOrderRepository, RestServiceForDelivery restServiceForDelivery)
{
if(customerOrder.trackingId == null)
return new DeliveryData("Bestellung wurde elektronisch angekündigt","");
if(customerOrder.deliveredAt == null)
{
DeliveryData deliveryData = restServiceForDelivery.getDeliveryData(UUID.fromString(customerOrder.trackingId));
if(deliveryData.isDelivered())
{
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
try {
calendar.setTime(simpleDateFormat.parse(deliveryData.getEstimatedArrival()));
} catch (ParseException e) {
e.printStackTrace();
}
customerOrder.deliveredAt = new Timestamp(calendar.getTimeInMillis());
customerOrderRepository.save(customerOrder);
}
return deliveryData;
}
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
return new DeliveryData("Lieferung erfolgreich",formatter.format(customerOrder.deliveredAt));
}
public boolean isDelivered() {
return isDelivered;
}
public String getStatus() {
return status;
}
public String getEstimatedArrival() {
return estimatedArrival;
}
}

View File

@ -21,22 +21,26 @@
<main class="sidebar-layout content-width">
<nav th:replace="fragments/customer :: sidebar"></nav>
<div class="content-width detailflex">
<div th:each="order: ${orders}">
<h2 id="20202701" th:text="|Bestellung vom ${order.formatCreated()}" />
<div th:each="order: ${orderDeliveryDataMap}">
<h2 id="20202701" th:text="|Bestellung vom ${order.getKey().formatCreated()}" />
<div>
<table class="key-value">
<tr>
<th>Lieferstatus</th>
<td th:if="${order.deliveredAt == null}"><b>Unterwegs</b> <br/> Vorraussichtliche Ankunft: <span th:text="${order.getEstimatedArrival()}" /></td>
<td th:if="${order.deliveredAt != null}"><b>Angekommen</b> <br/> Ankunft: <span th:text="${order.formatDeliveredAt()}" /></td>
<td th:text="${order.getValue().getStatus()}"></td>
</tr>
<tr>
<td></td>
<td th:if="${order.getKey().deliveredAt == null && order.getKey().trackingId!=null}">Vorraussichtliche Ankunft: <span th:text="${order.getValue().getEstimatedArrival()}" /></td>
<td th:if="${order.getKey().deliveredAt != null && order.getKey().trackingId!=null}"><b>Angekommen</b> Ankunft: <span th:text="${order.getValue().getEstimatedArrival()}" /></td>
</tr>
<tr>
<th>Sendeverfolgungsnummer</th>
<td th:text="${order.trackingId}"></td>
<td th:text="${order.getKey().trackingId!=null} ? ${order.getKey().trackingId} : 'Es wurde noch keine Sendungsnummer vergeben'"></td>
</tr>
<tr>
<th></th>
<td th:text="${order.destination.toString()}" />
<td th:text="${order.getKey().destination.toString()}" />
</tr>
</table>
</div>
@ -47,7 +51,7 @@
<th>Menge</th>
<th>Preis (Brutto)</th>
</tr>
<tr th:each="position: ${order.positions}">
<tr th:each="position: ${order.getKey().positions}">
<td><a th:href="@{/shop/articles/{id}(id = ${position.article.id})}"><img th:src="@{/shop/articles/{id}/image.jpg(id=${position.article.id})}" class="s"/></a></td>
<td><a th:href="@{/shop/articles/{id}(id = ${position.article.id})}" th:text="${position.article.title}" class="s"></a></td>
<td th:text="${position.quantity}" />
@ -63,13 +67,13 @@
<td></td>
<td></td>
<td>Artikel (Netto)</td>
<td th:text="${#numbers.formatDecimal(order.totalNetCent * 0.01, 1, 'POINT', 2, 'COMMA')}" />
<td th:text="${#numbers.formatDecimal(order.getKey().totalNetCent * 0.01, 1, 'POINT', 2, 'COMMA')}" />
</tr>
<tr>
<td></td>
<td></td>
<td>Umsatzsteuer</td>
<td th:text="${#numbers.formatDecimal(order.totalVatCent * 0.01, 1, 'POINT', 2, 'COMMA')}" />
<td th:text="${#numbers.formatDecimal(order.getKey().totalVatCent * 0.01, 1, 'POINT', 2, 'COMMA')}" />
</tr>
<tr>
<td></td>
@ -78,7 +82,7 @@
<h3>Gesammtpreis</h3>
</td>
<td>
<h3 th:text="${#numbers.formatDecimal(order.totalGrossCent * 0.01, 1, 'POINT', 2, 'COMMA')}"/>
<h3 th:text="${#numbers.formatDecimal(order.getKey().totalGrossCent * 0.01, 1, 'POINT', 2, 'COMMA')}"/>
</td>
</tr>
</table>