From 34d8cec6c5d49ec771c4973750c629713d11404b Mon Sep 17 00:00:00 2001 From: eason <30045503+Eason0729@users.noreply.github.com> Date: Fri, 5 Jan 2024 16:33:06 +0800 Subject: [PATCH] feat(Backend): :sparkles: provide sorting for list_by* --- backend/entity/src/problem.rs | 1 + .../migration/src/m20231207_000001_create_table.rs | 3 +++ backend/src/endpoint/chat.rs | 2 +- backend/src/endpoint/education.rs | 2 +- backend/src/endpoint/problem.rs | 7 ++++--- backend/src/endpoint/submit.rs | 2 +- backend/src/endpoint/testcase.rs | 2 +- backend/src/endpoint/util/pager/impls.rs | 2 ++ backend/src/endpoint/util/pager/mod.rs | 12 ++++++------ proto/backend.proto | 10 +++++++++- 10 files changed, 29 insertions(+), 14 deletions(-) diff --git a/backend/entity/src/problem.rs b/backend/entity/src/problem.rs index da0be414..9b431502 100644 --- a/backend/entity/src/problem.rs +++ b/backend/entity/src/problem.rs @@ -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)] diff --git a/backend/migration/src/m20231207_000001_create_table.rs b/backend/migration/src/m20231207_000001_create_table.rs index ee4604ff..b01cde0e 100644 --- a/backend/migration/src/m20231207_000001_create_table.rs +++ b/backend/migration/src/m20231207_000001_create_table.rs @@ -82,6 +82,7 @@ enum Problem { CreateAt, UpdateAt, MatchRule, + Order, } #[derive(Iden)] @@ -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?; @@ -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); diff --git a/backend/src/endpoint/chat.rs b/backend/src/endpoint/chat.rs index ab2ae961..f20f3803 100644 --- a/backend/src/endpoint/chat.rs +++ b/backend/src/endpoint/chat.rs @@ -97,7 +97,7 @@ impl ChatSet for Arc { let mut pager: Pager = 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; diff --git a/backend/src/endpoint/education.rs b/backend/src/endpoint/education.rs index ca3a3e31..c5fb68a5 100644 --- a/backend/src/endpoint/education.rs +++ b/backend/src/endpoint/education.rs @@ -226,7 +226,7 @@ impl EducationSet for Arc { let mut pager: Pager = 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; diff --git a/backend/src/endpoint/problem.rs b/backend/src/endpoint/problem.rs index a603e370..a9bbabc1 100644 --- a/backend/src/endpoint/problem.rs +++ b/backend/src/endpoint/problem.rs @@ -201,7 +201,7 @@ impl ProblemSet for Arc { 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::::into)?; @@ -244,7 +244,8 @@ impl ProblemSet for Arc { content, match_rule, ac_rate, - submit_count + submit_count, + order ); let model = model.update(db).await.map_err(Into::::into)?; @@ -404,7 +405,7 @@ impl ProblemSet for Arc { let mut pager: Pager = 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; diff --git a/backend/src/endpoint/submit.rs b/backend/src/endpoint/submit.rs index f46daf3b..fcddbfc0 100644 --- a/backend/src/endpoint/submit.rs +++ b/backend/src/endpoint/submit.rs @@ -105,7 +105,7 @@ impl SubmitSet for Arc { let mut pager: Pager = 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; diff --git a/backend/src/endpoint/testcase.rs b/backend/src/endpoint/testcase.rs index 05536bab..3966a7a7 100644 --- a/backend/src/endpoint/testcase.rs +++ b/backend/src/endpoint/testcase.rs @@ -288,7 +288,7 @@ impl TestcaseSet for Arc { let mut pager: Pager = 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; diff --git a/backend/src/endpoint/util/pager/impls.rs b/backend/src/endpoint/util/pager/impls.rs index 51a5db00..7ef697e4 100644 --- a/backend/src/endpoint/util/pager/impls.rs +++ b/backend/src/endpoint/util/pager/impls.rs @@ -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, } } @@ -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(), } } diff --git a/backend/src/endpoint/util/pager/mod.rs b/backend/src/endpoint/util/pager/mod.rs index 37db1636..1aeab2ee 100644 --- a/backend/src/endpoint/util/pager/mod.rs +++ b/backend/src/endpoint/util/pager/mod.rs @@ -96,8 +96,8 @@ pub trait HasParentPager where E: EntityTrait + PagerTrait>, { - 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, Error>; async fn fetch( &mut self, @@ -131,23 +131,23 @@ where P: Related, { #[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")] diff --git a/proto/backend.proto b/proto/backend.proto index 7ad0cf20..87bf65b6 100644 --- a/proto/backend.proto +++ b/proto/backend.proto @@ -39,6 +39,7 @@ enum SortBy { Committed = 11; Time = 12; Memory = 13; + Order = 14; } // paginator is used to paginate list @@ -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 @@ -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); } @@ -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); } @@ -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; @@ -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; @@ -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); } @@ -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); } @@ -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); }