Skip to content

Commit

Permalink
refactor(backend): fix paginator api
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Jul 11, 2024
1 parent a4d5067 commit 226503b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion backend/src/endpoint/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Problem for ArcServer {
) -> Result<Response<ListProblemResponse>, Status> {
let (auth, req) = self
.parse_request_fn(req, |req| {
((req.size.saturating_abs() as u64) + req.offset / 5 + 2)
((req.size as u64) + req.offset.saturating_abs() as u64 / 5 + 2)
.try_into()
.unwrap_or(u32::MAX)
})
Expand Down
13 changes: 11 additions & 2 deletions backend/src/entity/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use std::ops::Deref;

use super::*;
use crate::entity::problem::Paginator::Parent;
use crate::entity::util::paginator::{Pager, Remain};
use crate::union;
use grpc::backend::list_problem_request::Sort;
Expand Down Expand Up @@ -412,7 +411,17 @@ impl<'a> WithAuth<'a, Paginator> {
}

impl<'a, 'b> WithDB<'a, WithAuth<'b, Paginator>> {
pub async fn fetch(&mut self, size: i64, offset: u64) -> Result<Vec<PartialModel>, Error> {
pub async fn fetch(&mut self, size: u64, offset: i64) -> Result<Vec<PartialModel>, Error> {
let size = size.min((i64::MAX - 1) as u64) as i64;
if offset < 0 {
self.fetch_inner(-size, (-offset).saturating_sub(size) as u64)
.await
.map(|x| x.into_iter().rev().collect())
} else {
self.fetch_inner(size, offset as u64).await
}
}
async fn fetch_inner(&mut self, size: i64, offset: u64) -> Result<Vec<PartialModel>, Error> {
let db = self.0;
let auth = self.1 .0;
macro_rules! write_back_with_list {
Expand Down
32 changes: 16 additions & 16 deletions grpc/proto/backend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ message ListProblemRequest {
Create create = 1;
string paginator = 2;
}
required int64 size = 3;
required uint64 offset = 4;
required uint64 size = 3;
required int64 offset = 4;
}

message ListProblemResponse {
Expand Down Expand Up @@ -192,8 +192,8 @@ message ListSubmitRequest {
Create create = 1;
string paginator = 2;
}
required int64 size = 3;
required uint64 offset = 4;
required uint64 size = 3;
required int64 offset = 4;
}

message ListSubmitResponse {
Expand Down Expand Up @@ -276,8 +276,8 @@ message ListAnnouncementRequest {
Create create = 1;
string paginator = 2;
}
required int64 size = 3;
required uint64 offset = 4;
required uint64 size = 3;
required int64 offset = 4;
}

service Announcement {
Expand Down Expand Up @@ -348,8 +348,8 @@ message ListEducationRequest {
Create create = 1;
string paginator = 2;
}
required int64 size = 3;
required uint64 offset = 4;
required uint64 size = 3;
required int64 offset = 4;
}

service Education {
Expand Down Expand Up @@ -421,8 +421,8 @@ message ListTestcaseRequest {
Create create = 1;
string paginator = 2;
}
required int64 size = 3;
required uint64 offset = 4;
required uint64 size = 3;
required int64 offset = 4;
}

// Testcase
Expand Down Expand Up @@ -527,8 +527,8 @@ message ListContestRequest {
Create create = 1;
string paginator = 2;
}
required int64 size = 3;
required uint64 offset = 4;
required uint64 size = 3;
required int64 offset = 4;
}

message Users { repeated UserRank list = 1; }
Expand Down Expand Up @@ -607,8 +607,8 @@ message ListUserRequest {
Create create = 1;
string paginator = 2;
}
required int64 size = 3;
required uint64 offset = 4;
required uint64 size = 3;
required int64 offset = 4;
}

service User {
Expand Down Expand Up @@ -681,8 +681,8 @@ message ListChatRequest {
Create create = 1;
string paginator = 2;
}
required int64 size = 3;
required uint64 offset = 4;
required uint64 size = 3;
required int64 offset = 4;
}

service Chat {
Expand Down

0 comments on commit 226503b

Please sign in to comment.