/// <inheritdoc /> /// <summary> /// Jedes Buch muss zumindest einen Autor haben. /// Weiters darf ein Autor einem Buch nicht mehrfach zugewiesen /// werden. /// </summary> /// <param name="validationContext"></param> /// <returns></returns> public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { if (BookAuthors.Count == 0) { yield return(new ValidationResult("Book must have at least one author")); } var doubleAuthorNames = BookAuthors .GroupBy(ba => ba.Author) .Where(baGroup => baGroup.Count() > 1) .Select(baGroup => baGroup.Key.Name) .ToArray() .Join(); if (doubleAuthorNames.Length > 0) { yield return(new ValidationResult($"{doubleAuthorNames} are twice authors of the book")); } }
/// <inheritdoc /> /// <summary> /// Jedes Buch muss zumindest einen Autor haben. /// Weiters darf ein Autor einem Buch nicht mehrfach zugewiesen /// werden. /// </summary> /// <param name="validationContext"></param> /// <returns></returns> public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { ValidationResult result = new IsbnValidation().GetValidationResult(Isbn, new ValidationContext(Isbn)); if (result != null && result != ValidationResult.Success) { yield return(new ValidationResult(result.ErrorMessage, new string[] { nameof(Isbn) })); } if (BookAuthors.Count == 0) { yield return(new ValidationResult("Book must have at least one author", new string[] { nameof(BookAuthors) })); } var duplicates = BookAuthors.GroupBy(_ => _.Author) .Where(_ => _.Count() > 1) .Select(_ => _.Key); foreach (var duplicate in duplicates) { yield return(new ValidationResult($"{duplicate.Name} are twice authors of the book", new string[] { nameof(BookAuthors) })); } }
/// <inheritdoc /> /// <summary> /// Jedes Buch muss zumindest einen Autor haben. /// Weiters darf ein Autor einem Buch nicht mehrfach zugewiesen /// werden. /// </summary> /// <param name="validationContext"></param> /// <returns></returns> public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { if (BookAuthors.Count < 1 || BookAuthors == null) { yield return(new ValidationResult( "Buch muss mindestens einen Author haben", new string[] { nameof(BookAuthors) })); } ValidationResult validationResult = new IsbnValidation().GetValidationResult(Isbn, new ValidationContext(Isbn)); if (validationResult != null || validationResult != ValidationResult.Success) { yield return(new ValidationResult(validationResult.ErrorMessage, new string[] { nameof(Isbn) })); } var duplicateAuthors = BookAuthors .GroupBy(_ => _.Author) .Where(_ => _.Count() > 1) .Select(_ => _.Key); foreach (var duplicateAuthor in duplicateAuthors) { yield return(new ValidationResult($"{duplicateAuthor.Name} are twice authors of the book", new string[] { nameof(BookAuthors) })); } }