private bool IsShipmentRemovable(Shipment shipment)
        {

            var latestStatus = (from shipmentState in shipment.ShipmentStates.ToList()
                                orderby shipmentState.ShipmentStatus descending
                                select shipmentState).First();

            return latestStatus.ShipmentStatus < ShipmentStatus.InTransit;
        }
        public Shipment UpdateShipment(Shipment shipment)
        {

            _shipmentRepository.Edit(shipment);
            _shipmentRepository.Save();

            // Get the shipment seperately so that 
            // we would have down level references such as ShipmentStates.
            var updatedShipment = GetShipment(shipment.Key);

            return shipment;
        }
        public OperationResult RemoveShipment(Shipment shipment)
        {

            if (IsShipmentRemovable(shipment))
            {

                _shipmentRepository.DeleteGraph(shipment);
                _shipmentRepository.Save();

                return new OperationResult(true);
            }

            return new OperationResult(false);
        }
        public OperationResult<Shipment> AddShipment(Shipment shipment)
        {

            var affiliate = _affiliateRepository.GetSingle(shipment.AffiliateKey);
            var shipmentType = _shipmentTypeRepository.GetSingle(shipment.ShipmentTypeKey);

            if (affiliate == null || shipmentType == null)
            {

                return new OperationResult<Shipment>(false);
            }

            shipment.Key = Guid.NewGuid();
            shipment.CreatedOn = DateTime.Now;

            _shipmentRepository.Add(shipment);
            _shipmentRepository.Save();

            // Add the first state for this shipment
            var shipmentState = InsertFirstShipmentState(shipment.Key);

            // Add the down level references manual so that
            // we don't have to have a trip to database to get them
            shipment.ShipmentType = shipmentType;
            shipment.ShipmentStates = new List<ShipmentState> { shipmentState };

            return new OperationResult<Shipment>(true)
            {
                Entity = shipment
            };
        }