diff --git a/src/Meilisearch/Index.Tasks.cs b/src/Meilisearch/Index.Tasks.cs index 0d7f1c8b..a24a9aff 100644 --- a/src/Meilisearch/Index.Tasks.cs +++ b/src/Meilisearch/Index.Tasks.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -13,15 +14,22 @@ public partial class Index /// /// Query parameters supports by the method. /// The cancellation token for this call. + /// Whether to reverse the order of the tasks by EnqueuedAt. /// Returns a list of the operations status. - public async Task>> GetTasksAsync(TasksQuery query = null, CancellationToken cancellationToken = default) + public async Task>> GetTasksAsync(TasksQuery query = null, CancellationToken cancellationToken = default, bool reverseOrder = false) { if (query == null) { query = new TasksQuery { IndexUids = new List { this.Uid } }; } - return await TaskEndpoint().GetTasksAsync(query, cancellationToken).ConfigureAwait(false); + var tasks = await TaskEndpoint().GetTasksAsync(query, cancellationToken).ConfigureAwait(false); + + return reverseOrder ? new TasksResults>(tasks.Results.OrderBy(t => t.EnqueuedAt), + tasks.Limit, + tasks.From, + tasks.Next, + tasks.Total) : tasks; } /// diff --git a/tests/Meilisearch.Tests/TaskInfoTests.cs b/tests/Meilisearch.Tests/TaskInfoTests.cs index f5cb51bb..01e8691d 100644 --- a/tests/Meilisearch.Tests/TaskInfoTests.cs +++ b/tests/Meilisearch.Tests/TaskInfoTests.cs @@ -103,5 +103,18 @@ public async Task WaitForTaskWithException() var task = await _index.AddDocumentsAsync(new[] { new Movie { Id = "5" } }); await Assert.ThrowsAsync(() => _index.WaitForTaskAsync(task.TaskUid, 0.0, 20)); } + + [Fact] + public async Task ReverseTasksByEnqueuedAt() + { + var task1 = await _index.AddDocumentsAsync(new[] { new Movie { Id = "6" } }); + var task2 = await _index.AddDocumentsAsync(new[] { new Movie { Id = "7" } }); + var task3 = await _index.AddDocumentsAsync(new[] { new Movie { Id = "8" } }); + + var tasks = await _index.GetTasksAsync(reverseOrder: true); + var results = tasks.Results.ToList(); + + tasks.Should().NotBeNull(); + results.Select(t => t.EnqueuedAt).Should().BeInDescendingOrder(); } }