Ejemplo n.º 1
0
        public TodoChangeMessage DeleteTodo(string id)
        {
            var result = new TodoChangeMessage();
            var todo   = todoDictionary[id];

            if (todo.Parent != null)
            {
                var oldParent = todo.Parent;
                oldParent.DeleteChild(todo);
                result.Upsert = result.Upsert.Concat(new[] { TodoConvert.ConvertSingle(oldParent) });
            }
            else
            {
                topTodo.Remove(todo);
            }
            if (todo.HasChildren)
            {
                foreach (var children in todo.Children)
                {
                    result.Merge(DeleteTodoWithoutParent(children.Id));
                }
            }
            result.Delete = result.Delete.Concat(new[] { TodoConvert.ConvertSingle(todo) });
            todoDictionary.Remove(id);

            return(result);
        }
Ejemplo n.º 2
0
        private TodoChangeMessage AddTodo(TodoData data)
        {
            var result = new List <TodoData>();

            if (data.Parent != null && todoDictionary.ContainsKey(data.Parent))
            {
                var parent = todoDictionary[data.Parent];
                var child  = parent.Children?.SingleOrDefault(c => c.Id == data.Id);
                if (child != null)
                {
                    todoDictionary.Add(data.Id, child);
                    return(UpdateTodo(data));
                }
            }

            var timeRecords = data.TimeRecords == null? new TimeRecord[0] : data.TimeRecords.Select(time => new TimeRecord(time.Start, time.End)).ToArray();
            var todo        = new Todo(data.Name, data.EstimateTime, timeRecords, data.Attributes);

            todo.Id         = data.Id;
            todo.Compleated = data.Completed;
            todoDictionary.Add(data.Id, todo);
            if (data.Parent is null)
            {
                topTodo.Add(todo);
            }
            else
            {
                todo.IsChild = true;
                if (!todoDictionary.ContainsKey(data.Parent))
                {
                    AddTodo(new TodoData
                    {
                        Id           = data.Parent,
                        Parent       = null,
                        Attributes   = new Dictionary <string, string>(),
                        Completed    = false,
                        EstimateTime = TimeSpan.Zero,
                        Name         = $"temp parent :{data.Parent}",
                        TimeRecords  = Enumerable.Empty <TimeRecordData>()
                    });
                }

                var parent = todoDictionary[data.Parent];
                parent.AddChild(todo);
                result.Add(TodoConvert.ConvertSingle(parent));
            }
            result.Add(data);
            return(new TodoChangeMessage {
                Upsert = result
            });
        }
Ejemplo n.º 3
0
        private TodoChangeMessage DeleteTodoWithoutParent(string id)
        {
            var result = new TodoChangeMessage();

            var todo = todoDictionary[id];

            if (todo.HasChildren)
            {
                foreach (var children in todo.Children)
                {
                    result.Merge(DeleteTodoWithoutParent(children.Id));
                }
            }
            result.Delete = result.Delete.Concat(new[] { TodoConvert.ConvertSingle(todo) });
            todoDictionary.Remove(id);

            return(result);
        }
Ejemplo n.º 4
0
        private TodoChangeMessage UpdateTodo(TodoData data)
        {
            var result = false;
            var list   = new List <TodoData>();

            var todo = todoDictionary[data.Id];

            if (todo.Attribute?
                .OrderBy((o) => o.Key)
                .SequenceEqual(data.Attributes?
                               .OrderBy((o) => o.Key) ?? Enumerable.Empty <KeyValuePair <string, string> >()
                               )
                is not true)
            {
                todo.Attribute = data.Attributes;
                result         = true;
            }

            if (todo.TimeRecords?
                .OrderBy((o) => o.StartDateTime)
                .SequenceEqual(data.TimeRecords?.Select(data => new TimeRecord(data.Start, data.End))
                               .OrderBy((o) => o.StartDateTime) ?? Enumerable.Empty <TimeRecord>()
                               )
                is not true)
            {
                todo.RenewTimeRecords(data.TimeRecords.Select(data => new TimeRecord(data.Start, data.End)));
                result = true;
            }
            if (todo.Compleated != data.Completed)
            {
                todo.Compleated = data.Completed;
                result          = true;
            }

            if (todo.Name != data.Name)
            {
                todo.Name = data.Name;
                result    = true;
            }

            if (todo.EstimateTime != data.EstimateTime)
            {
                todo.EstimateTime = data.EstimateTime;
                result            = true;
            }

            if (todo.Parent?.Id != data.Parent)
            {
                if (todo.Parent != null)
                {
                    var oldParent = todo.Parent;
                    oldParent.DeleteChild(todo);
                    list.Add(TodoConvert.ConvertSingle(oldParent));
                }

                if (data.Parent != null)
                {
                    todo.IsChild = true;
                    if (!todoDictionary.ContainsKey(data.Parent))
                    {
                        AddTodo(new TodoData
                        {
                            Id           = data.Parent,
                            Parent       = null,
                            Attributes   = new Dictionary <string, string>(),
                            Completed    = false,
                            EstimateTime = TimeSpan.Zero,
                            Name         = $"temp parent :{data.Parent}",
                            TimeRecords  = Enumerable.Empty <TimeRecordData>()
                        });
                    }

                    var parent = todoDictionary[data.Parent];
                    parent.AddChild(todo);

                    if (topTodo.Contains(todo))
                    {
                        topTodo.Remove(todo);
                    }

                    list.Add(TodoConvert.ConvertSingle(parent));
                }

                result = true;
            }

            if (result)
            {
                list.Add(data);
            }
            return(new TodoChangeMessage {
                Upsert = list
            });
        }