public async Task <ActionResult> ProcessStores(bool forceGeocode = false, bool ESIndex = true) { var storesToProcess = await _db.Stores .Include(s => s.ClientStores) .ThenInclude(cs => cs.Client) .Include(s => s.Retailer) .ToListAsync(); if (ESIndex) { _esStoreClient.DeleteIndex(); } foreach (var storeToProcess in storesToProcess) { if (forceGeocode || storeToProcess.Latitude == 0 || storeToProcess.Latitude == null) { GoogleGeocoding_Location location = GeneralPurpose.GetLatLong(storeToProcess.Address); storeToProcess.Latitude = location.lat; storeToProcess.Longitude = location.lng; _db.Attach(storeToProcess).State = EntityState.Modified; } if (ESIndex) { _esStoreClient.CreateAsAsync(storeToProcess); } } await _db.SaveChangesAsync(); return(Ok()); }
public async Task <IActionResult> Update(Store_AddEditModel model) { if (!ModelState.IsValid) { model.ClientRetailers = await _db.ClientRetailers .Where(q => q.ClientId == _client_id) .Select(s => s.Retailer) .ToListAsync(); View("Edit", model); } Store storeToUpdate = await _db.Stores .Include(i => i.ClientStores) .FirstOrDefaultAsync(q => q.Id == model.Id); if (storeToUpdate == null) { return(NotFound()); } string addr_cmp_old = storeToUpdate.Address.ToUpper(); string addr_cmp_new = model.Address.ToUpper(); storeToUpdate.Name = model.Name; storeToUpdate.Addr_Ln_1 = model.Addr_Ln_1; storeToUpdate.Addr_Ln_2 = model.Addr_Ln_2; storeToUpdate.City = model.City; storeToUpdate.State = model.State; storeToUpdate.Zip = model.Zip; storeToUpdate.Phone = model.Phone; if (!String.IsNullOrEmpty(model.NewRetailer.Name)) { Retailer newRetailer = new Retailer { Name = model.NewRetailer.Name, ClientRetailers = new List <ClientRetailer> { new ClientRetailer { ClientId = _client_id } } }; _db.Retailers.Add(newRetailer); storeToUpdate.Retailer = newRetailer; } else { storeToUpdate.RetailerId = (Guid)model.RetailerId; } ClientStore clientStore = storeToUpdate.ClientStores.FirstOrDefault(q => q.ClientId == _client_id); Dictionary <string, object> props = new Dictionary <string, object>(); string[] keysToUpdate = new string[] { "MaxOrderAmount", "LocationNumber" }; // Copy JSON data that is not relevant to this action as-is try { foreach (var key in clientStore.Properties.Object.Keys.Where(q => !keysToUpdate.Contains(q))) { props.Add(key, clientStore.Properties.Object[key]); } } catch { props = new Dictionary <string, object>(); } // Update JSON data that is relevant to this action // Only update the ones that are set to non-null values // This logic will drop the properties that don't have values foreach (string keyToUpdate in keysToUpdate) { try { if (model.GetType().GetProperty(keyToUpdate).GetValue(model, null) != null) { props.Add(keyToUpdate, model.GetType().GetProperty(keyToUpdate).GetValue(model, null)); } } catch { continue; } } // Finally associate the Properties to the // client-store combo clientStore.Properties = new JsonObject <Dictionary <string, object> > { Object = props }; _db.Attach(clientStore).State = EntityState.Modified; // Only Geocode if the address has changed if (addr_cmp_new != addr_cmp_old || storeToUpdate.Latitude == null || storeToUpdate.Latitude == 0) { GoogleGeocoding_Location location = GeneralPurpose.GetLatLong(addr_cmp_new); storeToUpdate.Latitude = location.lat; storeToUpdate.Longitude = location.lng; } _db.Attach(storeToUpdate).State = EntityState.Modified; try { await _db.SaveChangesAsync(); } catch (Exception) { model.ClientRetailers = await _db.ClientRetailers .Where(q => q.ClientId == _client_id) .Select(s => s.Retailer) .ToListAsync(); View("Edit", model); } // Reindex with ES IndexStoreWithES(storeToUpdate.Id); return(RedirectToAction("Details", new { id = model.Id })); }
public async Task <IActionResult> Create(Store_AddEditModel model) { if (!ModelState.IsValid) { model.ClientRetailers = await _db.ClientRetailers .Where(q => q.ClientId == _client_id) .Select(s => s.Retailer) .ToListAsync(); return(View("New", model)); } ClientStore clientStore = new ClientStore { ClientId = _client_id, Store = new Store { Name = model.Name, Active = true, Addr_Ln_1 = model.Addr_Ln_1, Addr_Ln_2 = model.Addr_Ln_2, City = model.City, State = model.State, Zip = model.Zip, Country = "USA", Phone = model.Phone }, Active = true }; if (!String.IsNullOrEmpty(model.NewRetailer.Name)) { Retailer newRetailer = new Retailer { Name = model.NewRetailer.Name, ClientRetailers = new List <ClientRetailer> { new ClientRetailer { ClientId = _client_id } } }; _db.Retailers.Add(newRetailer); clientStore.Store.Retailer = newRetailer; } else { clientStore.Store.RetailerId = model.RetailerId ?? (new Guid()); } Dictionary <string, object> props = new Dictionary <string, object>(); if (model.MaxOrderAmount != null) { props.Add("MaxOrderAmount", model.MaxOrderAmount); } if (model.LocationNumber != null) { props.Add("LocationNumber", model.LocationNumber); } if (props.Any()) { clientStore.Properties = new JsonObject <Dictionary <string, object> > { Object = props } } ; GoogleGeocoding_Location location = GeneralPurpose.GetLatLong(clientStore.Store.Address); clientStore.Store.Latitude = location.lat; clientStore.Store.Longitude = location.lng; _db.ClientStores.Add(clientStore); await _db.SaveChangesAsync(); IndexStoreWithES(clientStore.Store.Id); return(RedirectToAction("Index")); }