public TodoChangeMessage UpsertTodo(TodoData data) { if (todoDictionary.ContainsKey(data.Id)) { return(UpdateTodo(data)); } else { return(AddTodo(data)); } }
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 }); }
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 }); }