private void AssertRequest(LoadshopSmartSpotPriceRequest request) { if (request == null) { throw new ValidationException($"{ERROR_TYPE}: Request is null"); } if (request.LoadId == Guid.Empty) { throw new ValidationException($"{ERROR_TYPE}: {nameof(request.LoadId)} is required"); } if (string.IsNullOrWhiteSpace(request.Commodity)) { throw new ValidationException($"{ERROR_TYPE}: {nameof(request.Commodity)} is required"); } if (string.IsNullOrWhiteSpace(request.EquipmentId)) { throw new ValidationException($"{ERROR_TYPE}: {nameof(request.EquipmentId)} is required"); } }
private async Task <AWSSmartSpotPriceRequest> MapFromLoadshopSmartSpotPriceRequestAsync(LoadshopSmartSpotPriceRequest request) { AssertRequest(request); var aws = new AWSSmartSpotPriceRequest(); var load = await _db.Loads.AsNoTracking() .Include(x => x.LoadStops) .Include(x => x.CarrierScacs) .SingleOrDefaultAsync(x => x.LoadId == request.LoadId); if (load == null) { throw new Exception($"Unable to find load with ID {request.LoadId}."); } var origin = load.LoadStops.FirstOrDefault(x => x.StopNbr == 1); if (origin == null) { throw new Exception("Unable to find origin pickup stop"); } var dest = load.LoadStops.FirstOrDefault(x => x.StopNbr == load.Stops); if (dest == null) { throw new Exception("Unable to find destination delivery stop"); } aws.LoadId = request.LoadId; aws.TransactionCreate = DateTime.Now; aws.TransactionTypeId = load.LatestTransactionTypeId; aws.LoadShopMiles = load.Miles; aws.DirectMiles = load.DirectMiles; aws.Stops = load.LoadStops.Count; aws.Weight = request.Weight; aws.Commodity = request.Commodity; aws.EquipmentId = request.EquipmentId; aws.PkupDate = origin.LateDtTm; aws.OrigState = origin.State; aws.OriginZip = origin.PostalCode; aws.O3Zip = (origin.PostalCode != null && origin.PostalCode.Length >= 3) ? origin.PostalCode.Substring(0, 3) : null; aws.DestState = dest.State; aws.DestZip = dest.PostalCode; aws.D3Zip = (dest.PostalCode != null && dest.PostalCode.Length >= 3) ? dest.PostalCode.Substring(0, 3) : null; var carrierScacs = _db.CarrierScacs.Where(x => request.CarrierIds.Contains(x.CarrierId)).Select(x => x.Scac).Distinct().ToList(); aws.NbrSCACsRequest = carrierScacs.Count; aws.NbrCarriersRequest = request.CarrierIds.Count; aws.NbrSCACsPosted = load.CarrierScacs .Where(x => x.ContractRate == null || x.ContractRate.Value <= load.LineHaulRate) .Select(x => x.Scac) .Distinct() .Count(); aws.NbrContractSCACsPosted = load.CarrierScacs .Where(x => x.ContractRate != null && x.ContractRate.Value <= load.LineHaulRate) .Select(x => x.Scac) .Distinct() .Count(); aws.NbrSCACsHidden = load.CarrierScacs .Where(x => x.ContractRate != null && x.ContractRate.Value > load.LineHaulRate) .Select(x => x.Scac) .Distinct() .Count(); return(aws); }