public bool AddFilter(List <ProductFilterValueResultSet> productFilterValueResultSetList) { try { foreach (ProductFilterValueResultSet filterResult in productFilterValueResultSetList) { ProductFilterValue productFilterValue = this.context.ProductFilterValues.Where(x => x.ProductId == filterResult.ProductId && x.Id == filterResult.Id).FirstOrDefault(); var productFilterValuesExists = this.context.ProductFilterValues.Where(x => x.ProductId == filterResult.ProductId && x.CategoryMasterFilter == filterResult.CategoryMasterFilterId && x.FilterValue == filterResult.FilterValue && x.FilterValueText == filterResult.FilterValueText).ToList(); if (productFilterValue != null) { productFilterValue.CategoryMasterFilter = filterResult.CategoryMasterFilterId; productFilterValue.FilterValue = filterResult.FilterValue; productFilterValue.FilterValueText = filterResult.FilterValueText; this.context.Entry(productFilterValue).State = EntityState.Modified; } else if (productFilterValuesExists.Count == 0 && filterResult.CategoryMasterFilterId > 0 && !string.IsNullOrEmpty(filterResult.FilterValue) && !string.IsNullOrEmpty(filterResult.FilterValueText)) { ProductFilterValue productFilterValueNew = new ProductFilterValue(); productFilterValueNew.CategoryMasterFilter = filterResult.CategoryMasterFilterId; productFilterValueNew.FilterValue = filterResult.FilterValue; productFilterValueNew.FilterValueText = filterResult.FilterValueText; productFilterValueNew.ProductId = filterResult.ProductId; context.ProductFilterValues.Add(productFilterValueNew); this.context.Entry(productFilterValueNew).State = EntityState.Added; } } var changes = context.SaveChanges(); if (changes > 0) { return(true); } } catch { } return(false); }
public JsonResult SaveValueList(ProductFilterValueViewModel model) { try { //if (string.IsNullOrEmpty(model.value)) //{ // return Error("وارد کردن عنوان اجباری است."); //} var currentUser = GetAuthenticatedUser(); int order = 1; var lastItem = _context.ProductFilterValue.Where(x => x.StatusId != ProductFilterValueStatus.Deleted.Id && x.ProductFilterId == model.productFilterId).OrderByDescending(x => x.Order).FirstOrDefault(); if (lastItem != null) { order = lastItem.Order + 1; } var entity = new ProductFilterValue() { ProductFilterId = model.productFilterId, Order = order, Sku = model.translations.Single(x => x.languageId == Language.Persian.Id).title.ToStandardPersian(), StatusId = ProductFilterValueStatus.Active.Id, CreateUserId = currentUser.id, ModifyUserId = currentUser.id, CreateDate = GetDatetime(), ModifyDate = GetDatetime(), CreateIp = GetCurrentIp(), ModifyIp = GetCurrentIp() }; _context.ProductFilterValue.Add(entity); model.translations.ForEach(item => { var entityItem = entity.TranslationList.SingleOrDefault(x => x.LanguageId == item.languageId); if (entityItem != null) { entityItem.Value = item.title; } else { entityItem = new ProductFilterValueTranslation() { ProductFilterValueId = entity.Id, LanguageId = item.languageId, Value = item.title }; _context.ProductFilterValueTranslation.Add(entityItem); } }); _context.SaveChanges(); return(Success("اطلاعات با موفقیت ذخیره شد.")); } catch (Exception ex) { return(ServerError(ex)); } }