/** * Method that will remove price history from the material with the passed reference. * It is assumed that a list with 1 or more objects is received. * * Validations performed: * 1. Validation of the passed material's reference (database); * 2. The received list has 1 or more elements. * 3. Validation of the passed finish's reference (database); * FOREACH PRICE HISTORY RECEIVED { * 4. Validation of the existence of each price history received, in the material with the passed dto. * 5. Validation for duplication between received price history. * 6. Validation that the date of the price history is future * } */ public ValidationOutput DeleteFinishPriceHistoryFromMaterial(string reference, string finishReference, IEnumerable <PriceHistoryDto> enumerableHistoryDto) { List <PriceHistoryDto> listPriceHistoryDto = new List <PriceHistoryDto>( enumerableHistoryDto); //Since we receive an IEnumerable, we need to have something concrete ValidationOutput validationOutput = new ValidationOutputBadRequest(); //1. validationOutput = new ValidationOutputNotFound(); if (!MaterialExists(reference)) { validationOutput.AddError("Reference of material", "No material with the reference '" + reference + "' exists in the system."); return(validationOutput); } //2. if (listPriceHistoryDto.Count == 0) { validationOutput.AddError("Selected price history", "No price history were selected!"); return(validationOutput); } Material materialToModify = _materialRepository.GetByReference(reference); List <PriceHistory> priceHistoryToDelete = new List <PriceHistory>(); //3. if (!materialToModify.ContainsFinish(finishReference)) { validationOutput.AddError("Finish", "Finish with the reference '" + finishReference + "' does not exist in material '" + reference + "'!"); return(validationOutput); } Finish finishToModify = materialToModify.GetFinish(finishReference); foreach (var currentPriceHistoryDto in listPriceHistoryDto) { validationOutput = new ValidationOutputBadRequest(); PriceHistory currentPriceHistory = _mapper.Map <PriceHistory>(currentPriceHistoryDto); //4. if (!finishToModify.ContainsPriceHistory(currentPriceHistory)) { validationOutput.AddError("Price History", "Price History with the date '" + currentPriceHistory.Date + "' does not exist in finish '" + finishReference + "'!"); return(validationOutput); } //5. if (priceHistoryToDelete.Contains(currentPriceHistory)) { validationOutput.AddError("Price History", "Price History with the date '" + currentPriceHistory.Date + "' is duplicated in the list of selected price history."); return(validationOutput); } //6. if (currentPriceHistoryDto.Date.CompareTo(DateTime.Now) < 0) { validationOutput.AddError("Price History", "Price History with the date '" + currentPriceHistory.Date + "' can't be deleted."); return(validationOutput); } priceHistoryToDelete.Add(currentPriceHistory); } foreach (var priceToDelete in priceHistoryToDelete) { finishToModify.RemovePriceHistory(priceToDelete); } //Removes the old Finish materialToModify.RemoveFinish(materialToModify.GetFinish(finishReference)); //Adds the new one materialToModify.AddFinish(finishToModify); _materialRepository.Update(materialToModify); return(validationOutput); }