private async Task <TaskResponseBase> DeleteTask(Task task, bool keepSubtasks) { var subtasks = await _dbContext.Tasks.Where(predicate : t => t.ParentId == task.Id) .ToListAsync(); if (subtasks != null) { if (keepSubtasks) { foreach (var subtask in subtasks) { subtask.ParentId = Guid.Empty; } } else { foreach (var subtask in subtasks) { _deleted.Add(item: await DeleteTask(task: subtask, keepSubtasks: keepSubtasks)); } } } _dbContext.Tasks.Remove(entity: task); return(new TaskResponseBase { Id = task.Id, Name = task.Name }); }
private void DataGenerator() { int numOfTasks = 3; int numOfSubtasks = 3; var tasks = new List <Task>(new Task[numOfTasks] /*{new Task(), new Task(), new Task()}*/); for (var i = 0; i < tasks.Count; i++) { tasks[i] = new Task(); // { // Id = DateTimeOffset.UtcNow.ToUnixTimeSeconds() // }; // var stopwatch = Stopwatch.StartNew(); // Thread.Sleep(1100); // stopwatch.Stop(); } var subtasksList = new List <List <Task> >(new List <Task> [tasks.Count]); for (var i = 0; i < subtasksList.Count; i++) { subtasksList[i] = new List <Task>(new Task[numOfSubtasks]); for (var j = 0; j < subtasksList[i].Count; j++) { subtasksList[i][j] = new Task { Id = new Guid() }; // Id = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() // }; // var stopwatch = Stopwatch.StartNew(); // Thread.Sleep(100); // stopwatch.Stop(); } } int index = 0; tasks.ForEach(task => { ++index; task.Name = $"Task {index}"; task.Description = $"Description for task {index}"; task.Assignee = $"Assignee for task {index}"; task.CreatedAt = DateTime.Now; task.CompletedAt = default; task.Status = Status.Created; task.Subtasks = subtasksList[index - 1]; task.ParentId = null; // task.Subtasks = subtasksList.Count > index ? subtasksList[index - 1] : subtasksList[^1]; int subindex = 0; foreach (Task subtask in task.Subtasks) { ++subindex; subtask.Name = $"Subtask {subindex} for task {index}"; subtask.Description = $"Description for subtask {subindex} of task {index}"; subtask.Assignee = $"Assignee for subtask {subindex} of task {index}"; subtask.CreatedAt = DateTime.Now; subtask.CompletedAt = default; subtask.Status = Status.Created; subtask.ParentId = task.Id; subtask.Subtasks = new List <Task>(new Task[1] { new Task { //Id = numOfTasks + numOfSubtasks + subindex + 1, Name = $"Sub-subtask for subtask {subindex} of task {index}", Description = $"", Assignee = $"", CreatedAt = DateTime.Now, CompletedAt = default,