public UserApplication Save(UserApplication data, UserApplication editor) { Contract.Requires <ArgumentNullException>(null != data); Contract.Requires <ArgumentNullException>(null != editor); Contract.Requires <ArgumentNullException>(null != editor.Application); Contract.Requires <ArgumentNullException>(null != editor.User); Contract.Requires <ArgumentNullException>(null != data.Application); Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != data.Application.Identifier); Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != editor.User.Identifier); Contract.Requires <ArgumentOutOfRangeException>(Guid.Empty != editor.Application.Identifier); Contract.Ensures(Contract.Result <UserApplication>() != null); using (new PerformanceMonitor()) { if (!this.UserIsAssociated(editor, data.Application)) { throw new SecurityException("Editor is not associated with Application."); } var table = new AzureTable <UserApplicationData>(ServerConfiguration.Default, new UserApplicationValidation()); var existing = table.QueryBy(data.User.Identifier.ToString(), data.Application.Identifier.ToString()); if (null == existing) { existing = data.Convert(); existing.CreatedOn = DateTime.UtcNow; existing.LastUpdatedOn = DateTime.UtcNow; existing.CreatedBy = editor.User.Identifier; existing.LastUpdatedBy = editor.User.Identifier; table.AddEntity(existing); } else { var createdBy = existing.CreatedBy; var createdOn = existing.CreatedOn; existing = data.Convert(); existing.CreatedBy = createdBy; existing.CreatedOn = createdOn; existing.LastUpdatedOn = DateTime.UtcNow; existing.LastUpdatedBy = editor.User.Identifier; table.AddOrUpdateEntity(existing); } return(data); } }
public void Convert() { var user = new User() { Identifier = Guid.NewGuid(), }; var application = new Application() { Identifier = Guid.NewGuid(), }; var ua = new UserApplication() { User = user, Active = true, Application = application, Deleted = true, }; var converted = ua.Convert(); Assert.AreEqual <bool>(ua.Active, converted.Active); Assert.AreEqual <bool>(ua.Deleted, converted.Deleted); Assert.AreEqual <Guid>(ua.User.Identifier, converted.UserId); Assert.AreEqual <Guid>(ua.Application.Identifier, converted.ApplicationId); }