public async Task <EmptyResponseDto> RemoveTaskAsync(IEnumerable <string> taskIds) { return(await Task.Run(async() => { _logger.Information($"RemoveTaskAsync: Trying to remove following taskIDs = {string.Join(",", taskIds)}"); var response = new EmptyResponseDto { Message = string.Empty, Succeed = false }; using (var context = new MiraiNotesContext()) { try { var entities = context .Tasks .Where(t => taskIds.Any(a => a == t.GoogleTaskID)); if (entities.Count() == 0) { response.Message = "Couldn't find the tasks to delete"; _logger.Warning($"RemoveTaskAsync: Couldn't find any of the taskIDs = {string.Join(",", taskIds)}"); return response; } if (entities.Any(t => t.LocalStatus == LocalStatus.CREATED)) { var tasksToDelete = entities.Where(t => t.LocalStatus == LocalStatus.CREATED); var subTasks = context .Tasks .Where(st => tasksToDelete.Any(t => st.ParentTask == t.GoogleTaskID)); context.RemoveRange(tasksToDelete); if (subTasks.Count() > 0) { context.RemoveRange(subTasks); } } if (entities.Any(t => t.LocalStatus != LocalStatus.CREATED)) { var tasksToUpdate = entities.Where(t => t.LocalStatus != LocalStatus.CREATED); var subTasks = context .Tasks .Where(st => tasksToUpdate.Any(t => st.ParentTask == t.GoogleTaskID)); await tasksToUpdate.ForEachAsync(t => { t.UpdatedAt = DateTimeOffset.UtcNow; t.LocalStatus = LocalStatus.DELETED; t.ToBeSynced = true; }); await subTasks.ForEachAsync(t => { t.UpdatedAt = DateTimeOffset.UtcNow; t.LocalStatus = LocalStatus.DELETED; t.ToBeSynced = true; }); context.UpdateRange(tasksToUpdate); if (subTasks.Count() > 0) { 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)); }
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)); }