public static double CalculateDistance(Guid shoppingCartId, string address, double latitude, double longitude) { List <Distance> distances = new List <Distance>(); ShoppingCartShippingAddress cartAddress = new ShoppingCartShippingAddress(); cartAddress.Address = address; cartAddress.AddressLatitude = latitude; cartAddress.AddressLongitude = longitude; using (var db = new CoreEcommerceDbContext()) { distances = db.ShoppingCartItems.Where(i => i.ShoppingCartId == shoppingCartId) .Join(db.RelationShips, ci => ci.ProductId, r => r.FromId, (ci, r) => new { Ci = ci, R = r }) .Join(db.Suppliers, m => m.R.ToId, s => s.Id, (m, s) => new { S = s }) .Select(i => new Distance() { SupplierLat = i.S.AddressLatitude, SupplierLng = i.S.AddressLongitude }).ToList(); } foreach (var ds in distances) { ds.CartLat = cartAddress.AddressLatitude; ds.CartLng = cartAddress.AddressLongitude; ds.DistanceInKm = GeoExtensions.GetDistanceFromLatLonInKm(ds.CartLat, ds.CartLng, ds.SupplierLat, ds.SupplierLng); } if (distances.Count == 0) { return(0); } return(Math.Round(distances.Max(i => i.DistanceInKm), 2)); }
public void Handle(ShoppingCartShippingAddressCreated e) { using (var db = new CoreEcommerceDbContext()) { var tempA = db.ShoppingCartShippingAddresses.FirstOrDefault(i => i.ShoppingCartId == e.Id); if (tempA == null) { var entity = new ShoppingCartShippingAddress(); entity.Id = Guid.NewGuid(); entity.ShoppingCartId = e.Id; entity.AddressName = e.AddressName; entity.Address = e.Address; entity.AddressLatitude = e.AddressLatitude; entity.AddressLongitude = e.AddressLongitude; entity.Phone = e.Phone; entity.Email = e.Email; entity.Message = e.Message; entity.CreatedDate = DateTime.Now; db.ShoppingCartShippingAddresses.Add(entity); } else { tempA.AddressName = e.AddressName; tempA.Address = e.Address; tempA.AddressLatitude = e.AddressLatitude; tempA.AddressLongitude = e.AddressLongitude; tempA.Phone = e.Phone; tempA.Email = e.Email; tempA.Message = e.Message; tempA.CreatedDate = DateTime.Now; } var temp = db.ShoppingCarts.SingleOrDefault(i => i.Id == e.Id); if (temp != null) { temp.ShippingMethodId = e.ShippingMethodId; } db.SaveChanges(); } }