public async Task <IActionResult> Create(CountryCreateInputModel countryCreateInputModel) { if (!this.ModelState.IsValid) { return(this.View(countryCreateInputModel)); } await this.countriesService.CreateAsync(countryCreateInputModel); return(this.RedirectToAction("GetAll", "Countries", new { area = "Administration" })); }
public async Task CheckSettingOfCountryProperties() { var model = new CountryCreateInputModel { Name = "Germany", }; await this.countriesService.CreateAsync(model); var country = await this.countriesRepository.All().FirstOrDefaultAsync(); Assert.Equal("Germany", country.Name); }
public async Task TestAddingCountry() { var model = new CountryCreateInputModel { Name = "China", }; await this.countriesService.CreateAsync(model); var count = await this.countriesRepository.All().CountAsync(); Assert.Equal(1, count); }
public async Task CheckIfAddingCountryReturnsViewModel() { var country = new CountryCreateInputModel { Name = "Germany", }; var viewModel = await this.countriesService.CreateAsync(country); var dbEntry = await this.countriesRepository.All().FirstOrDefaultAsync(); Assert.Equal(dbEntry.Id, viewModel.Id); Assert.Equal(dbEntry.Name, viewModel.Name); }
public async Task CheckIfAddingCountryThrowsArgumentException() { this.SeedDatabase(); var country = new CountryCreateInputModel { Name = "Bulgaria", }; var exception = await Assert .ThrowsAsync <ArgumentException>(async() => await this.countriesService.CreateAsync(country)); Assert.Equal(string.Format(ExceptionMessages.CountryAlreadyExists, country.Name), exception.Message); }
public async Task <ActionResult> Create(CountryCreateInputModel model) { bool isExisits = await this.countriesService.IsCountryExistsAsync(model.Name); if (isExisits) { this.ModelState.AddModelError("Name", "Provided country name already exists"); } if (!ModelState.IsValid) { return(this.View(model)); } await this.countriesService.CreateCountryAsync(model.Name); return(RedirectToAction(nameof(Index))); }
public async Task <CountryDetailsViewModel> CreateAsync(CountryCreateInputModel countryCreateInputModel) { var country = new Country { Name = countryCreateInputModel.Name, }; bool doesCountryExist = await this.countriesRepository.All().AnyAsync(x => x.Name == country.Name); if (doesCountryExist) { throw new ArgumentException( string.Format(ExceptionMessages.CountryAlreadyExists, country.Name)); } await this.countriesRepository.AddAsync(country); await this.countriesRepository.SaveChangesAsync(); var viewModel = await this.GetViewModelByIdAsync <CountryDetailsViewModel>(country.Id); return(viewModel); }