public async Task <EmptyResponseDto> RemoveTaskAsync(string taskID) { return(await Task.Run(async() => { _logger.Information($"RemoveTaskAsync: Trying to remove taskID = {taskID}"); var response = new EmptyResponseDto { Message = string.Empty, Succeed = false }; using (var context = new MiraiNotesContext()) { try { var entity = await context .Tasks .FirstOrDefaultAsync(t => t.GoogleTaskID == taskID); if (entity == null) { response.Message = "Couldn't find the task to delete"; _logger.Warning($"RemoveTaskAsync: Couldn't find the task with taskID = {taskID}"); return response; } var subTasks = context .Tasks .Where(t => t.ParentTask == entity.GoogleTaskID); if (entity.LocalStatus == LocalStatus.CREATED) { context.Remove(entity); if (subTasks.Count() > 0) { context.RemoveRange(subTasks); } } else { entity.LocalStatus = LocalStatus.DELETED; entity.UpdatedAt = DateTimeOffset.UtcNow; entity.ToBeSynced = true; context.Update(entity); if (subTasks.Count() > 0) { await subTasks.ForEachAsync(st => { st.LocalStatus = LocalStatus.DELETED; st.UpdatedAt = DateTimeOffset.UtcNow; st.ToBeSynced = true; }); context.UpdateRange(subTasks); } } response.Succeed = await context.SaveChangesAsync() > 0; _logger.Information("RemoveTaskAsync: Completed successfully"); } catch (Exception e) { _logger.Error(e, "RemoveTaskAsync: An unknown error occurred"); response.Message = GetExceptionMessage(e); } } return response; }).ConfigureAwait(false)); }