private Location CreateFromModel(AddEditLocationModel model, Int32 providerId) { Location location = model.ToEntity(db); location.ProviderId = providerId; location.RecordStatusId = (Int32)Constants.RecordStatus.Live; Address locationAddress = model.Address.ToEntity(db); location.Address = locationAddress; if (locationAddress.Latitude.HasValue) { db.Addresses.Add(locationAddress); db.Locations.Add(location); db.SaveChanges(); } return(location); }
public async Task <ActionResult> Edit(Int32 id, AddEditLocationModel model) { Provider provider = db.Providers.Find(userContext.ItemId); if (provider == null) { return(HttpNotFound()); } if (model.LocationId != id) { return(HttpNotFound()); } Location location = db.Locations.Find(id); if (location == null) { return(HttpNotFound()); } Location loc = db.Locations.FirstOrDefault(x => x.LocationId != location.LocationId && x.LocationName == model.LocationName && x.ProviderId == provider.ProviderId); if (loc != null) { ModelState.AddModelError("LocationName", AppGlobal.Language.GetText("Location_Edit_DuplicateLocatioName", "The Location Name supplied is already in use.")); } Address address = model.Address.ToEntity(db); Location locDupPostcode = db.Locations.FirstOrDefault(x => x.RecordStatusId == (int)Constants.RecordStatus.Live && x.Address.Postcode == address.Postcode && x.ProviderId == provider.ProviderId && x.LocationId != model.LocationId); if (locDupPostcode != null) { ModelState.AddModelError("Address.Postcode", AppGlobal.Language.GetText("Location_Create_DuplicatePostcode", "The postcode supplied is already in use.")); } if (ModelState.IsValid) { location = model.ToEntity(db); if (location.ProviderId != userContext.ItemId) { // User is trying to change the LocationId (or the context has changed) return(HttpNotFound()); } location.ModifiedByUserId = Permission.GetCurrentUserId(); location.ModifiedDateTimeUtc = DateTime.UtcNow; Address locationAddress = model.Address.ToEntity(db); if (!locationAddress.Latitude.HasValue) { ModelState.AddModelError("Address.Postcode", unableToFindLatLngMessage); } else { db.Entry(locationAddress).State = EntityState.Modified; db.Entry(location).State = EntityState.Modified; await db.SaveChangesAsync(); List <String> messages = model.GetWarningMessages(); if (messages.Count == 0) { ShowGenericSavedMessage(); } else { // Add a blank entry at the beginning so the String.Join starts with <br /><br /> messages.Insert(0, ""); SessionMessage.SetMessage(AppGlobal.Language.GetText(this, "SaveSuccessfulWithWarnings", "Your changes were saved successfully with the following warnings:") + String.Join("<br /><br />", messages), SessionMessageType.Success); } return(RedirectToAction("List")); } } // Populate drop downs GetLookups(model); return(View(model)); }