public async Task <IActionResult> Edit(Guid id) { var store = await _db.Stores .Include(i => i.ClientStores) .FirstOrDefaultAsync(q => q.Id == id); if (store == null) { return(View("StoreNotFound")); } Store_AddEditModel model = new Store_AddEditModel { Id = id, Name = store.Name, Addr_Ln_1 = store.Addr_Ln_1, Addr_Ln_2 = store.Addr_Ln_2, City = store.City, State = store.State, Zip = store.Zip, Phone = store.Phone }; var storeClient = store.ClientStores.FirstOrDefault(q => q.ClientId == _client_id); try { model.MaxOrderAmount = Convert.ToDecimal(storeClient.Properties.Object["MaxOrderAmount"]); } catch { model.MaxOrderAmount = null; } try { model.LocationNumber = Convert.ToInt32(storeClient.Properties.Object["LocationNumber"]); } catch { model.LocationNumber = null; } model.ClientRetailers = await _db.ClientRetailers .Where(q => q.ClientId == _client_id) .Select(s => s.Retailer) .ToListAsync(); ViewData["Title"] = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(store.Name.ToLower()); return(View(model)); }
public async Task <IActionResult> New() { Store_AddEditModel model = new Store_AddEditModel(); model.ClientRetailers = await _db.ClientRetailers .Include(i => i.Retailer) .Where(q => q.Active && q.ClientId == _client_id) .Select(s => s.Retailer) .ToListAsync(); ViewData["Title"] = "Add New Store"; return(View(model)); }
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")); }