예제 #1
0
        public virtual PartialViewResult UpdateValueListAjax(int? id, ValueListEditAdminViewModel valueListViewModel, ValueListItemEditAdminViewModel[] listItems, string fastAddValueListItem)
        {
            if (!id.HasValue)
            {
                return HttpBadRequestPartial("id is null");
            }

            if (valueListViewModel.ValueListId == 0)
            {
                return HttpBadRequestPartial("Value List Id in view model is 0");
            }

            if (valueListViewModel.ValueListId != id.Value)
            {
                return HttpBadRequestPartial("Value List Id mismatch. Parameter value: '" + id.Value + "' != view model value: " + valueListViewModel.ValueListId);
            }

            Client client = CurrentClientOrThrow;
            ValueList valueListToUpdate = client.ValueLists.SingleOrDefault(vl => vl.ValueListId == valueListViewModel.ValueListId);

            if (valueListToUpdate == null)
            {
                throw new ApplicationException("Value List not found in client Value Lists. Value List Id: " + valueListToUpdate.ValueListId + " Client '" + client.Name + "' [" + client.ClientId + "]");
            }

            bool nameIsValid = GStoreDb.ValidateValueListName(this, valueListToUpdate.Name, client.ClientId, valueListToUpdate.ValueListId);
            bool fastAddIsValid = false;
            if (!string.IsNullOrWhiteSpace(fastAddValueListItem))
            {
                fastAddIsValid = GStoreDb.ValidateValueListFastAddItemName(this, fastAddValueListItem, valueListToUpdate, null);
            }

            if (nameIsValid && ModelState.IsValid)
            {
                ValueList valueList = null;
                try
                {
                    valueList = GStoreDb.UpdateValueList(valueListViewModel, client, CurrentUserProfileOrThrow);

                    if (listItems != null && listItems.Count() > 0)
                    {
                        foreach (ValueListItemEditAdminViewModel listItem in listItems)
                        {
                            if (listItem.ValueListId != id.Value)
                            {
                                throw new ApplicationException("ValueListId mismatch for list item. ValueList id: " + id.Value + " listItem.ValueListId: " + listItem.ValueListId);
                            }
                            ValueListItem listItemUpdated = GStoreDb.UpdateValueListItem(listItem, client, CurrentUserProfileOrThrow);
                        }
                    }
                    valueListViewModel = new ValueListEditAdminViewModel(CurrentStoreFrontOrThrow, CurrentUserProfileOrThrow, valueList, isStoreAdminEdit: true, activeTab: valueListViewModel.ActiveTab, sortBy: valueListViewModel.SortBy, sortAscending: valueListViewModel.SortAscending);

                    if (fastAddIsValid)
                    {
                        ValueListItem newItem = GStoreDb.CreateValueListItemFastAdd(valueListViewModel, fastAddValueListItem, client, CurrentUserProfileOrThrow);
                        AddUserMessage("Value List Item Created!", "Value List Item '" + newItem.Name.ToHtml() + "' [" + newItem.ValueListItemId + "] created successfully.", UserMessageType.Success);
                        ModelState.Remove("fastAddValueListItem");
                    }

                    AddUserMessage("Value List Changes Saved!", "Value List '" + valueList.Name.ToHtml() + "' [" + valueList.ValueListId + "] saved successfully for Client '" + client.Name.ToHtml() + "' [" + client.ClientId + "]", UserMessageType.Success);

                    this.ModelState.Clear();
                    return PartialView("_ValueListEditPartial", valueListViewModel);
                }
                catch (Exception ex)
                {
                    string errorMessage = "An error occurred while saving your changes to Value List '" + valueListViewModel.Name + "' [" + valueListViewModel.ValueListId + "] for Client: '" + client.Name + "' [" + client.ClientId + "] \nError: '" + ex.GetType().FullName + "'";

                    if (CurrentUserProfileOrThrow.AspNetIdentityUserIsInRoleSystemAdmin())
                    {
                        errorMessage += " \nException.ToString(): '" + ex.ToString() + "'";
                    }
                    AddUserMessage("Error Saving Value List!", errorMessage.ToHtmlLines(), UserMessageType.Danger);
                    ModelState.AddModelError("Ajax", errorMessage);
                }
            }
            else
            {
                AddUserMessage("Value List Edit Error", "There was an error with your entry for Value List " + valueListViewModel.Name.ToHtml() + " [" + valueListViewModel.ValueListId + "] for Client '" + client.Name.ToHtml() + "' [" + client.ClientId + "]. Please correct it.", UserMessageType.Danger);
            }

            foreach (string key in this.ModelState.Keys.Where(k => k.StartsWith("ValueListItems[")).ToList())
            {
                this.ModelState.Remove(key);
            }

            valueListViewModel.IsStoreAdminEdit = true;
            return PartialView("_ValueListEditPartial", valueListViewModel);
        }
예제 #2
0
        public static ValueListItem UpdateValueListItem(this IGstoreDb db, ValueListItemEditAdminViewModel viewModel, Client client, UserProfile userProfile)
        {
            if (viewModel == null)
            {
                throw new ArgumentNullException("viewModel");
            }
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }
            if (userProfile == null)
            {
                throw new ArgumentNullException("userProfile");
            }

            //find existing record, update it
            ValueList valueList = client.ValueLists.SingleOrDefault(p => p.ValueListId == viewModel.ValueListId);
            if (valueList == null)
            {
                throw new ApplicationException("Value List not found in client Value Lists. Value List Id: " + viewModel.ValueListId + " Client '" + client.Name + " [" + client.ClientId + "]");
            }
            ValueListItem listItem = valueList.ValueListItems.Where(vli => vli.ValueListItemId == viewModel.ValueListItemId).SingleOrDefault();
            if (listItem == null)
            {
                throw new ApplicationException("Value List Item not found in existing values. Value List Id: " + viewModel.ValueListId + " Value List Item Id: " + viewModel.ValueListItemId + " Client '" + client.Name + " [" + client.ClientId + "]");
            }

            listItem.Description = viewModel.Description;
            listItem.EndDateTimeUtc = viewModel.EndDateTimeUtc;
            listItem.IsPending = viewModel.IsPending;
            listItem.Name = viewModel.Name;
            listItem.Order = viewModel.Order;
            listItem.StartDateTimeUtc = viewModel.StartDateTimeUtc;
            listItem.UpdateDateTimeUtc = DateTime.UtcNow;
            listItem.UpdatedBy = userProfile;
            listItem.UpdatedBy_UserProfileId = userProfile.UserProfileId;

            listItem.UpdateAuditFields(userProfile);

            listItem = db.ValueListItems.Update(listItem);
            db.SaveChanges();

            return listItem;
        }