public IActionResult Create( Guid personGuid, PersonNameCreateModel createModel) { // Must have an GUID if (personGuid == Guid.Empty) return NotFound(); // Must have a person var person = _personData.ReadPersonWithNames(personGuid); // Do we have what we need so far? if (person == null) return NotFound(); // Page navigation ViewData["PersonGuid"] = person.PersonGuid; // // Validate // var form = new PersonNameModelState( ModelState, new Logic.Validate.PersonName( createModel.Prefix, createModel.First, createModel.Middle, createModel.Last, createModel.Suffix, null), // no name weight value in creation form person.Names, null); form.HasValidValues(); if (!ModelState.IsValid) return View(createModel); form.HasUniquePersonNameToBeCreated(); if (!ModelState.IsValid) return View(createModel); // // Create // var newName = new PersonName(); newName.PersonId = person.PersonId; newName.Prefix = createModel.Prefix; newName.First = createModel.First; newName.Middle = createModel.Middle; newName.Last = createModel.Last; newName.Suffix = createModel.Suffix; // TimeCreated and TimeLastUpdated is set at class creation. // New name is added at the bottom of the list. newName.NameWeight = person.Names.Count + 1; try { _personData.AddPersonName(newName); } catch (DbUpdateException /* ex */) { //Log the error (uncomment ex variable name and write a log.) ModelState.AddModelError( "", "Unable to save changes. " + "Try again, and if the problem persists, " + "see your system administrator."); return View(createModel); } // // Update RDF // var readPerson = _personData.ReadAllPersonData(personGuid); if (readPerson != null) _rdfData.AddOrUpdatePerson(readPerson); // // Update search index // var names = new List<string>(); person = _personData.ReadPersonWithNames(personGuid); foreach (var name in person.Names.OrderByDescending(x => x.NameWeight)) { var fullName = string.Join(" ", new[] { name.Prefix, name.First, name.Middle, name.Last, name.Suffix }.Where(x => !string.IsNullOrWhiteSpace(x)).ToArray()); names.Add(fullName); } // TODO try catch to handle errors _personSearchIndex.MergeNames( person.PersonGuid.ToString(), names.ToArray()); // // Redirect // return RedirectToAction( "Details", "Person", new { personGuid = personGuid }); }