public async Task <AddClientServiceValidationResult> Validate(AddClientServiceCommand command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } if (!IsValid(command.Name, 1, 50)) { return(new AddClientServiceValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatoryAndShouldContainsBetween, Constants.DtoNames.ClientService.Name, 1, 15))); } if (!IsValid(command.Description, 1, 255)) { return(new AddClientServiceValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatoryAndShouldContainsBetween, Constants.DtoNames.ClientService.Description, 1, 255))); } if (command.Price < 0) { return(new AddClientServiceValidationResult(ErrorDescriptions.ThePriceCannotBeLessThanZero)); } if (!IsValid(command.GooglePlaceId)) { return(new AddClientServiceValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.ClientService.GooglePlaceId))); } if (!string.IsNullOrWhiteSpace(command.CategoryId)) { var category = await _categoryRepository.Get(command.CategoryId); if (category == null) { return(new AddClientServiceValidationResult(ErrorDescriptions.TheCategoryDoesntExist)); } } return(new AddClientServiceValidationResult()); }
public async Task <IActionResult> Execute(string id) { if (string.IsNullOrWhiteSpace(id)) { throw new ArgumentNullException(nameof(id)); } if (string.IsNullOrWhiteSpace(id)) { throw new ArgumentNullException(nameof(id)); } var category = await _repository.Get(id); if (category == null) { return(new NotFoundResult()); } _shopCategoryEnricher.Enrich(_halResponseBuilder, category); return(new OkObjectResult(_halResponseBuilder.Build())); }
public async Task <AddShopValidationResult> Validate(AddShopCommand shop, string subject) { if (shop == null) { throw new ArgumentNullException(nameof(shop)); } if (string.IsNullOrWhiteSpace(subject)) { throw new ArgumentNullException(nameof(subject)); } // 1. Check category. var category = await _categoryRepository.Get(shop.CategoryId); if (category == null) { return(new AddShopValidationResult(ErrorDescriptions.TheCategoryDoesntExist)); } // 2. Check the user doesn't already have a shop on the same category. var searchResult = await _shopRepository.Search(new SearchShopsParameter { CategoryIds = new[] { shop.CategoryId }, Subjects = new[] { subject } }); if (searchResult.Content != null && searchResult.Content.Any()) { return(new AddShopValidationResult(ErrorDescriptions.TheShopCannotBeAddedBecauseThereIsAlreadyOneInTheCategory)); } // 3. Check mandatory parameters. if (!IsValid(shop.Name, 1, 15)) { return(new AddShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatoryAndShouldContainsBetween, Constants.DtoNames.Shop.Name, 1, 15))); } if (!IsValid(shop.Description, 1, 255)) { return(new AddShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatoryAndShouldContainsBetween, Constants.DtoNames.Shop.Description, 5, 15))); } if (!IsValid(shop.GooglePlaceId)) { return(new AddShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.Shop.GooglePlaceId))); } if (!IsValid(shop.StreetAddress)) { return(new AddShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.Shop.StreetAddress))); } if (!IsValid(shop.PostalCode)) { return(new AddShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.Shop.PostalCode))); } if (!IsValid(shop.Locality)) { return(new AddShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.Shop.Locality))); } if (!IsValid(shop.Country)) { return(new AddShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.Shop.Country))); } if (shop.ProductCategories != null) { if (shop.ProductCategories.Any(f => string.IsNullOrWhiteSpace(f.Name))) { return(new AddShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.Shop.ProductCategories + "." + Constants.DtoNames.ProductCategory.Name))); } if (shop.ProductCategories.GroupBy(c => c.Name).Any(kvp => kvp.Count() > 1)) { return(new AddShopValidationResult(string.Format(ErrorDescriptions.DuplicateValues, Constants.DtoNames.Shop.ProductCategories))); } if (shop.ProductCategories.Count() > 8) { return(new AddShopValidationResult(ErrorDescriptions.OnlyEightCategoriesCanBeAdded)); } } if (shop.ProductFilters != null) { if (shop.ProductFilters.Any(f => string.IsNullOrWhiteSpace(f.Name))) { return(new AddShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.Shop.Filters + "." + Constants.DtoNames.Filter.Name))); } if (shop.ProductFilters.Any(f => f.Values == null || !f.Values.Any())) { return(new AddShopValidationResult(string.Format(ErrorDescriptions.TheParameterIsMandatory, Constants.DtoNames.Shop.Filters + "." + Constants.DtoNames.Filter.Values))); } if (shop.ProductFilters.GroupBy(c => c.Name).Any(kvp => kvp.Count() > 1)) { return(new AddShopValidationResult(string.Format(ErrorDescriptions.DuplicateValues, Constants.DtoNames.Shop.Filters))); } if (shop.ProductFilters.Any(p => p.Values.GroupBy(v => v).Count() != p.Values.Count())) { return(new AddShopValidationResult(string.Format(ErrorDescriptions.DuplicateValues, Constants.DtoNames.Shop.Filters + "." + Constants.DtoNames.Filter.Values))); } } return(new AddShopValidationResult(category)); }