public void UserPatchPopulateShouldKeepExistingWhenWithPropertyIsNull() { var existingUser = new User { FirstName = "First", LastName = "Last", Title = "Dr", Suffix = "Jr", DateOfBirth = new DateTime(2010, 5, 15), SocialSecurityNumber = "123456789", Email = "*****@*****.**", Password = "******", Status = UserStatus.Active }; var patchRequest = new UserPatchReqeust { LastName = "NewLast", Suffix = "NewJr", Password = "******" }; existingUser.PopulateWith(patchRequest); Assert.AreEqual(existingUser.FirstName, "First", "First should remain"); Assert.AreEqual(existingUser.Title, "Dr", "Title should remain"); Assert.AreEqual(existingUser.DateOfBirth, new DateTime(2010, 5, 15), "DateOfBirth should remain"); Assert.AreEqual(existingUser.SocialSecurityNumber, "123456789", "SocialSecurityNumber should remain"); Assert.AreEqual(existingUser.Email, "*****@*****.**", "Email should remain"); Assert.AreEqual(existingUser.Status, UserStatus.Active, "Status should remain"); Assert.AreEqual(existingUser.LastName, "NewLast", "Last should change"); Assert.AreEqual(existingUser.Suffix, "NewJr", "Suffix should change"); Assert.AreEqual(existingUser.Password, "newPassword", "Password should change"); }
public static void PopulateWith(this User source, UserPatchReqeust with) { // Normally would use a mapper here, but for now... source.FirstName = (with.FirstName ?? source.FirstName).ToNullIfEmpty(); source.LastName = (with.LastName ?? source.LastName).ToNullIfEmpty(); source.Title = (with.Title ?? source.Title).ToNullIfEmpty(); source.Suffix = (with.Suffix ?? source.Suffix).ToNullIfEmpty(); source.DateOfBirth = with.DateOfBirth ?? source.DateOfBirth; source.SocialSecurityNumber = (with.SocialSecurityNumber ?? source.SocialSecurityNumber).ToNullIfEmpty(); source.Email = (with.Email ?? source.Email).ToNullIfEmpty(); source.Password = (with.Password ?? source.Password).ToNullIfEmpty(); }
public ActionResult Patch(int id, [FromBody] UserPatchReqeust value) { var existingUser = _userRepository.GetById(id); if (existingUser == null) { return(NotFound()); } existingUser.PopulateWith(value); _userRepository.Update(existingUser); return(new StatusCodeResult((int)HttpStatusCode.NoContent)); }