public CreateCityCommandValidator(ICountryDomainRepository repository) { this.RuleFor(cmd => cmd.Name) .MinimumLength(MinNameLength) .MaximumLength(MaxNameLength) .NotEmpty(); this.RuleFor(cmd => cmd.CountryId) .MustAsync(async(countryId, token) => await repository .Find(countryId, token) != null) .WithMessage("'{PropertyName}' does not exist."); }
public PlaceCommandValidator( IPlaceDomainRepository placeRepository, ICategoryDomainRepository categoryRepository, ICountryDomainRepository countryRepository) { CascadeMode = CascadeMode.StopOnFirstFailure; this.RuleFor(c => c.Name) .MinimumLength(MinNameLength) .MaximumLength(MaxNameLength) .NotEmpty(); this.RuleFor(c => c.Description) .MinimumLength(MinDescriptionLength) .MaximumLength(MaxDescriptionLength) .NotEmpty(); //this.RuleFor(c => c.CategoryId) // .MustAsync(async (category, token) => await categoryRepository // .Get(category, token) != null) // .WithMessage("{PropertyName} does not exist."); this.RuleFor(c => c.CountryId) .MustAsync(async(counrty, token) => await countryRepository .Find(counrty, token) != null) .WithMessage("{PropertyName} does not exist."); this.RuleFor(c => c) .Must(cmd => Enumeration.HasValue <PlaceType>(cmd.Type.Value)) .WithMessage("'PlaceType' is not valid."); this.RuleSet("Data", () => { this.RuleFor(c => c) .MustAsync(async(cmd, token) => await countryRepository .Find(cmd.CountryId, cmd.CityId, token) != null) .WithMessage("'City' does not exist."); }); }
public async Task <CreateCityOutputModel> Handle( CreateCityCommand request, CancellationToken cancellationToken) { var country = await countryDomainRepository.Find(request.CountryId); country.AddCity(request.Name); await this.countryDomainRepository.Save(country, cancellationToken); var city = country.Cities.Single(city => city.Name == request.Name); return(new CreateCityOutputModel(city.Id)); }