int MergeDuplicateRecipes(Recipe[] recipes) { var count = 0; for (int i = 0; i < recipes.Length; i++) { for (int j = i + 1; j < recipes.Length; j++) { var source = recipes[j]; var target = recipes[i]; if (source == null || target == null) { continue; } if (!DuplicateChecker.RecipesAreEqual(source, target)) { continue; } _recipeMerger.MergeRecipes(source, target); count++; recipes[j] = null; } } return(count); }
/// <summary> /// Merges the associated processing, craft and design observations for two recipes. Deletes source. /// </summary> /// <param name="source"></param> /// <param name="target"></param> /// <returns></returns> public int MergeRecipes(Recipe source, Recipe target) { //todo: update this when craft and design observations are implemented if (source == null) { throw new ArgumentNullException(nameof(source)); } if (target == null) { throw new ArgumentNullException(nameof(target)); } if (source.Id == Guid.Empty) { throw new InvalidOperationException("Source is transient."); } if (target.Id == Guid.Empty) { throw new InvalidOperationException("Target is transient."); } if (!DuplicateChecker.RecipesAreEqual(source, target)) { throw new InvalidOperationException("Recipes are not duplicates. Cannot merge."); } var obs = _processingObservationRepository.GetByRecipe(source).ToArray(); foreach (var o in obs) { o.Recipe = target; _processingObservationRepository.Save(o); } _recipeRepository.Delete(source); return(obs.Length); }