예제 #1
0
        //[Bind(new string[] { "Name", "Id", "UrlCode" })] Client client
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            Client clientToUpdate = new Client
            {
                Id         = Client.Id,
                Name       = Client.Name,
                UrlCode    = Client.UrlCode,
                Active     = Client.Active,
                Properties = Client.Properties
            };

            _db.Attach(clientToUpdate).State = Microsoft.EntityFrameworkCore.EntityState.Modified;

            try
            {
                await _db.SaveChangesAsync();
            }
            catch (Exception)
            {
                return(Page());
            }

            return(RedirectToPage("./Index"));
        }
예제 #2
0
        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 }));
        }