コード例 #1
0
        public void GetHop_ByCode_Succeeded()
        {
            string given = "ABC";
            Hop    hop   = new Hop()
            {
                Code = given
            };

            _dal.Create(hop);

            hop = _dal.GetByCode(given);

            Assert.NotNull(hop);
            Assert.AreEqual(hop.Code, given);
        }
コード例 #2
0
ファイル: StaffLogic.cs プロジェクト: JoeNeu/parcel-tracker
        public bool StaffReportHop(string trackingId, string code)
        {
            try
            {
                //Get Data from DB and check if exists
                var parcelDal = _sqlRepoParcel.GetByTrackingID(trackingId);
                var hopDal    = _sqlRepoHop.GetByCode(code);
                if (parcelDal == null || hopDal == null || parcelDal.FutureHops.Count == 1 || parcelDal.FutureHops[0].Code != code)
                {
                    return(false);
                }
                //Validation
                var parcelBL = _mapper.Map <Parcel>(parcelDal);
                var hopBL    = new HopArrival()
                {
                    Code     = code,
                    DateTime = DateTime.Now
                };
                var validatorParcel     = new ParcelValidation();
                var validatorHopArrival = new HopArrivalValidation();
                var checkParcel         = validatorParcel.Validate(parcelBL);
                var checkHop            = validatorHopArrival.Validate(hopBL);
                //check if Parcel and Hop is Valid and the next Hop is the same as the incoming Hop
                if (!checkParcel.IsValid && !checkHop.IsValid)
                {
                    return(false);
                }

                //Change State
                var hopArrival = _mapper.Map <DAL.HopArrival>(hopBL);
                if (hopDal.HopType == "Truck")
                {
                    parcelDal.State = DAL.Parcel.StateEnum.InTruckDeliveryEnum;
                }
                if (hopDal.HopType == "Transferwarehouse")
                {
                    DAL.Transferwarehouse post = (DAL.Transferwarehouse)hopDal;

                    string url = $"{post.LogisticsPartnerUrl}/parcel/{trackingId}";
                    HttpResponseMessage msg = SendParcelToPartner(url, parcelBL);

                    if (msg.StatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception($"Could not POST Parcel to Partner {post.LogisticsPartnerUrl}");
                    }
                    parcelDal.State = DAL.Parcel.StateEnum.DeliveredEnum;
                }
                if (hopDal.HopType == "Warehouse")
                {
                    parcelDal.State = DAL.Parcel.StateEnum.InTransportEnum;
                }
                // Update Visited/Future Hops
                parcelDal.FutureHops.RemoveAt(0);
                parcelDal.VisitedHops.Add(hopArrival);
                _sqlRepoParcel.Update(parcelDal);

                //Contact Webhook Subscriber
                List <DAL.Webhook> contactList = _webrep.GetWebhooksByTrackingID(trackingId);
                var webhookparcel = _mapper.Map <Parcel>(parcelDal);
                foreach (var hook in contactList)
                {
                    HttpResponseMessage msg = SendWebhookResponse(hook.Url, webhookparcel);
                }
                return(true);
            }
            catch (DAL.DALException exc)
            {
                _logger.LogError(exc.ToString());
                throw new BLException($"{exc.GetType()} Exception in {System.Reflection.MethodBase.GetCurrentMethod().Name}", exc);
            }
            catch (Exception exc)
            {
                _logger.LogError(exc.ToString());
                throw new BLException($"{exc.GetType()} Exception in {System.Reflection.MethodBase.GetCurrentMethod().Name}", exc);
            }
        }
