Skip to content

Commit d1dc40b

Browse files
committed
controllers/helpers/pagination: Add an option to enable seeking backward
1 parent f697740 commit d1dc40b

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/controllers/helpers/pagination.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ impl PaginationOptions {
4343
PaginationOptionsBuilder {
4444
limit_page_numbers: false,
4545
enable_seek: false,
46+
enable_seek_backward: false,
4647
enable_pages: true,
4748
}
4849
}
@@ -85,6 +86,7 @@ pub(crate) struct PaginationOptionsBuilder {
8586
limit_page_numbers: bool,
8687
enable_pages: bool,
8788
enable_seek: bool,
89+
enable_seek_backward: bool,
8890
}
8991

9092
impl PaginationOptionsBuilder {
@@ -103,6 +105,12 @@ impl PaginationOptionsBuilder {
103105
self
104106
}
105107

108+
#[allow(dead_code)]
109+
pub(crate) fn enable_seek_backward(mut self, enable: bool) -> Self {
110+
self.enable_seek_backward = enable;
111+
self
112+
}
113+
106114
pub(crate) fn gather(self, parts: &Parts) -> AppResult<PaginationOptions> {
107115
use axum::extract::Query;
108116

@@ -143,11 +151,19 @@ impl PaginationOptionsBuilder {
143151

144152
Page::Numeric(numeric_page)
145153
} else if let Some(s) = params.seek {
146-
if !self.enable_seek {
147-
return Err(bad_request("?seek= is not supported for this request"));
154+
match s.starts_with('-') {
155+
true if !self.enable_seek_backward => {
156+
return Err(bad_request(
157+
"seek backward ?seek=- is not supported for this request",
158+
));
159+
}
160+
// TODO: add a varaint for seek backward
161+
true => unimplemented!("seek backward is not yet implemented"),
162+
false if !self.enable_seek => {
163+
return Err(bad_request("?seek= is not supported for this request"));
164+
}
165+
false => Page::Seek(RawSeekPayload(s)),
148166
}
149-
150-
Page::Seek(RawSeekPayload(s))
151167
} else {
152168
Page::Unspecified
153169
};

0 commit comments

Comments
 (0)