Skip to content

Commit

Permalink
feat(Backend): ✨ provide sorting for list_by*
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Jan 5, 2024
1 parent b43fd16 commit 34d8cec
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions backend/entity/src/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Model {
#[sea_orm(column_type = "Time", on_update = "current_timestamp")]
pub update_at: chrono::NaiveDateTime,
pub match_rule: i32,
pub order: f32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand Down
3 changes: 3 additions & 0 deletions backend/migration/src/m20231207_000001_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ enum Problem {
CreateAt,
UpdateAt,
MatchRule,
Order,
}

#[derive(Iden)]
Expand Down Expand Up @@ -351,6 +352,7 @@ impl MigrationTrait for Migration {
.extra(UPDATE_AT.to_string()),
)
.col(ColumnDef::new(Problem::MatchRule).integer().not_null())
.col(ColumnDef::new(Problem::Order).float().not_null())
.to_owned(),
)
.await?;
Expand Down Expand Up @@ -609,6 +611,7 @@ impl MigrationTrait for Migration {
index!(manager, Problem, AcRate);
index!(manager, Problem, AcceptCount);
index!(manager, Problem, Difficulty);
index!(manager, Problem, Order);
index!(manager, Submit, Committed);
index!(manager, Submit, Time);
index!(manager, Submit, Memory);
Expand Down
2 changes: 1 addition & 1 deletion backend/src/endpoint/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl ChatSet for Arc<Server> {
let mut pager: Pager<Entity> = match req.request.ok_or(Error::NotInPayload("request"))? {
list_by_request::Request::ParentId(ppk) => {
tracing::debug!(id = ppk);
Pager::parent_search(ppk)
Pager::parent_search(ppk, false)
}
list_by_request::Request::Pager(old) => {
reverse = old.reverse;
Expand Down
2 changes: 1 addition & 1 deletion backend/src/endpoint/education.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ impl EducationSet for Arc<Server> {
let mut pager: Pager<Entity> = match req.request.ok_or(Error::NotInPayload("request"))? {
list_by_request::Request::ParentId(ppk) => {
tracing::debug!(id = ppk);
Pager::parent_search(ppk)
Pager::parent_search(ppk, false)
}
list_by_request::Request::Pager(old) => {
reverse = old.reverse;
Expand Down
7 changes: 4 additions & 3 deletions backend/src/endpoint/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl ProblemSet for Arc<Server> {
model.user_id = ActiveValue::Set(user_id);

fill_active_model!(
model, req.info, title, difficulty, time, memory, tags, content, match_rule
model, req.info, title, difficulty, time, memory, tags, content, match_rule, order
);

let model = model.save(db).await.map_err(Into::<Error>::into)?;
Expand Down Expand Up @@ -244,7 +244,8 @@ impl ProblemSet for Arc<Server> {
content,
match_rule,
ac_rate,
submit_count
submit_count,
order
);

let model = model.update(db).await.map_err(Into::<Error>::into)?;
Expand Down Expand Up @@ -404,7 +405,7 @@ impl ProblemSet for Arc<Server> {
let mut pager: Pager<Entity> = match req.request.ok_or(Error::NotInPayload("request"))? {
list_by_request::Request::ParentId(ppk) => {
tracing::debug!(id = ppk);
Pager::parent_search(ppk)
Pager::parent_sorted_search(ppk, SortBy::Order, false)
}
list_by_request::Request::Pager(old) => {
reverse = old.reverse;
Expand Down
2 changes: 1 addition & 1 deletion backend/src/endpoint/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl SubmitSet for Arc<Server> {
let mut pager: Pager<Entity> = match req.request.ok_or(Error::NotInPayload("request"))? {
list_by_request::Request::ParentId(ppk) => {
tracing::debug!(id = ppk);
Pager::parent_search(ppk)
Pager::parent_search(ppk, false)
}
list_by_request::Request::Pager(old) => {
reverse = old.reverse;
Expand Down
2 changes: 1 addition & 1 deletion backend/src/endpoint/testcase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl TestcaseSet for Arc<Server> {
let mut pager: Pager<Entity> = match req.request.ok_or(Error::NotInPayload("request"))? {
list_by_request::Request::ParentId(ppk) => {
tracing::debug!(id = ppk);
Pager::parent_search(ppk)
Pager::parent_search(ppk, false)
}
list_by_request::Request::Pager(old) => {
reverse = old.reverse;
Expand Down
2 changes: 2 additions & 0 deletions backend/src/endpoint/util/pager/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl PagerTrait for problem::Entity {
SortBy::AcRate => problem::Column::AcRate,
SortBy::SubmitCount => problem::Column::SubmitCount,
SortBy::Difficulty => problem::Column::Difficulty,
SortBy::Order => problem::Column::Order,
_ => problem::Column::Id,
}
}
Expand All @@ -49,6 +50,7 @@ impl PagerTrait for problem::Entity {
SortBy::AcRate => model.ac_rate.to_string(),
SortBy::SubmitCount => model.submit_count.to_string(),
SortBy::Difficulty => model.difficulty.to_string(),
SortBy::Order => model.order.to_string(),
_ => model.id.to_string(),
}
}
Expand Down
12 changes: 6 additions & 6 deletions backend/src/endpoint/util/pager/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ pub trait HasParentPager<P, E>
where
E: EntityTrait + PagerTrait<ParentMarker = HasParent<P>>,
{
fn parent_search(ppk: i32) -> Self;
fn parent_sorted_search(ppk: i32, sort: SortBy) -> Self;
fn parent_search(ppk: i32, rev: bool) -> Self;
fn parent_sorted_search(ppk: i32, sort: SortBy, rev: bool) -> Self;
fn from_raw(s: String, server: &Server) -> Result<Pager<E>, Error>;
async fn fetch(
&mut self,
Expand Down Expand Up @@ -131,23 +131,23 @@ where
P: Related<E>,
{
#[instrument]
fn parent_search(ppk: i32) -> Self {
fn parent_search(ppk: i32, rev: bool) -> Self {
Self {
type_number: E::TYPE_NUMBER,
sort: SearchDep::Parent(ppk),
_entity: PhantomData,
last_pk: None,
last_rev: true,
last_rev: rev,
}
}
#[instrument]
fn parent_sorted_search(ppk: i32, sort: SortBy) -> Self {
fn parent_sorted_search(ppk: i32, sort: SortBy, rev: bool) -> Self {
Self {
type_number: E::TYPE_NUMBER,
sort: SearchDep::ParentSort(ppk, sort, LastValue::default()),
_entity: PhantomData,
last_pk: None,
last_rev: false,
last_rev: rev,
}
}
#[instrument(skip_all, name = "pagination_deserialize", level = "trace")]
Expand Down
10 changes: 9 additions & 1 deletion proto/backend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum SortBy {
Committed = 11;
Time = 12;
Memory = 13;
Order = 14;
}

// paginator is used to paginate list
Expand Down Expand Up @@ -171,7 +172,7 @@ service SubmitSet {
rpc Create(CreateSubmitRequest) returns (SubmitId);
rpc Remove(SubmitId) returns (google.protobuf.Empty);

// list submit by problem, sorted by pirmary key(desc)
// list submit by problem, sorted by order(increasing)
rpc ListByProblem(ListByRequest) returns (ListSubmitResponse);

// are not guarantee to yield status
Expand Down Expand Up @@ -241,6 +242,7 @@ service AnnouncementSet {
rpc Publish(AnnouncementId) returns (google.protobuf.Empty);
rpc Unpublish(AnnouncementId) returns (google.protobuf.Empty);

// paginate by id(increasing)
rpc ListByContest(ListByRequest) returns (ListAnnouncementResponse);
rpc FullInfoByContest(AnnouncementLink) returns (AnnouncementFullInfo);
}
Expand Down Expand Up @@ -307,6 +309,7 @@ service EducationSet {
rpc Link(EducationLink) returns (google.protobuf.Empty);
rpc Unlink(EducationLink) returns (google.protobuf.Empty);

// paginate by id(increasing)
rpc ListByProblem(ListByRequest) returns (ListEducationResponse);
rpc FullInfoByProblem(EducationLink) returns (EducationFullInfo);
}
Expand Down Expand Up @@ -350,6 +353,7 @@ message CreateProblemRequest {
required string tags = 6;
required string content = 7;
required MatchRule match_rule = 9;
required float order = 10;
};
required Info info = 1;
required string request_id = 2;
Expand All @@ -366,6 +370,7 @@ message UpdateProblemRequest {
optional MatchRule match_rule = 10;
optional uint32 submit_count = 3;
optional float ac_rate = 8;
optional float order = 11;
};
required Info info = 1;
required ProblemId id = 2;
Expand All @@ -388,6 +393,7 @@ service ProblemSet {
rpc Unpublish(ProblemId) returns (google.protobuf.Empty);

rpc FullInfoByContest(ProblemLink) returns (ProblemFullInfo);
// paginate by order(increasing)
rpc ListByContest(ListByRequest) returns (ListProblemResponse);
}

Expand Down Expand Up @@ -448,6 +454,7 @@ service TestcaseSet {
rpc Unlink(TestcaseLink) returns (google.protobuf.Empty);

rpc FullInfoByProblem(TestcaseLink) returns (TestcaseFullInfo);
// paginate by id(increasing)
rpc ListByProblem(ListByRequest) returns (ListTestcaseResponse);
}

Expand Down Expand Up @@ -692,5 +699,6 @@ service ChatSet {
rpc Create(CreateChatRequest) returns (ChatId);
rpc Remove(ChatId) returns (google.protobuf.Empty);

// paginate by id in increasing order
rpc ListByProblem(ListByRequest) returns (ListChatResponse);
}

0 comments on commit 34d8cec

Please sign in to comment.