public async Task Add_Calls_SaveAsync() { // arrange var countryService = new Mock <ICountryService>(MockBehavior.Strict); var controller = new CountryController(countryService.Object) { TempData = new Mock <ITempDataDictionary>().Object }; var countryName = "CountryName"; var model = new AddCountryViewModel { Name = countryName }; countryService.Setup(s => s.ExistsAsync(countryName)).ReturnsAsync(false); countryService.Setup(s => s.AddAsync(countryName)).ReturnsAsync(2); // act var result = await controller.Add(model); // assert Assert.IsAssignableFrom <RedirectToActionResult>(result); var actionName = (result as RedirectToActionResult).ActionName; Assert.Equal(nameof(CountryController.Add), actionName); countryService.VerifyAll(); }
public async Task Add_When_ModelState_Invalid() { // arrange var countryService = new Mock <ICountryService>(MockBehavior.Strict); var controller = new CountryController(countryService.Object) { TempData = new Mock <ITempDataDictionary>().Object }; var countryName = "CountryName"; var model = new AddCountryViewModel { Name = countryName }; controller.ModelState.AddModelError(string.Empty, "Some Extreme Error"); countryService.Setup(s => s.ExistsAsync(countryName)).ReturnsAsync(false); // act var result = await controller.Add(model); // assert Assert.IsAssignableFrom <ViewResult>(result); var resultModel = (result as ViewResult).Model; Assert.NotNull(resultModel); Assert.IsAssignableFrom <AddCountryViewModel>(resultModel); Assert.Equal(model, resultModel); countryService.VerifyAll(); }
public void AddCountry(AddCountryViewModel model) { try { if (model != null) { MasterCountry entity = new MasterCountry(); entity.CountryCode = model.CountryCode; entity.CountryName = model.CountryName; entity.CountryCallingCode = model.CountryCallingCode; entity.InternationalDialingPrefix = model.InternationalDialingPrefix; db.MasterCountries.Add(entity); } else { throw new Exception("Country could not be blank!"); } } catch (Exception) { throw; } }
private void btnAdd_Click(object sender, RoutedEventArgs e) { AddCountry countryDialog = new AddCountry(); AddCountryViewModel countryDialogVm = new AddCountryViewModel(); countryDialogVm.CancelCommand = new RelayCommand(() => { countryDialog.Close(); }); countryDialogVm.OKCommand = new RelayCommand(() => { if (countryDialogVm.IsValid) { DataProvider dp = new DataProvider(); City ci = dp.AddCoutryWithCity(countryDialogVm.CountryName, countryDialogVm.CityName); context.Countries.Load(); context.Cities.Load(); myCountriesViewSource.View.Refresh(); countryDialog.Close(); Messenger.Default.Send <string>(Utils.CountryAddedMessage); } else { countryDialogVm.UserMessage = "Please Complete Info "; } }); countryDialog.DataContext = countryDialogVm; countryDialog.ShowDialog(); }
public async Task <ActionResult> Create(AddCountryViewModel request) { if (!ModelState.IsValid) { Alert($"Invalid Request.", NotificationType.error, Int32.Parse(_appConfig.Value.NotificationDisplayTime)); return(View()); } try { var addCountryRequest = new AddCountryRequest { Name = request.Name, Description = request.Description }; var result = await _countryService.Create(addCountryRequest); if (!result.Success) { Alert($"{result.Message}", NotificationType.info, Int32.Parse(_appConfig.Value.NotificationDisplayTime)); return(View()); } Alert($"Country Created Successfully", NotificationType.success, Int32.Parse(_appConfig.Value.NotificationDisplayTime)); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { Alert($"Error! {ex.Message}.", NotificationType.error, Int32.Parse(_appConfig.Value.NotificationDisplayTime)); return(View()); } }
public ActionResult AddCountry() { AddCountryViewModel addCountryViewModel = new AddCountryViewModel(); addCountryViewModel.CityList = _cityService.All(); addCountryViewModel.CountryList = _countryService.All(); return(View(addCountryViewModel)); }
/// <summary> /// Add new country /// </summary> /// <param name="model"></param> /// <returns></returns> public virtual async Task <ResultModel> AddNewCountryAsync(AddCountryViewModel model) { if (model == null) { return(new InvalidParametersResultModel()); } await _context.Countries.AddAsync(model.Adapt <Country>()); return(await _context.PushAsync()); }
public IActionResult AddNewCountryToList(AddCountryViewModel model) { var successId = this.country.AddNewCountry(new CountryModel { Country = model.Country, RefSite = model.RefSite, SpphIdCountry = model.SpphIdCountry }); TempData.AddSuccessMessage($"Successfully added new country with id: {successId}."); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Post(AddCountryViewModel viewModel, CancellationToken cancellationToken) { using (var transaction = new Transactions(_serviceProvider)) { var command = _mapper.Map <AddCountryViewModel, AddCountryCommand>(viewModel); var result = await _mediator.SendCommandAsync <AddCountryCommand, Countries>(command, cancellationToken); if (result != null) { transaction.Commit(); } return(Response(result?.CountryId)); } }
public async Task <IActionResult> Add(AddCountryViewModel model) { if (!string.IsNullOrWhiteSpace(model.Name) && await countryService.ExistsAsync(model.Name)) { ModelState.AddModelError(nameof(model.Name), CountryWithNameExistsMessage); } if (!ModelState.IsValid) { return(View(model)); } await countryService.AddAsync(model.Name); return(RedirectToAction(nameof(Add))); }
public IActionResult ModifyCountry(AddCountryViewModel model) { if (!ModelState.IsValid) { TempData.AddErrorMessage($"Invalid country model with ID: {model.IdCountry}."); return(View(model)); } this.country.ModifyCountry(new CountryModel { IdCountry = model.IdCountry, Country = model.Country, RefSite = model.RefSite, SpphIdCountry = model.SpphIdCountry }); TempData.AddSuccessMessage($"Successfully modified a country with id: {model.IdCountry}."); return(RedirectToAction(nameof(Index))); }
public async Task Add_When_Name_Exists() { // arrange var countryService = new Mock <ICountryService>(MockBehavior.Strict); var controller = new CountryController(countryService.Object) { TempData = new Mock <ITempDataDictionary>().Object }; var existingName = "CountryName"; var model = new AddCountryViewModel { Name = existingName }; countryService.Setup(s => s.ExistsAsync(existingName)).ReturnsAsync(true); // act var result = await controller.Add(model); // assert Assert.IsAssignableFrom <ViewResult>(result); var resultModel = (result as ViewResult).Model; Assert.NotNull(resultModel); Assert.IsAssignableFrom <AddCountryViewModel>(resultModel); Assert.Equal(model, resultModel); Assert.False(controller.ModelState.IsValid); Assert.True(controller.ModelState.ContainsKey(nameof(AddCountryViewModel.Name))); var stateValues = controller.ModelState.Values.ToList(); Assert.True(stateValues.Count == 1); var valueMessages = stateValues[0].Errors.ToList(); Assert.True(valueMessages.Count == 1); Assert.True(valueMessages[0].ErrorMessage == CountryWithNameExistsMessage); countryService.VerifyAll(); }
/// <summary> /// Update country meta data /// </summary> /// <param name="model"></param> /// <returns></returns> public virtual async Task <ResultModel> UpdateCountryAsync(AddCountryViewModel model) { if (model == null) { return(new InvalidParametersResultModel()); } var country = await _context.Countries .AsNoTracking() .FirstOrDefaultAsync(x => x.Id.Equals(model.Id)); if (country == null) { return(new NotFoundResultModel()); } country.Code3 = model.Code3; country.IsBillingEnabled = model.IsBillingEnabled; country.IsCityEnabled = model.IsCityEnabled; country.IsDistrictEnabled = model.IsDistrictEnabled; country.IsShippingEnabled = model.IsShippingEnabled; country.IsZipCodeEnabled = model.IsZipCodeEnabled; country.Name = model.Name; _context.Countries.Update(country); return(await _context.PushAsync()); }
public void AddCountry(AddCountryViewModel model) { ContactsRepository.AddCountry(model.Name); }
public async Task <JsonResult> AddNewCountry([Required] AddCountryViewModel model) => !ModelState.IsValid ? Json(new ResultModel().AttachModelState(ModelState)) : Json(await _locationService.AddNewCountryAsync(model));
public ActionResult AddCountry(AddCountryViewModel addCountry) { return(View(addCountry)); }
public async Task <JsonResult> UpdateCountry([Required] AddCountryViewModel model) => !ModelState.IsValid ? Json(new ResultModel().AttachModelState(ModelState)) : Json(await _countryService.UpdateCountryAsync(model));