public async Task <IActionResult> Create(CountryInputModel countryInput) { var existsCountry = this.countriesService .Exists(x => x.Name == countryInput.Name || x.CountryCode == countryInput.CountryCode); if (!this.ModelState.IsValid || existsCountry) { if (existsCountry) { this.ModelState.AddModelError(MatchingCountryKey, MatchingCountryErrorMessage); } return(this.View(countryInput)); } var image = await this.cloudinaryService.SaveImageAsync(countryInput.FormFile); await this.countriesService.AddAsync(countryInput, image); var countryNameEncoded = HttpUtility.HtmlEncode(countryInput.Name); this.TempData[GlobalConstants.MessageKey] = $"Successfully created country <strong>{countryNameEncoded}</strong>!"; return(this.RedirectToAction("Index", "Countries", new { area = "Places" })); }
public void AddCountry(CountryInputModel model) { var country = new Country { Name = model.Country, ShippingCost = model.ShippingCost }; _db.Countries.Add(country); }
public async Task AddAsync(CountryInputModel countryInput, ImageInputModel imageInput = null) { this.NullCheck(countryInput, nameof(countryInput)); var country = this.mapper.Map <Country>(countryInput); country.Image = this.mapper.Map <Image>(imageInput); await this.countriesRepository.AddAsync(country); await this.countriesRepository.SaveChangesAsync(); }
public ActionResult Create(CountryInputModel model) { if (model != null && this.ModelState.IsValid) { var country = Mapper.Map <Country>(model); this.Data.Countries.Add(country); this.Data.SaveChanges(); return(this.RedirectToAction("Index", "Countries")); } return(this.View(model)); }
public async Task <IActionResult> Create(CountryInputModel countryInputModel) { if (!this.ModelState.IsValid) { return(this.View(countryInputModel)); } var countryServiceModel = countryInputModel.To <CountryServiceInputModel>(); await this.countriesService.CreateAsync(countryServiceModel); return(this.Redirect("/Administration/Countries/All")); }
public async Task <IActionResult> AddCountry(CountryInputModel countryInputModel) { if (!this.ModelState.IsValid) { return(this.View(countryInputModel)); } try { await this.countriesService.AddAsync(countryInputModel); } catch (Exception) { return(this.View("DuplicateValue", countryInputModel)); } return(this.RedirectToAction("Index")); }
public async Task AddAsync(CountryInputModel countryInputModel) { var country = new Country { Name = countryInputModel.Name, }; bool doesCountryExist = await this.countriesRepository.All().AnyAsync(x => x.Name == country.Name); if (doesCountryExist) { throw new ArgumentException(string.Format(GlobalConstants.ErrorMessages.CountryNameAlreadyExists, country.Name)); } await this.countriesRepository.AddAsync(country); await this.countriesRepository.SaveChangesAsync(); }
public async Task EditAsync(string id, CountryInputModel countryInput, ImageInputModel imageInput) { this.NullCheck(id, nameof(id)); this.NullCheck(countryInput, nameof(countryInput)); var country = this.countriesRepository .All() .FirstOrDefault(x => x.Id == id); if (imageInput != null) { country.Image = this.mapper.Map <Image>(imageInput); } country.Name = countryInput.Name; country.CountryCode = countryInput.CountryCode; country.Description = countryInput.Description; await this.countriesRepository.SaveChangesAsync(); }
public async Task <IActionResult> Edit(string id, CountryInputModel countryInput) { var existsId = this.countriesService .Exists(x => x.Id == id); if (!existsId) { return(this.NotFound()); } var existsCountry = this.countriesService .Exists(x => x.Id != id && (x.Name == countryInput.Name || x.CountryCode == countryInput.CountryCode)); if (!this.ModelState.IsValid || existsCountry) { if (existsCountry) { this.ModelState.AddModelError(MatchingCountryKey, MatchingCountryErrorMessage); } var country = new CountryEditViewModel() { Id = id, CountryInput = countryInput, }; return(this.View(country)); } var image = await this.cloudinaryService.SaveImageAsync(countryInput.FormFile); await this.countriesService.EditAsync(id, countryInput, image); var countryNameEncoded = HttpUtility.HtmlEncode(countryInput.Name); this.TempData[GlobalConstants.MessageKey] = $"Successfully edited country <strong>{countryNameEncoded}</strong>!"; return(this.RedirectToAction("Details", "Countries", new { area = "Places", id })); }
public async Task AddMapsTheInputModelAndImageAndAddsThem( string name, string description, string countryCode, bool nullImage, string imageSource) { // Arrange AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly); var saved = true; var countriesList = new List <Country>(); var repositoryMock = new Mock <IDeletableEntityRepository <Country> >(); repositoryMock.Setup(x => x.AddAsync(It.IsAny <Country>())) .Callback((Country country) => { countriesList.Add(country); }); repositoryMock.Setup(x => x.SaveChangesAsync()) .Callback(() => { saved = true; }); var cityInputModel = new CountryInputModel() { Name = name, CountryCode = countryCode, Description = description, }; var imageInputModel = new ImageInputModel() { Source = imageSource, }; if (nullImage) { imageInputModel = null; } var countriesService = new CountriesService(repositoryMock.Object, AutoMapperConfig.MapperInstance); // Act await countriesService.AddAsync(cityInputModel, imageInputModel); // Assert var actualCountry = countriesList[0]; Assert.True(saved); Assert.Equal(name, actualCountry.Name); Assert.Equal(description, actualCountry.Description); Assert.Equal(countryCode, actualCountry.CountryCode); var actualImage = actualCountry.Image; if (nullImage) { Assert.Null(actualImage); } else { Assert.Equal(imageSource, actualImage.Source); } }
public async Task EditAsyncEditsTheCorrectPropertiesAndSavesTheResult( string countryName, string description, string countryCode, string newCountryName, string newDescription, string newCountryCode, bool imageNull, string imageSource, string newImageSource) { // Arrange AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly); var saved = true; var country = new Country() { Name = countryName, CountryCode = countryCode, Description = description, Image = new Image() { Source = imageSource, }, }; var countriesList = new List <Country>() { new Country(), new Country(), country, new Country(), }; var repositoryMock = new Mock <IDeletableEntityRepository <Country> >(); repositoryMock.Setup(x => x.All()) .Returns(countriesList.AsQueryable()); repositoryMock.Setup(x => x.SaveChangesAsync()) .Callback(() => { saved = true; }); var countriesService = new CountriesService(repositoryMock.Object, AutoMapperConfig.MapperInstance); var editInputModel = new CountryInputModel() { Name = newCountryName, CountryCode = newCountryCode, Description = newDescription, }; var imageInputModel = new ImageInputModel() { Source = newImageSource, }; if (imageNull) { imageInputModel = null; } // Act await countriesService.EditAsync(country.Id, editInputModel, imageInputModel); // Assert Assert.True(saved); Assert.Equal(newCountryName, country.Name); Assert.Equal(newCountryCode, country.CountryCode); Assert.Equal(newDescription, country.Description); var actualImage = country.Image; if (imageNull) { Assert.Equal(imageSource, actualImage.Source); } else { Assert.Equal(newImageSource, actualImage.Source); } }