Skip to content

Commit

Permalink
refactor(backend): refactor and finish representation layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Jul 12, 2024
1 parent 34a53a6 commit 5f71d16
Show file tree
Hide file tree
Showing 20 changed files with 411 additions and 136 deletions.
27 changes: 16 additions & 11 deletions backend/migration/src/m20231207_000001_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ enum Submit {
Score,
}
#[derive(Iden)]
enum Test {
enum Testcase {
Table,
Id,
UserId,
Expand Down Expand Up @@ -457,32 +457,37 @@ impl MigrationTrait for Migration {
manager
.create_table(
Table::create()
.table(Test::Table)
.table(Testcase::Table)
.if_not_exists()
.col(
ColumnDef::new(Test::Id)
ColumnDef::new(Testcase::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Test::UserId).integer().not_null())
.col(ColumnDef::new(Testcase::UserId).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("fk-test-user")
.from(Test::Table, Test::UserId)
.from(Testcase::Table, Testcase::UserId)
.to(User::Table, User::Id),
)
.col(ColumnDef::new(Test::ProblemId).integer().null())
.col(ColumnDef::new(Testcase::ProblemId).integer().null())
.foreign_key(
ForeignKey::create()
.name("fk-test-user")
.from(Test::Table, Test::ProblemId)
.from(Testcase::Table, Testcase::ProblemId)
.to(Problem::Table, Problem::Id),
)
.col(ColumnDef::new(Test::Input).binary().not_null())
.col(ColumnDef::new(Test::Output).binary().not_null())
.col(ColumnDef::new(Test::Score).unsigned().not_null().default(0))
.col(ColumnDef::new(Testcase::Input).binary().not_null())
.col(ColumnDef::new(Testcase::Output).binary().not_null())
.col(
ColumnDef::new(Testcase::Score)
.unsigned()
.not_null()
.default(0),
)
.to_owned(),
)
.await?;
Expand Down Expand Up @@ -703,7 +708,7 @@ impl MigrationTrait for Migration {
.drop_table(Table::drop().table(Submit::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(Test::Table).to_owned())
.drop_table(Table::drop().table(Testcase::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(Token::Table).to_owned())
Expand Down
4 changes: 2 additions & 2 deletions backend/src/controller/judger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ impl Judger {
let db = self.db.clone();

let mut binding = problem::Entity::find_by_id(req.problem)
.find_with_related(test::Entity)
.order_by_asc(test::Column::Score)
.find_with_related(testcase::Entity)
.order_by_asc(testcase::Column::Score)
.all(db.as_ref())
.await?;
let (problem, testcases) = binding.pop().ok_or(Error::BadArgument("problem id"))?;
Expand Down
88 changes: 45 additions & 43 deletions backend/src/endpoint/announcement.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::tools::*;

use crate::util::time::into_prost;
use grpc::backend::announcement_server::*;
use grpc::backend::*;
use grpc::{backend::announcement_server::*, backend::*};

use crate::entity::announcement::*;
use crate::entity::*;

use crate::NonZeroU32;
use crate::{
entity::announcement::{Paginator, *},
entity::*,
util::time::into_prost,
NonZeroU32,
};

impl From<Model> for AnnouncementFullInfo {
fn from(value: Model) -> Self {
Expand Down Expand Up @@ -51,42 +51,44 @@ impl Announcement for ArcServer {
&self,
req: Request<ListAnnouncementRequest>,
) -> Result<Response<ListAnnouncementResponse>, Status> {
// let (auth, rev, size, offset, pager) = parse_pager_param!(self, req);
//
// let (pager, models) = match pager {
// list_announcement_request::Request::Create(create) => {
// ColPaginator::new_fetch(
// (create.sort_by(), Default::default()),
// &auth,
// size,
// offset,
// create.start_from_end(),
// &self.db,
// )
// .in_current_span()
// .await
// }
// list_announcement_request::Request::Pager(old) => {
// let span = tracing::info_span!("paginate").or_current();
// let pager: ColPaginator = span.in_scope(|| self.crypto.decode(old.session))?;
// pager
// .fetch(&auth, size, offset, rev, &self.db)
// .instrument(span)
// .await
// }
// }?;
//
// let remain = pager.remain(&auth, &self.db).in_current_span().await?;
//
// let next_session = self.crypto.encode(pager)?;
// let list = models.into_iter().map(|x| x.into()).collect();
//
// Ok(Response::new(ListAnnouncementResponse {
// list,
// next_session,
// remain,
// }))
todo!()
let (auth, req) = self
.parse_request_fn(req, |req| {
((req.size as u64) + req.offset.saturating_abs() as u64 / 5 + 2)
.try_into()
.unwrap_or(u32::MAX)
})
.await?;

req.bound_check()?;

let paginator = match req.request.ok_or(Error::NotInPayload("request"))? {
list_announcement_request::Request::Create(create) => {
let query = create.query.unwrap_or_default();
let start_from_end = create.order == Order::Descend as i32;
if let Some(text) = query.text {
Paginator::new_text(text, start_from_end)
} else if let Some(sort) = query.sort_by {
Paginator::new_sort(sort.try_into().unwrap_or_default(), start_from_end)
} else if let Some(parent) = query.contest_id {
Paginator::new_parent(parent, start_from_end)
} else {
Paginator::new(start_from_end)
}
}
list_announcement_request::Request::Paginator(x) => self.crypto.decode(x)?,
};
let mut paginator = paginator.with_auth(&auth).with_db(&self.db);

let list = paginator.fetch(req.size, req.offset).await?;
let remain = paginator.remain().await?;

let paginator = paginator.into_inner();

Ok(Response::new(ListAnnouncementResponse {
list: list.into_iter().map(Into::into).collect(),
paginator: self.crypto.encode(paginator)?,
remain,
}))
}
#[instrument(skip_all, level = "debug")]
async fn full_info(&self, req: Request<Id>) -> Result<Response<AnnouncementFullInfo>, Status> {
Expand Down
37 changes: 32 additions & 5 deletions backend/src/endpoint/chat.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use super::tools::*;

use grpc::backend::chat_server::*;
use grpc::backend::*;
use grpc::{backend::chat_server::*, backend::*};

use crate::entity::chat::*;
use crate::entity::chat::{Paginator, *};

impl From<Model> for ChatInfo {
fn from(value: Model) -> Self {
Expand Down Expand Up @@ -77,8 +76,36 @@ impl Chat for ArcServer {

async fn list(
&self,
request: Request<ListChatRequest>,
req: Request<ListChatRequest>,
) -> Result<Response<ListChatResponse>, Status> {
todo!()
let (auth, req) = self
.parse_request_fn(req, |req| {
((req.size as u64) + req.offset.saturating_abs() as u64 / 5 + 2)
.try_into()
.unwrap_or(u32::MAX)
})
.await?;

req.bound_check()?;

let paginator = match req.request.ok_or(Error::NotInPayload("request"))? {
list_chat_request::Request::Create(create) => {
let start_from_end = create.order == Order::Descend as i32;
Paginator::new(create.problem_id, start_from_end)
}
list_chat_request::Request::Paginator(x) => self.crypto.decode(x)?,
};
let mut paginator = paginator.with_auth(&auth).with_db(&self.db);

let list = paginator.fetch(req.size, req.offset).await?;
let remain = paginator.remain().await?;

let paginator = paginator.into_inner();

Ok(Response::new(ListChatResponse {
list: list.into_iter().map(Into::into).collect(),
paginator: self.crypto.encode(paginator)?,
remain,
}))
}
}
42 changes: 40 additions & 2 deletions backend/src/endpoint/contest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use super::tools::*;

use crate::entity::{contest::*, *};
use crate::entity::{
contest::{Paginator, *},
*,
};

use grpc::backend::contest_server::*;
use grpc::backend::*;
Expand Down Expand Up @@ -55,7 +58,42 @@ impl Contest for ArcServer {
&self,
req: Request<ListContestRequest>,
) -> Result<Response<ListContestResponse>, Status> {
todo!()
let (auth, req) = self
.parse_request_fn(req, |req| {
((req.size as u64) + req.offset.saturating_abs() as u64 / 5 + 2)
.try_into()
.unwrap_or(u32::MAX)
})
.await?;

req.bound_check()?;

let paginator = match req.request.ok_or(Error::NotInPayload("request"))? {
list_contest_request::Request::Create(create) => {
let query = create.query.unwrap_or_default();
let start_from_end = create.order == Order::Descend as i32;
if let Some(text) = query.text {
Paginator::new_text(text, start_from_end)
} else if let Some(sort) = query.sort_by {
Paginator::new_sort(sort.try_into().unwrap_or_default(), start_from_end)
} else {
Paginator::new(start_from_end)
}
}
list_contest_request::Request::Paginator(x) => self.crypto.decode(x)?,
};
let mut paginator = paginator.with_auth(&auth).with_db(&self.db);

let list = paginator.fetch(req.size, req.offset).await?;
let remain = paginator.remain().await?;

let paginator = paginator.into_inner();

Ok(Response::new(ListContestResponse {
list: list.into_iter().map(Into::into).collect(),
paginator: self.crypto.encode(paginator)?,
remain,
}))
}
#[instrument(skip_all, level = "debug")]
async fn full_info(&self, req: Request<Id>) -> Result<Response<ContestFullInfo>, Status> {
Expand Down
37 changes: 36 additions & 1 deletion backend/src/endpoint/education.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,42 @@ impl Education for ArcServer {
&self,
req: Request<ListEducationRequest>,
) -> Result<Response<ListEducationResponse>, Status> {
todo!()
let (auth, req) = self
.parse_request_fn(req, |req| {
((req.size as u64) + req.offset.saturating_abs() as u64 / 5 + 2)
.try_into()
.unwrap_or(u32::MAX)
})
.await?;

req.bound_check()?;

let paginator = match req.request.ok_or(Error::NotInPayload("request"))? {
list_education_request::Request::Create(create) => {
let query = create.query.unwrap_or_default();
let start_from_end = create.order == Order::Descend as i32;
if let Some(text) = query.text {
Paginator::new_text(text, start_from_end)
} else if let Some(parent) = query.problem_id {
Paginator::new_parent(parent, start_from_end)
} else {
Paginator::new(start_from_end)
}
}
list_education_request::Request::Paginator(x) => self.crypto.decode(x)?,
};
let mut paginator = paginator.with_auth(&auth).with_db(&self.db);

let list = paginator.fetch(req.size, req.offset).await?;
let remain = paginator.remain().await?;

let paginator = paginator.into_inner();

Ok(Response::new(ListEducationResponse {
list: list.into_iter().map(Into::into).collect(),
paginator: self.crypto.encode(paginator)?,
remain,
}))
}
#[instrument(skip_all, level = "debug")]
async fn create(&self, req: Request<CreateEducationRequest>) -> Result<Response<Id>, Status> {
Expand Down
39 changes: 37 additions & 2 deletions backend/src/endpoint/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use grpc::backend::submit_server::*;
use grpc::backend::StateCode as BackendCode;
use grpc::backend::*;

use crate::entity::{submit::*, *};
use crate::entity::{
submit::{Paginator, *},
*,
};
use tokio_stream::wrappers::ReceiverStream;

const SUBMIT_CODE_LEN: usize = 32 * 1024;
Expand Down Expand Up @@ -54,7 +57,39 @@ impl Submit for ArcServer {
&self,
req: Request<ListSubmitRequest>,
) -> Result<Response<ListSubmitResponse>, Status> {
todo!()
let (auth, req) = self
.parse_request_fn(req, |req| {
((req.size as u64) + req.offset.saturating_abs() as u64 / 5 + 2)
.try_into()
.unwrap_or(u32::MAX)
})
.await?;

req.bound_check()?;

let paginator = match req.request.ok_or(Error::NotInPayload("request"))? {
list_submit_request::Request::Create(create) => {
let start_from_end = create.order == Order::Descend as i32;
if let Some(problem_id) = create.problem_id {
Paginator::new_parent(problem_id, start_from_end)
} else {
Paginator::new_sort(start_from_end)
}
}
list_submit_request::Request::Paginator(x) => self.crypto.decode(x)?,
};
let mut paginator = paginator.with_auth(&auth).with_db(&self.db);

let list = paginator.fetch(req.size, req.offset).await?;
let remain = paginator.remain().await?;

let paginator = paginator.into_inner();

Ok(Response::new(ListSubmitResponse {
list: list.into_iter().map(Into::into).collect(),
paginator: self.crypto.encode(paginator)?,
remain,
}))
}
#[instrument(skip_all, level = "debug")]
async fn info(&self, req: Request<Id>) -> Result<Response<SubmitInfo>, Status> {
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 @@ -3,7 +3,7 @@ use super::tools::*;
use grpc::backend::testcase_server::*;
use grpc::backend::*;

use crate::entity::{test::*, *};
use crate::entity::{testcase::*, *};

impl From<Model> for TestcaseFullInfo {
fn from(value: Model) -> Self {
Expand Down
Loading

0 comments on commit 5f71d16

Please sign in to comment.