protected override void InitializeRelations() { base.InitializeRelations(); workspace = new ForeignRelation <WorkspaceModel> () { ShouldLoad = EnsureLoaded, Factory = id => new WorkspaceModel(id), Changed = m => MutateData(data => data.WorkspaceId = m.Id), }; }
protected override void InitializeRelations() { base.InitializeRelations(); project = new ForeignRelation <ProjectModel> () { ShouldLoad = EnsureLoaded, Factory = id => new ProjectModel(id), Changed = m => MutateData(data => data.ProjectId = m.Id), }; user = new ForeignRelation <UserModel> () { ShouldLoad = EnsureLoaded, Factory = id => new UserModel(id), Changed = m => MutateData(data => data.UserId = m.Id), }; }
protected override void InitializeRelations() { base.InitializeRelations(); timeEntry = new ForeignRelation <TimeEntryModel> () { ShouldLoad = EnsureLoaded, Factory = id => new TimeEntryModel(id), Changed = m => MutateData(data => data.TimeEntryId = m.Id), }; tag = new ForeignRelation <TagModel> () { ShouldLoad = EnsureLoaded, Factory = id => new TagModel(id), Changed = m => MutateData(data => data.TagId = m.Id), }; }
protected override void InitializeRelations() { base.InitializeRelations(); workspace = new ForeignRelation <WorkspaceModel> () { ShouldLoad = EnsureLoaded, Factory = id => new WorkspaceModel(id), Changed = m => MutateData(data => data.WorkspaceId = m.Id), }; client = new ForeignRelation <ClientModel> () { Required = false, ShouldLoad = EnsureLoaded, Factory = id => new ClientModel(id), Changed = m => MutateData(data => data.ClientId = GetOptionalId(m)), }; }
protected override void InitializeRelations() { base.InitializeRelations(); user = new ForeignRelation <UserModel> () { ShouldLoad = EnsureLoaded, Factory = id => new UserModel(id), Changed = m => MutateData(data => data.UserId = m.Id), }; workspace = new ForeignRelation <WorkspaceModel> () { ShouldLoad = EnsureLoaded, Factory = id => new WorkspaceModel(id), Changed = m => MutateData(data => data.WorkspaceId = m.Id), }; project = new ForeignRelation <ProjectModel> () { Required = false, ShouldLoad = EnsureLoaded, Factory = id => new ProjectModel(id), Changed = m => MutateData(data => { if (m != null) { data.IsBillable = m.IsBillable; } data.ProjectId = GetOptionalId(m); }), }; task = new ForeignRelation <TaskModel> () { Required = false, ShouldLoad = EnsureLoaded, Factory = id => new TaskModel(id), Changed = m => MutateData(data => data.TaskId = GetOptionalId(m)), }; }
/// <summary> /// Stores the draft time entry in model store as a finished time entry. /// </summary> public async Task StoreAsync() { await LoadAsync(); if (Data.State != TimeEntryState.New) { throw new InvalidOperationException(String.Format("Cannot store a time entry in {0} state.", Data.State)); } if (Data.StartTime.IsMinValue() || Data.StopTime == null) { throw new InvalidOperationException("Cannot store time entry with start/stop time not set."); } var task = Task; var project = Project; var workspace = Workspace; var user = User; // Preload all pending relations: var pending = new List <Task> (); if (task != null) { pending.Add(task.LoadAsync()); } if (project != null) { pending.Add(project.LoadAsync()); } if (workspace != null) { pending.Add(workspace.LoadAsync()); } if (user != null) { pending.Add(user.LoadAsync()); } await System.Threading.Tasks.Task.WhenAll(pending); if (ModelExists(task)) { project = task.Project; } if (ModelExists(project)) { workspace = project.Workspace; } if (ModelExists(user) && !ModelExists(workspace)) { workspace = user.DefaultWorkspace; } if (!ModelExists(workspace)) { throw new InvalidOperationException("Workspace (or user default workspace) must be set."); } MutateData(data => { data.TaskId = task != null ? (Guid?)task.Id : null; data.ProjectId = project != null ? (Guid?)project.Id : null; data.WorkspaceId = workspace.Id; data.UserId = user.Id; data.State = TimeEntryState.Finished; }); await SaveAsync(); await AddDefaultTags(); }
public static async Task <TimeEntryModel> GetDraftAsync() { TimeEntryData data = null; var user = ServiceContainer.Resolve <AuthManager> ().User; if (user == null) { return(null); } // We're already loading draft data, wait for it to load, no need to create several drafts if (draftDataTCS != null) { data = await draftDataTCS.Task; if (data == null) { return(null); } data = new TimeEntryData(data); return(new TimeEntryModel(data)); } draftDataTCS = new TaskCompletionSource <TimeEntryData> (); try { var store = ServiceContainer.Resolve <IDataStore> (); if (user.DefaultWorkspaceId == Guid.Empty) { // User data has not yet been loaded by AuthManager, duplicate the effort and load ourselves: var userRows = await store.Table <UserData> () .Take(1).QueryAsync(m => m.Id == user.Id); user = userRows.First(); } var rows = await store.Table <TimeEntryData> () .Where(m => m.State == TimeEntryState.New && m.DeletedAt == null && m.UserId == user.Id) .OrderBy(m => m.ModifiedAt) .Take(1).QueryAsync(); data = rows.FirstOrDefault(); if (data == null) { // Create new draft object var newData = new TimeEntryData() { State = TimeEntryState.New, UserId = user.Id, WorkspaceId = user.DefaultWorkspaceId, DurationOnly = user.TrackingMode == TrackingMode.Continue, }; MarkDirty(newData); await store.ExecuteInTransactionAsync(ctx => { newData = ctx.Put(newData); if (ShouldAddDefaultTag) { AddDefaultTags(ctx, newData.WorkspaceId, newData.Id); } }); data = newData; } } catch (Exception ex) { var log = ServiceContainer.Resolve <ILogger> (); log.Warning(Tag, ex, "Failed to retrieve/create draft."); } finally { draftDataTCS.SetResult(data); draftDataTCS = null; } return(new TimeEntryModel(data)); }