Beispiel #1
0
        public async Task MapTagsFromModel(TimeEntryModel model)
        {
            var dataStore = ServiceContainer.Resolve <IDataStore> ();

            var oldTags = await dataStore.Table <TimeEntryTagData> ()
                          .QueryAsync(r => r.TimeEntryId == Id && r.DeletedAt == null);

            var task1 = oldTags.Select(d => new TimeEntryTagModel(d).DeleteAsync()).ToList();

            var modelTags = await dataStore.Table <TimeEntryTagData> ()
                            .QueryAsync(r => r.TimeEntryId == model.Id && r.DeletedAt == null);

            var task2 = modelTags.Select(d => new TimeEntryTagModel()
            {
                TimeEntry = this, Tag = new TagModel(d.TagId)
            }.SaveAsync()).ToList();

            await System.Threading.Tasks.Task.WhenAll(task1.Concat(task2));

            if (modelTags.Count > 0)
            {
                Touch();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Continues the finished time entry, either by creating a new time entry or restarting the current one.
        /// </summary>
        public async Task <TimeEntryModel> ContinueAsync()
        {
            var store = ServiceContainer.Resolve <IDataStore> ();

            await LoadAsync();

            // Validate the current state
            switch (Data.State)
            {
            case TimeEntryState.Running:
                return(this);

            case TimeEntryState.Finished:
                break;

            default:
                throw new InvalidOperationException(String.Format("Cannot continue a time entry in {0} state.", Data.State));
            }

            // We can continue time entries which haven't been synced yet:
            if (Data.DurationOnly && Data.StartTime.ToLocalTime().Date == Time.Now.Date)
            {
                if (Data.RemoteId == null)
                {
                    MutateData(data => {
                        data.State     = TimeEntryState.Running;
                        data.StartTime = Time.UtcNow - GetDuration();
                        data.StopTime  = null;
                    });

                    await SaveAsync();

                    return(this);
                }
            }

            // Create new time entry:
            var newData = new TimeEntryData()
            {
                WorkspaceId  = Data.WorkspaceId,
                ProjectId    = Data.ProjectId,
                TaskId       = Data.TaskId,
                UserId       = Data.UserId,
                Description  = Data.Description,
                StartTime    = Time.UtcNow,
                DurationOnly = Data.DurationOnly,
                IsBillable   = Data.IsBillable,
                State        = TimeEntryState.Running,
            };

            MarkDirty(newData);

            var parentId = Data.Id;
            await store.ExecuteInTransactionAsync(ctx => {
                newData = ctx.Put(newData);

                // Duplicate tag relations as well
                if (parentId != Guid.Empty)
                {
                    var q = ctx.Connection.Table <TimeEntryTagData> ()
                            .Where(r => r.TimeEntryId == parentId && r.DeletedAt == null);
                    foreach (var row in q)
                    {
                        ctx.Put(new TimeEntryTagData()
                        {
                            TimeEntryId = newData.Id,
                            TagId       = row.TagId,
                        });
                    }
                }
            });

            var model = new TimeEntryModel(newData);

            return(model);
        }