public async Task CreateOwner(OnboardFormViewModel form) { try { var owner = new User { FirstName = form.User.FirstName, LastName = form.User.LastName, AddressLine1 = form.User.AddressLine1, AddressLine2 = form.User.AddressLine2, City = form.User.City, State = form.User.State, ZipCode = form.User.ZipCode, Email = form.User.Email, UserGuid = Guid.NewGuid() }; var pet = new Pet { Name = form.Pet.Name, Age = form.Pet.Age, Breed = form.Pet.Breed, OwnerId = owner.UserGuid }; _context.User.Add(owner); _context.Pet.Add(pet); await _context.SaveChangesAsync(); } catch (Exception ex) { throw new DataAccessException("An error occurred while attempting to create the user.", ex); } }
public async Task <IActionResult> Create(OnboardFormViewModel form) { try { if (!ModelState.IsValid) { return(View(form)); } await _userManager.CreateOwner(form); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { if (ex is DataAccessException) { return(StatusCode(StatusCodes.Status400BadRequest, "An error occurred while attempting to retreieve this resource.")); } return(StatusCode(StatusCodes.Status500InternalServerError, "An unexpected error occurred while processing this request")); } }