コード例 #1
0
        private bool IsAttributeNameDuplicate(ProductAttributeViewModel productAttribute)
        {
            using (InnoventoryDBContext dbContext = new InnoventoryDBContext())
            {

                FindResult<ProductAttributeViewModel> findResult = productAttributeRepository.FindBy(dbContext, x => x.AttributeName == productAttribute.AttributeName);

                if (findResult.Success && findResult.Entities.Count > 0)
                {

                    if (findResult.Entities.FirstOrDefault().ProductAttributeId != productAttribute.ProductAttributeId)
                    {
                        return true;
                    }

                }

            }

            return false;
        }
コード例 #2
0
        public UpdateResult<ProductAttributeViewModel> UpdateProductAttribute(ProductAttributeViewModel productAttribute)
        {
            UpdateResult<ProductAttributeViewModel> updateResult = new UpdateResult<ProductAttributeViewModel>();

            updateResult.Entity = productAttribute;

            List<AttributeSubCategorySelection> selectedSubCategories = productAttribute.SubCategorySelections.Where(x => x.IsSelected).ToList();

            if (selectedSubCategories.Count == 0)
            {
                updateResult.Success = false;
                updateResult.ErrorMessage = "Please select at least one Sub Category";

                return updateResult;
            }

            if (string.IsNullOrEmpty(productAttribute.AttributeName))
            {
                updateResult.ErrorMessage = "Please enter a valid attribute name";
                return updateResult;
            }

            if (IsAttributeNameDuplicate(productAttribute))
            {
                updateResult.ErrorMessage = "Attribute Name already exist";

                return updateResult;
            }

            using (InnoventoryDBContext dbContext = new InnoventoryDBContext())
            {
                using (DbContextTransaction trans = dbContext.Database.BeginTransaction())
                {

                    if (productAttribute.ProductAttributeId != Guid.Empty)
                    {

                        FindResult<SubCategoryAttributeMapViewModel> subCategoryAttributeMapVMResult = subCategoryAttributeMapRepository.FindBy(x => x.ProductAttributeId == productAttribute.ProductAttributeId);

                        List<Guid> existingSubCategoryIds = subCategoryAttributeMapVMResult.Entities.Select(x => x.SubCategoryId).ToList();

                        if (!subCategoryAttributeMapVMResult.Success)
                        {
                            updateResult.ErrorMessage = updateResult.ErrorMessage = string.Format("Can not retrieve existing mappings");
                            return updateResult;
                        }

                        if (subCategoryAttributeMapVMResult.Entities.Count > 0)
                        {
                            foreach (var item in subCategoryAttributeMapVMResult.Entities)
                            {
                                AttributeSubCategorySelection existingSelection = productAttribute.SubCategorySelections
                                        .FirstOrDefault(x => x.SubCategory.SubCategoryId == item.SubCategoryId && x.IsSelected == true);

                                if (existingSelection == null)
                                {
                                    EntityOperationResultBase deleteResult = subCategoryAttributeMapRepository.Delete(dbContext, item.SubCategoryAttributeMapId);

                                    if (deleteResult.Success == false)
                                    {
                                        updateResult.ErrorMessage = updateResult.ErrorMessage = string.Format("Mapping {0} Can not be deleted. Error: {1}", item.SubCategoryAttributeMapId, deleteResult.ErrorMessage);
                                    }
                                }
                            }
                        }

                        foreach (AttributeSubCategorySelection subCategorySelection in selectedSubCategories)
                        {

                            if (!existingSubCategoryIds.Contains(subCategorySelection.SubCategory.SubCategoryId))
                            {

                                SubCategoryAttributeMapViewModel newMapping = new SubCategoryAttributeMapViewModel
                                {
                                    ProductAttributeId = productAttribute.ProductAttributeId,
                                    SubCategoryId = subCategorySelection.SubCategory.SubCategoryId
                                };

                                UpdateResult<SubCategoryAttributeMapViewModel> updMappingResult = subCategoryAttributeMapRepository.Update(dbContext, newMapping);

                                if (!updMappingResult.Success)
                                {
                                    updateResult.Success = false;
                                    updateResult.ErrorMessage = updateResult.ErrorMessage = string.Format("Mapping with {0} Can not be saved", subCategorySelection.SubCategory.SubCategoryName); ;
                                    return updateResult;
                                }

                            }

                        }

                        updateResult = productAttributeRepository.Update(productAttribute);

                    }
                    else
                    {

                        updateResult = productAttributeRepository.Update(productAttribute);

                        if (!updateResult.Success)
                        {
                            return updateResult;
                        }

                        ProductAttributeViewModel updatedModel = updateResult.Entity;

                        foreach (var subCatSelection in selectedSubCategories)
                        {

                            SubCategoryAttributeMapViewModel newMapping = new SubCategoryAttributeMapViewModel
                            {
                                SubCategoryId = subCatSelection.SubCategory.SubCategoryId,
                                ProductAttributeId = updatedModel.ProductAttributeId
                            };

                            UpdateResult<SubCategoryAttributeMapViewModel> newMappingUpdateResult = subCategoryAttributeMapRepository.Update(newMapping);

                            if (!newMappingUpdateResult.Success)
                            {
                                trans.Rollback();
                                updateResult.Success = false;
                                updateResult.ErrorMessage = string.Format("Mapping with {0} Can not be saved", subCatSelection.SubCategory.SubCategoryName);
                                return updateResult;
                            }

                        }

                    }

                    trans.Commit();
                }

                if (updateResult.Success)
                {
                    updateResult.SuccessMessage = "Product Attribute saved successfully";
                }
                //if(findre)

            }

            return updateResult;
        }