Esempio n. 1
0
        private static async Task AssignTag(WorkspaceModel workspace, string tagName, TimeEntryModel timeEntry)
        {
            var store    = ServiceContainer.Resolve <IDataStore>();
            var existing = await store.Table <TagData>()
                           .QueryAsync(r => r.WorkspaceId == workspace.Id && r.Name == tagName)
                           .ConfigureAwait(false);

            var      checkRelation = true;
            TagModel tag;

            if (existing.Count > 0)
            {
                tag = new TagModel(existing [0]);
            }
            else
            {
                tag = new TagModel()
                {
                    Name      = tagName,
                    Workspace = workspace,
                };
                await tag.SaveAsync().ConfigureAwait(false);

                checkRelation = false;
            }

            if (timeEntry != null)
            {
                var assignTag = true;

                if (checkRelation)
                {
                    // Check if the relation already exists before adding it
                    var relations = await store.Table <TimeEntryTagData> ()
                                    .CountAsync(r => r.TimeEntryId == timeEntry.Id && r.TagId == tag.Id && r.DeletedAt == null)
                                    .ConfigureAwait(false);

                    if (relations < 1)
                    {
                        assignTag = false;
                    }
                }

                if (assignTag)
                {
                    var relationModel = new TimeEntryTagModel()
                    {
                        TimeEntry = timeEntry,
                        Tag       = tag,
                    };
                    await relationModel.SaveAsync().ConfigureAwait(false);

                    timeEntry.Touch();
                    await timeEntry.SaveAsync().ConfigureAwait(false);
                }
            }
        }
        private async static void ReplaceTags(TimeEntryModel model, List <TimeEntryTagData> modelTags, List <TagData> selectedTags)
        {
            // Delete unused tag relations:
            var deleteTasks = modelTags
                              .Where(oldTag => !selectedTags.Any(newTag => newTag.Id == oldTag.TagId))
                              .Select(data => new TimeEntryTagModel(data).DeleteAsync());

            // Create new tag relations:
            var createTasks = selectedTags
                              .Where(newTag => !modelTags.Any(oldTag => oldTag.TagId == newTag.Id))
                              .Select(data => new TimeEntryTagModel()
            {
                TimeEntry = model, Tag = new TagModel(data)
            }.SaveAsync());

            await Task.WhenAll(deleteTasks.Concat(createTasks));

            if (deleteTasks.Any <Task> () || createTasks.Any <Task> ())
            {
                model.Touch();
                await model.SaveAsync();
            }
        }