public IActionResult Create([FromForm] NaturalPersonViewModel model) { try { if (ModelState.IsValid) { _personService.Add(model); return(RedirectToAction(nameof(Index))); } } catch (BusinessRuleException ex) { ModelState.AddModelError(ex.Key, ex.Message); } catch (Exception ex) { _logger.LogError(ex.Message); ModelState.AddModelError(string.Empty, _sharedLocalizer["GenericErrorMessage"]); } ViewBag.CountryList = _addressService.GetContriesName(); return(View(model)); }
public async Task <ActionResult> Edit(int id, [FromForm] NaturalPersonViewModel model) { if (id != model.Id) { return(NotFound()); } try { if (ModelState.IsValid) { await _personService.UpdateAsync(model); return(RedirectToAction(nameof(Index))); } } catch (BusinessRuleException ex) { ModelState.AddModelError(ex.Key, ex.Message); } catch (Exception ex) { _logger.LogError(ex.Message); ModelState.AddModelError(string.Empty, _sharedLocalizer["GenericErrorMessage"]); } ViewBag.CountryList = _addressService.GetContriesName(); return(View(model)); }
public async Task UpdateAsync(NaturalPersonViewModel model) { var person = await _personRepository.GetAsync(model.Id); if (person == null) { return; } person.Name = model.Name; person.Gender = model.Gender.Value; person.Email = model.Email; person.SetBirthday(model.Birthday); person.SetIdDocument(model.IdDocument); // Address is optional, if country not informed delete address entity if (!string.IsNullOrWhiteSpace(model.Country)) { if (person.Address == null) { person.Address = new Address( model.Country, model.State, model.City, model.ZipCode, model.AddressLine1, model.AddressLine2); } else { person.Address.ChangeAddress( model.Country, model.State, model.City, model.ZipCode, model.AddressLine1, model.AddressLine2); } } else { person.Address = null; } _personRepository.Update(person); _personRepository.SaveChanges(); }
public void Add(NaturalPersonViewModel model) { if (!_personRepository.IsIdDocumentUnique(model.IdDocument)) { throw new BusinessRuleException("IdDocument", "ID Document already informed."); } if (!_personRepository.IsEmailUnique(model.Email)) { throw new BusinessRuleException("Email", "Email already informed."); } var person = new NaturalPerson( model.Name, model.IdDocument, model.Birthday, model.Gender.Value, model.Email) { Address = new Address( model.Country, model.State, model.City, model.ZipCode, model.AddressLine1, model.AddressLine2) }; _personRepository.Add(person); _personRepository.SaveChanges(); }
private async Task SignUpAsync() { var success = false; try { switch (PersonType) { case PersonType.NaturalPerson: // set naturalPerson values NaturalPersonViewModel.NaturalPerson.PrimaryEmail = CredentialViewModel.UserCredential.PrimaryEmail; NaturalPersonViewModel.Password = CredentialViewModel.UserCredential.Password; // first validate naturalPersonViewModel and credentialViewModel // this.IsPersonValid = NaturalPersonViewModel.ValidateForm(); this.IsCredentialValid = CredentialViewModel.ValidateForm(); // if validation is true signup person if (this.IsPersonValid && this.IsCredentialValid) { await NaturalPersonViewModel.ProcessFormAsync(); success = true; } break; case PersonType.LegalPerson: // set legalPerson values LegalPersonViewModel.LegalPerson.PrimaryEmail = CredentialViewModel.UserCredential.PrimaryEmail; LegalPersonViewModel.Password = CredentialViewModel.UserCredential.Password; // first validate naturalPersonViewModel and credentialViewModel // this.IsPersonValid = LegalPersonViewModel.ValidateForm(); this.IsCredentialValid = CredentialViewModel.ValidateForm(); // if validation is true signup person if (this.IsPersonValid && this.IsCredentialValid) { await LegalPersonViewModel.ProcessFormAsync(); success = true; } break; case PersonType.None: break; default: throw new ArgumentOutOfRangeException(); } //if success clear sencetive information from memory and navigate to confirm registration page if (success) { NaturalPersonViewModel.Password = LegalPersonViewModel.Password = null; _navigationService.Navigate(ViewNames.ConfirmRegistration, null); } } catch (Exception) { await _alertMessageService.ShowAsync(_resourceLoader.GetString("ErrorServiceUnreachable"), null, DialogCommands.CloseDialogCommand); } }