This repository has been archived on 2020-08-02. You can view files and clone it, but cannot push or open issues or pull requests.
e-commerce/prototype/src/main/java/org/hso/ecommerce/controller/intern/customers/CustomerOrderController.java

110 lines
3.9 KiB
Java
Raw Normal View History

2020-04-29 22:44:16 +02:00
package org.hso.ecommerce.controller.intern.customers;
2020-04-28 22:41:29 +02:00
2020-06-10 14:51:12 +02:00
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.Session;
import org.hso.ecommerce.action.shop.RestServiceForDelivery;
2020-06-01 11:31:12 +02:00
import org.hso.ecommerce.entities.shop.CustomerOrder;
import org.hso.ecommerce.repos.shop.CustomerOderRepository;
2020-06-10 14:51:12 +02:00
import org.hso.ecommerce.repos.user.UserRepository;
2020-06-01 11:31:12 +02:00
import org.springframework.beans.factory.annotation.Autowired;
2020-06-10 14:51:12 +02:00
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.data.jpa.repository.Query;
2020-04-28 22:41:29 +02:00
import org.springframework.stereotype.Controller;
2020-06-01 11:31:12 +02:00
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
2020-06-01 11:53:29 +02:00
import org.springframework.web.bind.annotation.PathVariable;
2020-06-01 11:31:12 +02:00
import org.springframework.web.bind.annotation.RequestMapping;
2020-06-10 14:51:12 +02:00
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
2020-04-28 22:41:29 +02:00
@Controller
2020-06-01 11:31:12 +02:00
@RequestMapping("intern/customerOrders")
2020-05-01 10:48:12 +02:00
public class CustomerOrderController {
2020-06-01 11:31:12 +02:00
@Autowired
private final CustomerOderRepository customerOrderRepository = null;
@GetMapping("")
public String internCustomerOrder(Model model) {
List<CustomerOrder> orders = customerOrderRepository.getAllOrders();
2020-06-10 14:51:12 +02:00
Map<CustomerOrder, DeliveryData> customerOrderDeliveryDataMap = orders.stream().collect
(Collectors.toMap
(Function.identity(), this::getDeliveryDataFromCustomerOrder));
model.addAttribute("orderDeliveryDataMap", customerOrderDeliveryDataMap);
2020-06-01 11:31:12 +02:00
return "intern/customerOrders/index";
}
@GetMapping("/{id}")
2020-06-01 11:53:29 +02:00
public String internCustomerOrdersId(Model model,
@PathVariable("id") String id
) {
CustomerOrder order = customerOrderRepository.findById(Long.parseLong(id)).get();
2020-06-10 14:51:12 +02:00
DeliveryData deliveryData = getDeliveryDataFromCustomerOrder(order);
2020-06-01 11:53:29 +02:00
model.addAttribute("order", order);
2020-06-10 14:51:12 +02:00
model.addAttribute("deliveryData", deliveryData);
2020-06-01 11:53:29 +02:00
2020-06-01 11:31:12 +02:00
return "intern/customerOrders/id";
}
2020-06-10 14:51:12 +02:00
private DeliveryData getDeliveryDataFromCustomerOrder(CustomerOrder customerOrder)
{
if(customerOrder.trackingId == null)
return new CustomerOrderController.DeliveryData("Bestellung wurde elektronisch angekündigt","");
if(customerOrder.deliveredAt == null)
{
DeliveryData deliveryData = new RestServiceForDelivery(new RestTemplateBuilder()).getDeliveryData(UUID.fromString(customerOrder.trackingId));
if(deliveryData.isDelivered())
{
customerOrderRepository.updateUserDeliveredAt(customerOrder.id, Timestamp.valueOf(deliveryData.estimatedArrival));
System.out.println(Timestamp.valueOf(deliveryData.estimatedArrival));
}
return deliveryData;
}
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
return new CustomerOrderController.DeliveryData("Lieferung erfolgreich",formatter.format(customerOrder.deliveredAt));
}
public static class DeliveryData
{
@JsonProperty("status")
private String status;
@JsonProperty("estimatedArrival")
private String estimatedArrival;
private boolean isDelivered;
@JsonCreator(mode = JsonCreator.Mode.DELEGATING)
public DeliveryData(String status, String estimatedArrival) {
this.status = status;
System.out.println(status);
this.estimatedArrival = estimatedArrival;
isDelivered = status.equals("Lieferung erfolgreich");
}
public boolean isDelivered() {
return isDelivered;
}
public String getStatus() {
return status;
}
public String getEstimatedArrival() {
return estimatedArrival;
}
}
2020-04-28 22:41:29 +02:00
}