private UpdateResult<SubCategoryViewModel> CreateNewSubcategory(SubCategoryViewModel subCategoryViewModel) { UpdateResult<SubCategoryViewModel> updateResult = new UpdateResult<SubCategoryViewModel>(); using (InnoventoryDBContext dbContext = new InnoventoryDBContext()) { updateResult = subCategoryRepository.Update(subCategoryViewModel); if (updateResult.Success) { AddCategorySubCategoryMap(dbContext, subCategoryViewModel); } updateResult.Success = true; updateResult.ErrorMessage = string.Empty; } return updateResult; }
private void AddCategorySubCategoryMap(InnoventoryDBContext dbContext, SubCategoryViewModel viewModel) { if (viewModel.CategoryIds != null && viewModel.CategoryIds.Count > 0) { foreach (Guid categoryId in viewModel.CategoryIds) { CategorySubCategoryMapViewModel mapViewModel = new CategorySubCategoryMapViewModel { CategorySubCategoryMapId = Guid.Empty, CategoryId = categoryId, SubCategoryId = viewModel.SubCategoryId, }; categorySubCategoryMapRepo.Update(dbContext, mapViewModel); } } }
private bool AddUpdateCategorySubCategoryMapRepo(SubCategoryViewModel viewModel) { using (InnoventoryDBContext dbContext = new InnoventoryDBContext()) { using (DbContextTransaction transaction = dbContext.Database.BeginTransaction()) { DbSet<SubCategory> subCategorySet = dbContext.SubCategorySet; SubCategory subCategory = new SubCategory(); ObjectMapper.PropertyMap(viewModel, subCategory); FindResult<CategoryViewModel> categoryResult = categoryRepository.GetAll(dbContext); FindResult<CategorySubCategoryMapViewModel> mapResult = categorySubCategoryMapRepo.FindBy(x => x.SubCategoryId == viewModel.SubCategoryId); List<CategorySubCategoryMapViewModel> existingMappings = new List<CategorySubCategoryMapViewModel>(); existingMappings = mapResult.Entities; List<Guid> deletionList = new List<Guid>(); List<Guid> newAdded = new List<Guid>(); foreach (var emapvm in existingMappings) { if (!viewModel.CategoryIds.Contains(emapvm.CategoryId)) { deletionList.Add(emapvm.CategorySubCategoryMapId); } } if (viewModel.CategoryIds != null && viewModel.CategoryIds.Count > 0) { foreach (var cId in viewModel.CategoryIds) { CategorySubCategoryMapViewModel existingMap = existingMappings.FirstOrDefault(x => x.SubCategoryId == viewModel.SubCategoryId && x.CategoryId == cId); if (existingMap == null) { newAdded.Add(cId); } } } if (deletionList.Count > 0) { foreach (Guid mapid in deletionList) { categorySubCategoryMapRepo.Delete(mapid); } } if (newAdded.Count > 0) { foreach (Guid categoryId in newAdded) { CategorySubCategoryMapViewModel catSubCatMapVM = new CategorySubCategoryMapViewModel { CategorySubCategoryMapId = Guid.Empty, CategoryId = categoryId, SubCategoryId = viewModel.SubCategoryId, }; categorySubCategoryMapRepo.Update(dbContext, catSubCatMapVM); } } subCategorySet.Attach(subCategory); dbContext.Entry<SubCategory>(subCategory).State = EntityState.Modified; dbContext.SaveChanges(); transaction.Commit(); } } return true; }
public UpdateResult<SubCategoryViewModel> UpdateSubCategory(SubCategoryViewModel subCategoryViewModel) { UpdateResult<SubCategoryViewModel> updateResult = new UpdateResult<SubCategoryViewModel>(); if (subCategoryViewModel.SubCategoryId == Guid.Empty) { updateResult = CreateNewSubcategory(subCategoryViewModel); } else { bool success = AddUpdateCategorySubCategoryMapRepo(subCategoryViewModel); updateResult.Success = success; } return updateResult; }