fix delivery server crashing when tracking id not found
This commit is contained in:
		@ -7,7 +7,6 @@ import org.hso.ecommerce.supplier.data.ReturnStatus;
 | 
			
		||||
import org.springframework.http.MediaType;
 | 
			
		||||
import org.springframework.web.bind.annotation.*;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
import javax.servlet.http.HttpServletRequest;
 | 
			
		||||
import javax.servlet.http.HttpServletResponse;
 | 
			
		||||
 | 
			
		||||
@ -20,14 +19,19 @@ public class RequestController {
 | 
			
		||||
    public String supplier(HttpServletResponse response, HttpServletRequest request, @RequestBody Delivery delivery) {
 | 
			
		||||
        DeliveryManager.getInstance().add(delivery);
 | 
			
		||||
 | 
			
		||||
        return delivery.getUuid().toString();
 | 
			
		||||
        return delivery.getUuid();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @GetMapping(value = "/status", produces = MediaType.APPLICATION_JSON_VALUE)
 | 
			
		||||
    public ReturnStatus searchArticles(@RequestParam(value = "trackingID") String trackingID, HttpServletRequest request, HttpServletResponse response) {
 | 
			
		||||
 | 
			
		||||
        Delivery delivery = DeliveryManager.getInstance().getDeliveryByeID(trackingID);
 | 
			
		||||
        Delivery delivery = DeliveryManager.getInstance().getDeliveryByID(trackingID);
 | 
			
		||||
        if (delivery == null) {
 | 
			
		||||
            Delivery lostDelivery = Delivery.lostDelivery(trackingID);
 | 
			
		||||
            DeliveryManager.getInstance().add(lostDelivery);
 | 
			
		||||
            delivery = lostDelivery;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return new ReturnStatus(delivery.getStatus(),delivery.getEstimatedArrival());
 | 
			
		||||
        return new ReturnStatus(delivery.getStatus(), delivery.getEstimatedArrival());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -15,31 +15,34 @@ public class Delivery {
 | 
			
		||||
    private String address;
 | 
			
		||||
    private String estimatedArrival;
 | 
			
		||||
    private Date creationTime;
 | 
			
		||||
    private UUID uuid;
 | 
			
		||||
    private String uuid;
 | 
			
		||||
 | 
			
		||||
    public Delivery(String name, String address)
 | 
			
		||||
    {
 | 
			
		||||
    public Delivery(String name, String address) {
 | 
			
		||||
        this.name = name;
 | 
			
		||||
        this.address = address;
 | 
			
		||||
        this.uuid = UUID.randomUUID();
 | 
			
		||||
        this.uuid = UUID.randomUUID().toString();
 | 
			
		||||
        this.creationTime = new Date();
 | 
			
		||||
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
 | 
			
		||||
        this.estimatedArrival = formatter.format(addDays((Date)this.creationTime.clone(),1));
 | 
			
		||||
        this.estimatedArrival = formatter.format(addDays((Date) this.creationTime.clone(), 1));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static Delivery lostDelivery(String uuid) {
 | 
			
		||||
        Delivery delivery = new Delivery("", "");
 | 
			
		||||
        delivery.uuid = uuid;
 | 
			
		||||
        return delivery;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getStatus()
 | 
			
		||||
    {
 | 
			
		||||
    public String getStatus() {
 | 
			
		||||
        Date now = new Date();
 | 
			
		||||
        Long timeNow = now.getTime();
 | 
			
		||||
        Long creationTime = this.creationTime.getTime();
 | 
			
		||||
        long timeNow = now.getTime();
 | 
			
		||||
        long creationTime = this.creationTime.getTime();
 | 
			
		||||
 | 
			
		||||
        Long diff = timeNow - creationTime;
 | 
			
		||||
        long diff = timeNow - creationTime;
 | 
			
		||||
        double hour = (((diff / 1000.0) / 3600.0));
 | 
			
		||||
 | 
			
		||||
        for (int i = 0; i <  timeBorder.length; i++) {
 | 
			
		||||
        for (int i = 0; i < timeBorder.length; i++) {
 | 
			
		||||
 | 
			
		||||
            if(hour < timeBorder[i])
 | 
			
		||||
            if (hour < timeBorder[i])
 | 
			
		||||
                return states[i];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -59,7 +62,7 @@ public class Delivery {
 | 
			
		||||
        return cal.getTime();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public UUID getUuid() {
 | 
			
		||||
    public String getUuid() {
 | 
			
		||||
        return uuid;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,20 +1,18 @@
 | 
			
		||||
package org.hso.ecommerce.supplier.data;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
 | 
			
		||||
public class DeliveryManager {
 | 
			
		||||
 | 
			
		||||
    private List<Delivery> deliveryList;
 | 
			
		||||
    private HashMap<String, Delivery> deliveryList;
 | 
			
		||||
    private static DeliveryManager deliveryManager;
 | 
			
		||||
 | 
			
		||||
    private DeliveryManager()
 | 
			
		||||
    {
 | 
			
		||||
        deliveryList = new ArrayList<>();
 | 
			
		||||
        deliveryList = new HashMap<>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static DeliveryManager getInstance () {
 | 
			
		||||
    public static DeliveryManager getInstance() {
 | 
			
		||||
 | 
			
		||||
        if (DeliveryManager.deliveryManager == null) {
 | 
			
		||||
            DeliveryManager.deliveryManager = new DeliveryManager();
 | 
			
		||||
@ -22,13 +20,11 @@ public class DeliveryManager {
 | 
			
		||||
        return DeliveryManager.deliveryManager;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean add(Delivery delivery)
 | 
			
		||||
    {
 | 
			
		||||
       return deliveryList.add(delivery);
 | 
			
		||||
    public void add(Delivery delivery) {
 | 
			
		||||
        deliveryList.put(delivery.getUuid(), delivery);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Delivery getDeliveryByeID(String uuid)
 | 
			
		||||
    {
 | 
			
		||||
        return deliveryList.parallelStream().filter(d -> d.getUuid().equals(UUID.fromString(uuid))).findAny().get();
 | 
			
		||||
    public Delivery getDeliveryByID(String uuid) {
 | 
			
		||||
        return deliveryList.getOrDefault(uuid, null);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user