コード例 #3
0
        public void ReportParcelHop(string parcelID, string code)
        {
            Data.Parcel dataParcel;
            try
            {
                logger.LogDebug($"getting parcel {parcelID} from repo");
                dataParcel = parcelRepository.GetByTrackingId(parcelID);
            }
            catch (DataAccessLayerException e)
            {
                throw new BusinessLayerException("DAL Exception", e);
            }
            Data.Hop dataHop;
            try
            {
                logger.LogDebug($"getting hop {code} from repo");
                dataHop = hopRepository.GetByCode(code);
            }
            catch (DataAccessLayerException e)
            {
                throw new BusinessLayerException("DAL Exception", e);
            }

            Parcel businessParcel = this.mapper.Map <Parcel>(dataParcel);
            Hop    businessHop    = this.mapper.Map <Hop>(dataHop);

            businessParcel.ReportHop(businessHop);

            if (code == businessParcel.FutureHops[0].Hop.Code)
            {
                businessParcel.VisitedHops.Add(businessParcel.FutureHops[0]);
                businessParcel.FutureHops.Remove(businessParcel.FutureHops[0]);
            }

            /*
             * if (code == businessParcel.FutureHops[0].Code)
             * {
             *  switch (businessHop.)
             *  {
             *      case "Warehouse":
             *          businessParcel.State = BL.Entities.Parcel.StateEnum.InTransportEnum;
             *
             *          break;
             *      case "Truck":
             *          businessParcel.State = BL.Entities.Parcel.StateEnum.InTruckDeliveryEnum;
             *
             *          break;
             *      case "Transferwarehouse":
             *          TransferToWarehouse(businessParcel, hop);
             *          businessParcel.State = BL.Entities.Parcel.StateEnum.TransferredEnum;
             *
             *          break;
             *      default:
             *          break;
             *  }
             *
             *
             *  webHookAgent.NotifyStateChanged(dalParcel);
             *
             *  businessParcel.VisitedHops.Add(dalParcel.FutureHops[0]);
             *  businessParcel.FutureHops.Remove(dalParcel.FutureHops[0]);
             *
             *  parcelRepository.Update(dalParcel);
             *
             * }
             */



            string senderGeoString    = businessParcel.Sender.ToGeoCodingString();
            string recipientGeoString = businessParcel.Recipient.ToGeoCodingString();

            logger.LogDebug($"converting sender geoCodingString '{senderGeoString}' to Location");
            logger.LogDebug($"converting recepient geoCodingString '{recipientGeoString}' to Location");

            Geocoding.Location senderAddress    = geoCodingAgent.EncodeAddress(senderGeoString);
            Geocoding.Location recipientAddress = geoCodingAgent.EncodeAddress(recipientGeoString);

            Data.Hop dataSenderHop;
            Data.Hop dataRecipientHop;
            try
            {
                dataSenderHop    = hopRepository.GetByCoordinates(senderAddress.Latitude, senderAddress.Longitude);
                dataRecipientHop = hopRepository.GetByCoordinates(recipientAddress.Latitude, recipientAddress.Longitude);
            }
            catch (DataAccessLayerException e)
            {
                throw new BusinessLayerException("DAL Exception", e);
            }

            if (dataSenderHop == null || dataRecipientHop == null)
            {
                string errorMessage = "Error";

                NoHopException e = new NoHopException(errorMessage);
                logger.LogError(e, $"No Hop found");
                throw e;
            }

            Data.Warehouse dataWarehouse;
            try
            {
                logger.LogDebug("load full warehouse hierarchy");
                dataWarehouse = wareHouseRepository.Read();
            }
            catch (DataAccessLayerException e)
            {
                throw new BusinessLayerException("DAL Exception", e);
            }

            Hop       senderHop    = this.mapper.Map <Hop>(dataSenderHop);
            Hop       recipientHop = this.mapper.Map <Hop>(dataRecipientHop);
            Warehouse warehouse    = this.mapper.Map <Warehouse>(dataWarehouse);

            logger.LogDebug($"calculating route betweend sender {senderHop.Code} and recipeint {recipientHop.Code}");
            List <HopArrival> route = routeCalculator.CalculateRoute(warehouse, senderHop.Code, recipientHop.Code, businessParcel.EntryDate);

            bool checkIfOnRoute = false;

            foreach (HopArrival ha in route)
            {
                if (ha.Hop.Code == businessHop.Code)
                {
                    checkIfOnRoute = true;
                    break;
                }
            }

            if (!checkIfOnRoute)
            {
                throw new BusinessLayerException("Hop is not on TravelRoute");
            }

            try
            {
                logger.LogInformation($"updating parcel {parcelID}");
                parcelRepository.Update(this.mapper.Map <Data.Parcel>(businessParcel));

                List <Data.Webhook> dataWebhooks = webhookRepository.GetByTrackingId(parcelID);
                List <Webhook>      webhooks     = new List <Webhook>();
                dataWebhooks.ForEach(hook => webhooks.Add(this.mapper.Map <Webhook>(hook)));
                NotifyAllSubscribers(webhooks);
            }
            catch (DataAccessLayerException e)
            {
                throw new BusinessLayerException("DAL Exception", e);
            }
        }