public static ProjectUserData Import(this ProjectUserJson json, IDataStoreContext ctx, Guid?localIdHint = null, ProjectUserData mergeBase = null) { var converter = ServiceContainer.Resolve <ProjectUserJsonConverter> (); return(converter.Import(ctx, json, localIdHint, mergeBase)); }
private static void ImportJson(IDataStoreContext ctx, ProjectUserData data, ProjectUserJson json) { var projectId = GetLocalId <ProjectData> (ctx, json.ProjectId); var userId = GetLocalId <UserData> (ctx, json.UserId); data.HourlyRate = json.HourlyRate; data.IsManager = json.IsManager; data.ProjectId = projectId; data.UserId = userId; ImportCommonJson(data, json); }
public ProjectUserJson Export(IDataStoreContext ctx, ProjectUserData data) { var projectId = GetRemoteId <ProjectData> (ctx, data.ProjectId); var userId = GetRemoteId <UserData> (ctx, data.UserId); return(new ProjectUserJson() { Id = data.RemoteId, ModifiedAt = data.ModifiedAt.ToUtc(), HourlyRate = data.HourlyRate, IsManager = data.IsManager, ProjectId = projectId, UserId = userId, }); }
public void ExportInvalidProjectAndUser() { ProjectUserData projectUserData = null; RunAsync(async delegate { projectUserData = await DataStore.PutAsync(new ProjectUserData() { RemoteId = 4, ProjectId = Guid.NewGuid(), UserId = Guid.NewGuid(), ModifiedAt = new DateTime(2014, 1, 3), }); }); Assert.That(() => RunAsync(async delegate { await DataStore.ExecuteInTransactionAsync(ctx => converter.Export(ctx, projectUserData)); }), Throws.Exception.TypeOf <RelationRemoteIdMissingException> ()); }
private IEnumerable <ForeignRelation> GetProjectUserRelations(ProjectUserData data) { yield return(new ForeignRelation() { Name = "ProjectId", Type = typeof(ProjectData), Required = true, Id = data.ProjectId, }); yield return(new ForeignRelation() { Name = "UserId", Type = typeof(UserData), Required = true, Id = data.UserId, }); }
public static ProjectUserJson Export(this ProjectUserData data, IDataStoreContext ctx) { var converter = ServiceContainer.Resolve <ProjectUserJsonConverter> (); return(converter.Export(ctx, data)); }
private static async Task <DateTime> PullChanges(DateTime?lastRun) { var client = ServiceContainer.Resolve <ITogglClient> (); var store = ServiceContainer.Resolve <IDataStore> (); var log = ServiceContainer.Resolve <Logger> (); if (lastRun == null) { log.Info(Tag, "Importing all user data from server."); } else { log.Info(Tag, "Importing changes from server (since {0}).", lastRun); } var changes = await client.GetChanges(lastRun).ConfigureAwait(false); // Import data (in parallel batches) var userData = await store.ExecuteInTransactionAsync(ctx => changes.User.Import(ctx)); await store.ExecuteInTransactionAsync(ctx => { foreach (var json in changes.Workspaces) { var workspaceData = json.Import(ctx); // Make sure that the user relation exists and is up to date if (workspaceData != null) { var workspaceUserRows = ctx.Connection.Table <WorkspaceUserData> () .Where(r => r.WorkspaceId == workspaceData.Id && r.UserId == userData.Id && r.DeletedAt == null); var workspaceUserData = workspaceUserRows.FirstOrDefault(); if (workspaceUserData == null) { workspaceUserData = new WorkspaceUserData() { WorkspaceId = workspaceData.Id, UserId = userData.Id, IsAdmin = json.IsAdmin, }; } else { workspaceUserData.IsAdmin = json.IsAdmin; } ctx.Put(workspaceUserData); } } }); await store.ExecuteInTransactionAsync(ctx => { foreach (var json in changes.Tags) { json.Import(ctx); } }); await store.ExecuteInTransactionAsync(ctx => { foreach (var json in changes.Clients) { json.Import(ctx); } }); await store.ExecuteInTransactionAsync(ctx => { foreach (var json in changes.Projects) { var projectData = json.Import(ctx); // Make sure that the user relation exists if (projectData != null) { var projectUserRows = ctx.Connection.Table <ProjectUserData> () .Where(r => r.ProjectId == projectData.Id && r.UserId == userData.Id && r.DeletedAt == null); var projectUserData = projectUserRows.FirstOrDefault(); if (projectUserData == null) { projectUserData = new ProjectUserData() { ProjectId = projectData.Id, UserId = userData.Id, }; ctx.Put(projectUserData); } } } }); await store.ExecuteInTransactionAsync(ctx => { foreach (var json in changes.Tasks) { json.Import(ctx); } }); await store.ExecuteInTransactionAsync(ctx => { foreach (var json in changes.TimeEntries) { json.Import(ctx); } }); return(changes.Timestamp); }
public ProjectUserData Import(IDataStoreContext ctx, ProjectUserJson json, Guid?localIdHint = null, ProjectUserData mergeBase = null) { var log = ServiceContainer.Resolve <ILogger> (); var data = GetByRemoteId <ProjectUserData> (ctx, json.Id.Value, localIdHint); var merger = mergeBase != null ? new ProjectUserMerger(mergeBase) : null; if (merger != null && data != null) { merger.Add(new ProjectUserData(data)); } if (json.DeletedAt.HasValue) { if (data != null) { log.Info(Tag, "Deleting local data for {0}.", data.ToIdString()); ctx.Delete(data); data = null; } } else if (merger != null || ShouldOverwrite(data, json)) { data = data ?? new ProjectUserData(); ImportJson(ctx, data, json); if (merger != null) { merger.Add(data); data = merger.Result; } if (merger != null) { log.Info(Tag, "Importing {0}, merging with local data.", data.ToIdString()); } else { log.Info(Tag, "Importing {0}, replacing local data.", data.ToIdString()); } data = ctx.Put(data); } else { log.Info(Tag, "Skipping import of {0}.", json.ToIdString()); } return(data); }
private static async Task <DateTime> PullChanges(DateTime?lastRun) { if (ServiceContainer.Resolve <AuthManager> ().OfflineMode) { return(DateTime.Now); } var client = ServiceContainer.Resolve <ITogglClient> (); var store = ServiceContainer.Resolve <IDataStore> (); var log = ServiceContainer.Resolve <ILogger> (); if (lastRun == null) { log.Info(Tag, "Importing all user data from server."); } else { log.Info(Tag, "Importing changes from server (since {0}).", lastRun); } var changes = await client.GetChanges(lastRun).ConfigureAwait(false); // TODO: OBM data that comes in user object from this changes // is totally wrong. In that way, we should keep this info before // before process the object. var user = ServiceContainer.Resolve <AuthManager> ().User; if (user != null) { changes.User.OBM.Included = user.ExperimentIncluded; changes.User.OBM.Number = user.ExperimentNumber; } // Import data (in parallel batches) var userData = await store.ExecuteInTransactionAsync(ctx => changes.User.Import(ctx)); await store.ExecuteInTransactionAsync(ctx => { foreach (var json in changes.Workspaces) { var workspaceData = json.Import(ctx); // Make sure that the user relation exists and is up to date if (workspaceData != null) { var workspaceUserRows = ctx.Connection.Table <WorkspaceUserData> () .Where(r => r.WorkspaceId == workspaceData.Id && r.UserId == userData.Id && r.DeletedAt == null); var workspaceUserData = workspaceUserRows.FirstOrDefault(); if (workspaceUserData == null) { workspaceUserData = new WorkspaceUserData() { WorkspaceId = workspaceData.Id, UserId = userData.Id, IsAdmin = json.IsAdmin, }; } else { workspaceUserData.IsAdmin = json.IsAdmin; } ctx.Put(workspaceUserData); } } }); await store.ExecuteInTransactionAsync(ctx => { foreach (var json in changes.Tags) { json.Import(ctx); } }); await store.ExecuteInTransactionAsync(ctx => { foreach (var json in changes.Clients) { json.Import(ctx); } }); await store.ExecuteInTransactionAsync(ctx => { foreach (var json in changes.Projects) { var projectData = json.Import(ctx); // Make sure that the user relation exists if (projectData != null) { var projectUserRows = ctx.Connection.Table <ProjectUserData> () .Where(r => r.ProjectId == projectData.Id && r.UserId == userData.Id && r.DeletedAt == null); var projectUserData = projectUserRows.FirstOrDefault(); if (projectUserData == null) { projectUserData = new ProjectUserData() { ProjectId = projectData.Id, UserId = userData.Id, }; ctx.Put(projectUserData); } } } }); await store.ExecuteInTransactionAsync(ctx => { foreach (var json in changes.Tasks) { json.Import(ctx); } }); await store.ExecuteInTransactionAsync(ctx => { foreach (var json in changes.TimeEntries) { json.Import(ctx); } }); return(changes.Timestamp); }