public async Task <ValidationResult> ValidateAsync(ProductModel product) { var validationResult = ValidationResult.New(); if (product.Id == Guid.Empty) { validationResult.AddErrorMessage <Guid>($"{nameof(ProductModel)}.{nameof(ProductModel.Id)} property is required"); } if (product.Category.Id == Guid.Empty) { validationResult.AddErrorMessage <Guid>($"{nameof(ProductModel)}.{nameof(ProductModel.Category)}.{nameof(CategoryModel.Id)} property is required"); } if (product.Options.Any(x => x.Id == Guid.Empty)) { validationResult.AddErrorMessage <Guid>($"{nameof(ProductModel)}.{nameof(ProductModel.Options)}.{nameof(OptionModel.Id)} property is required"); } if (ContainsDuplicatedOptions(product)) { validationResult.AddErrorMessage <Guid>($"{nameof(ProductModel)}.{nameof(ProductModel.Options)} product {product.Id} contains duplicated options"); } if (!validationResult.IsValid) { return(validationResult); } if (!await _checkGateway.ExistsCategoryAsync(product.Category.Id)) { validationResult.AddErrorMessage <Guid>($"{nameof(ProductModel)}.{nameof(ProductModel.Category)}.{nameof(CategoryModel.Id)} specified category does not exists [{product.Category.Id}] "); } return(validationResult); }
public async Task <ValidationResult> ValidateAsync(OrderModel order) { var validationResult = ValidationResult.New(); if (order.Id == Guid.Empty) { validationResult.AddErrorMessage <Guid>($"{nameof(OrderModel)}.{nameof(OrderModel.Id)} property is required"); } if (order.OrderCategory.Id == Guid.Empty) { validationResult.AddErrorMessage <Guid>($"{nameof(OrderModel)}.{nameof(OrderModel.OrderCategory)}.{nameof(OrderCategoryModel.Id)} property is required"); } if (!order.Products.Any()) { validationResult.AddErrorMessage <Guid>($"{nameof(OrderModel)}.{nameof(OrderModel.Products)} Orders without products are not allowed"); } if (order.Products.Any(x => x.Id == Guid.Empty)) { validationResult.AddErrorMessage <Guid>($"{nameof(OrderModel)}.{nameof(OrderModel.Products)}.{nameof(OrderedProductModel.Id)} property is required"); } if (order.Products.SelectMany(x => x.CustomOptions).Any(x => x.Id == Guid.Empty)) { validationResult.AddErrorMessage <Guid>($"{nameof(OrderModel)}.{nameof(OrderModel.Products)}.{nameof(OrderedProductModel.CustomOptions)}.{nameof(Option.Id)} property is required"); } var productsWithDuplicatedOptions = GetProductsWithDuplicatedOptions(order.Products); if (productsWithDuplicatedOptions.Any()) { validationResult.AddErrorMessage <Guid>($"{nameof(OrderModel)}.{nameof(OrderModel.Products)}.{nameof(OrderedProductModel.CustomOptions)} products [{productsWithDuplicatedOptions.JoinStrings()}] contains duplicated options"); } if (!validationResult.IsValid) { return(validationResult); } if (await _checkGateway.ExistsOrderAsync(order.Id)) { validationResult.AddErrorMessage <Guid>($"{nameof(OrderModel)}.{nameof(OrderModel.Id)} order with identifier [{order.OrderCategory.Id}] already exists"); return(validationResult); } if (!await _checkGateway.ExistsCategoryAsync(order.OrderCategory.Id)) { validationResult.AddErrorMessage <Guid>($"{nameof(OrderModel)}.{nameof(OrderModel.OrderCategory)}.{nameof(OrderCategoryModel.Id)} specified category does not exists [{order.OrderCategory.Id}] "); return(validationResult); } var productsIds = order .Products .Select(x => x.Id) .Distinct() .ToList(); var storedProducts = await _checkGateway .GetProductsAsync(productsIds); var wrongCategoryProducts = storedProducts .GetList() .Where(p => p.Category.Id != order.OrderCategory.Id) .Select(x => x.Id) .ToList(); if (wrongCategoryProducts.Any()) { validationResult.AddErrorMessage <Guid>($"{nameof(OrderModel)}.{nameof(OrderModel.Products)} [{wrongCategoryProducts.JoinStrings()}] has category different from the category specified in order"); } var missingProducts = productsIds .Except(storedProducts.GetList().Select(x => x.Id)) .ToList(); if (missingProducts.Any()) { validationResult.AddErrorMessage <Guid>($"{nameof(OrderModel)}.{nameof(OrderModel.Products)} [{missingProducts.JoinStrings()}] does not exists"); } if (!validationResult.IsValid) { return(validationResult); } order.Products .ToList() .Select(product => GetProductsWithMissingOptions(storedProducts, product)) .Where(x => x.Item2.Any()) .ToList() .ForEach(x => validationResult.AddErrorMessage <Guid>($"{nameof(OrderModel)}.{nameof(OrderModel.Products)}.{nameof(OrderedProductModel.CustomOptions)} product [{x.Item1}] has associated [{x.Item2.JoinStrings()}] options that does not exists")); return(validationResult); }