Align the tasks route API to the new spec

This commit is contained in:
Kerollmops 2022-06-01 15:30:39 +02:00
parent c11d21879a
commit d80e8b64af
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
2 changed files with 11 additions and 22 deletions

View File

@ -30,7 +30,7 @@ pub struct TaskFilterQuery {
index_uid: Option<CS<StarOr<IndexUid>>>, index_uid: Option<CS<StarOr<IndexUid>>>,
#[serde(default = "DEFAULT_LIMIT")] #[serde(default = "DEFAULT_LIMIT")]
limit: usize, limit: usize,
after: Option<TaskId>, from: Option<TaskId>,
} }
#[rustfmt::skip] #[rustfmt::skip]
@ -74,7 +74,7 @@ async fn get_tasks(
status, status,
index_uid, index_uid,
limit, limit,
after, from,
} = params.into_inner(); } = params.into_inner();
let search_rules = &meilisearch.filters().search_rules; let search_rules = &meilisearch.filters().search_rules;
@ -135,25 +135,11 @@ async fn get_tasks(
indexes_filters indexes_filters
}; };
let offset = match after {
Some(0) => {
let tasks = TaskListView {
results: vec![],
limit,
after: None,
};
return Ok(HttpResponse::Ok().json(tasks));
}
// We -1 here because we need an offset and we must exclude the `after` one.
Some(n) => Some(n - 1),
None => None,
};
// We +1 just to know if there is more after this "page" or not. // We +1 just to know if there is more after this "page" or not.
let limit = limit.saturating_add(1); let limit = limit.saturating_add(1);
let mut tasks_results = meilisearch let mut tasks_results = meilisearch
.list_tasks(filters, Some(limit), offset) .list_tasks(filters, Some(limit), from)
.await? .await?
.into_iter() .into_iter()
.map(TaskView::from) .map(TaskView::from)
@ -161,17 +147,19 @@ async fn get_tasks(
// If we were able to fetch the number +1 tasks we asked // If we were able to fetch the number +1 tasks we asked
// it means that there is more to come. // it means that there is more to come.
let after = if tasks_results.len() == limit { let next = if tasks_results.len() == limit {
tasks_results.pop(); tasks_results.pop().map(|t| t.uid)
tasks_results.last().map(|t| t.uid)
} else { } else {
None None
}; };
let from = tasks_results.first().map(|t| t.uid);
let tasks = TaskListView { let tasks = TaskListView {
results: tasks_results, results: tasks_results,
limit: limit.saturating_sub(1), limit: limit.saturating_sub(1),
after, from,
next,
}; };
Ok(HttpResponse::Ok().json(tasks)) Ok(HttpResponse::Ok().json(tasks))

View File

@ -371,7 +371,8 @@ impl From<Task> for TaskView {
pub struct TaskListView { pub struct TaskListView {
pub results: Vec<TaskView>, pub results: Vec<TaskView>,
pub limit: usize, pub limit: usize,
pub after: Option<TaskId>, pub from: Option<TaskId>,
pub next: Option<TaskId>,
} }
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]