public IList <FreightDto> GetFreight(GetFreightInputDto inputDto) { var weightShoppingCart = calculateWeightShoppingCart(inputDto); var tenantExpeditions = _tenantExpeditionRepo.GetAllList(i => i.IsActive); var originDistrict = GetCurrentTenant().District; var destinationDistrict = _districtRepo.FirstOrDefault(i => i.Id == inputDto.DistrictId); var activeExpeditionServices = tenantExpeditions.Select(i => i.ExpeditionService).ToList(); var activeExpeditions = new List <Expedition>(); activeExpeditionServices.ForEach(i => { if (!activeExpeditions.Any(activeExpedition => activeExpedition.Id == i.Expedition.Id)) { activeExpeditions.Add(i.Expedition); } }); var getShippingCostInputDto = new GetShippingCostInputDto { Expeditions = activeExpeditions, Origin = originDistrict, Destination = destinationDistrict, Weight = weightShoppingCart }; IList <FreightDto> result = new List <FreightDto>(); var rajaOngkirResponses = _rajaOngkirService.GetShippingCost(getShippingCostInputDto); foreach (var rajaOngkirResponse in rajaOngkirResponses) { foreach (var rajaOngkirResponseResult in rajaOngkirResponse.Results) { if (activeExpeditionServices.Any(i => i.Expedition.RajaOngkirCode.ToLower() == rajaOngkirResponse.Code.ToLower() && i.RajaOngkirCode.ToLower() == rajaOngkirResponseResult.ServiceName.ToLower())) { var expeditionService = activeExpeditionServices.FirstOrDefault(i => i.Expedition.RajaOngkirCode.ToLower() == rajaOngkirResponse.Code.ToLower() && i.RajaOngkirCode.ToLower() == rajaOngkirResponseResult.ServiceName.ToLower()); var estimatedTimeDelivery = new EstimatedTimeDelivery(rajaOngkirResponseResult.EstimatedToDelivery); var freightDto = new FreightDto() { ExpeditionServiceId = expeditionService.Id, ExpeditionFullName = expeditionService.FullName, Cost = rajaOngkirResponseResult.Cost, EstimatedTimeDelivery = estimatedTimeDelivery, Description = estimatedTimeDelivery.GetEstimatedTimeDeliverySentence(DateTime.Now), TotalWeight = weightShoppingCart, ExpeditionServiceGroupName = expeditionService.GroupName }; result.Add(freightDto); } } } return(result); }
public FreightDto GetFreightByExpeditionService(GetFreightByServiceInputDto inputDto) { var tenantExpedition = _tenantExpeditionRepo.FirstOrDefault(i => i.ExpeditionService.Id == inputDto.ExpeditionService.Expedition.Id); Validate.Found(tenantExpedition, "Expedition"); if (!tenantExpedition.IsActive) { throw new HozaruException("Expedition tidak aktif"); } var getShippingCostInputDto = new GetShippingCostInputDto { Expeditions = new List <Expedition>() { inputDto.ExpeditionService.Expedition }, Origin = inputDto.Origin, Destination = inputDto.Destination, Weight = inputDto.Weight }; var rajaOngkirResponses = _rajaOngkirService.GetShippingCost(getShippingCostInputDto); if (rajaOngkirResponses.IsNullOrEmpty()) { throw new HozaruException("Ongkos Kirim tidak ditemukan"); } var rajaOngkirResponseResult = rajaOngkirResponses.FirstOrDefault().Results.FirstOrDefault(i => i.ServiceName.ToLower() == inputDto.ExpeditionService.RajaOngkirCode.ToLower()); var estimatedTimeDelivery = new EstimatedTimeDelivery(rajaOngkirResponseResult.EstimatedToDelivery); var freightDto = new FreightDto() { ExpeditionServiceId = inputDto.ExpeditionService.Id, ExpeditionFullName = inputDto.ExpeditionService.FullName, Cost = rajaOngkirResponseResult.Cost, EstimatedTimeDelivery = estimatedTimeDelivery, Description = estimatedTimeDelivery.GetEstimatedTimeDeliverySentence(DateTime.Now), TotalWeight = inputDto.Weight }; return(freightDto); }
public IList <ApiRajaOngkirShippingCostResponseDto> GetShippingCost(GetShippingCostInputDto inputDto) { using (var client = new HttpClient()) { var url = AppSettingConfigurationHelper.GetSection("APIUrlRajaOngkir").Value; var apiKey = AppSettingConfigurationHelper.GetSection("APIKeyRajaOngkir").Value; client.BaseAddress = new Uri(url); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("key", apiKey); var couriers = inputDto.Expeditions.Select(i => i.Code.ToLower()).JoinAsString(":"); var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("origin", inputDto.Origin.IdRajaOngkir.ToString()), new KeyValuePair <string, string>("originType", "subdistrict"), new KeyValuePair <string, string>("destination", inputDto.Destination.IdRajaOngkir.ToString()), new KeyValuePair <string, string>("destinationType", "subdistrict"), new KeyValuePair <string, string>("weight", (inputDto.Weight + 100).ToString()), new KeyValuePair <string, string>("courier", couriers) }); HttpResponseMessage response = client.PostAsync("cost", content).Result; var resultString = AsyncHelper.RunSync(() => response.Content.ReadAsStringAsync()); JToken token = JObject.Parse(resultString); if ((string)token.SelectToken("rajaongkir").SelectToken("status").SelectToken("description") != HttpStatusCode.OK.ToString()) { throw new HozaruException("Ongkos Kirim tidak ditemukan."); } var result = token.SelectToken("rajaongkir").SelectToken("results"); return(result.ToObject <IList <ApiRajaOngkirShippingCostResponseDto> >()); } }