/// <summary> /// Add or update delivery address. /// </summary> /// <param name="email"> /// The email. /// </param> /// <param name="address"> /// The address. /// </param> /// <returns> /// The TdService.Model.Addresses.DeliveryAddress. /// </returns> public DeliveryAddress AddOrUpdateDeliveryAddress(string email, DeliveryAddress address) { if (email == null) { throw new ArgumentNullException("email"); } if (address == null) { throw new ArgumentNullException("address"); } using (var context = new ShopAnyWareSql()) { var user = context.Users.Include("DeliveryAddresses").Include("Profile").Include("Wallet").Include("Roles").SingleOrDefault(u => u.Email == email); if (user == null) { throw new InvalidUserException(ErrorCode.UserNotFound.ToString()); } address.UserId = user.Id; var addressInDb = address.Id == 0 ? null : context.DeliveryAddresses.Find(address.Id); if (addressInDb == null) { addressInDb = context.DeliveryAddresses.Add(address); context.SaveChanges(); if (user.DeliveryAddresses == null) { user.DeliveryAddresses = new List<DeliveryAddress>(); } } else { addressInDb.AddressName = address.AddressName; addressInDb.FirstName = address.FirstName; addressInDb.LastName = address.LastName; addressInDb.Phone = address.Phone; addressInDb.Region = address.Region; addressInDb.State = address.State; addressInDb.ZipCode = address.ZipCode; addressInDb.AddressLine1 = address.AddressLine1; addressInDb.AddressLine2 = address.AddressLine2; addressInDb.AddressLine3 = address.AddressLine3; addressInDb.CountryId = address.CountryId; context.Entry(addressInDb).State = EntityState.Modified; context.SaveChanges(); } addressInDb.Country = context.Countries.Single(c => c.Id.Equals(addressInDb.CountryId)); return addressInDb; } }
/// <summary> /// Remove delivery address. /// </summary> /// <param name="email"> /// The email. /// </param> /// <param name="address"> /// The address. /// </param> /// <returns> /// The TdService.Model.Addresses.DeliveryAddress. /// </returns> public DeliveryAddress RemoveDeliveryAddress(string email, DeliveryAddress address) { if (email == null) { throw new ArgumentNullException("email"); } if (address == null) { throw new ArgumentNullException("address"); } using (var context = new ShopAnyWareSql()) { var user = context.Users.Include("DeliveryAddresses").Include("Profile").Include("Wallet").Include("Roles").SingleOrDefault(u => u.Email == email); if (user == null) { throw new InvalidUserException(ErrorCode.UserNotFound.ToString()); } var addressToRemove = user.DeliveryAddresses.Find(a => a.Id == address.Id); if (addressToRemove != null) { user.DeliveryAddresses.Remove(addressToRemove); } var removedAddress = context.DeliveryAddresses.Remove(addressToRemove); context.SaveChanges(); return removedAddress; } }