-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hide order logic in backend: issue 55 (#61)
* style: 💚 fix CI * fix: 🐛 reject unknown field on config * fix: 🐛 fix quick start docker-compose * Follow semantic conventions for GRPC Spans * bound check for add_to_request * rewrite rpc.oj.backend.token/list with normal pagaintor * optimize wasm size using alternative allocator and using different serialization library. * add order to database * change Dockerfile to enable bin feature * add ReOrder trait * change proto and the endpoint issue: #55
- Loading branch information
Showing
12 changed files
with
266 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod filter; | ||
pub mod helper; | ||
pub mod order; | ||
pub mod paginator; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use crate::util::with::WithDB; | ||
use crate::util::with::WithDBTrait; | ||
use sea_orm::*; | ||
use tonic::async_trait; | ||
|
||
#[async_trait] | ||
pub trait ReOrder { | ||
async fn insert_last(self) -> Result<f32, DbErr>; | ||
async fn insert_after(self, pivot: i32) -> Result<f32, DbErr>; | ||
async fn insert_front(self) -> Result<f32, DbErr>; | ||
} | ||
|
||
#[derive(Default, EnumIter, DeriveColumn, Clone, Copy, Debug)] | ||
enum RetValue { | ||
#[default] | ||
RetValue, | ||
} | ||
pub mod testcase { | ||
use super::*; | ||
use crate::entity::problem; | ||
use crate::entity::testcase::{Column, Entity}; | ||
|
||
impl WithDBTrait for problem::IdModel {} | ||
|
||
#[async_trait] | ||
impl ReOrder for WithDB<'_, problem::IdModel> { | ||
async fn insert_last(self) -> Result<f32, DbErr> { | ||
Entity::find() | ||
.filter(Column::ProblemId.eq(self.1.id)) | ||
.select_only() | ||
.column_as(Column::Order.max(), RetValue::default()) | ||
.into_values::<_, RetValue>() | ||
.one(self.0) | ||
.await | ||
.map(|x: Option<f32>| x.unwrap_or_default() + 1.0) | ||
} | ||
async fn insert_after(self, pivot: i32) -> Result<f32, DbErr> { | ||
let vals: Vec<f32> = Entity::find() | ||
.filter(Column::ProblemId.eq(self.1.id)) | ||
.filter(Column::Order.gte(pivot)) | ||
.select_only() | ||
.column_as(Column::Order.min(), RetValue::default()) | ||
.limit(2) | ||
.into_values::<_, RetValue>() | ||
.all(self.0) | ||
.await?; | ||
Ok(match vals.len() { | ||
1 => vals[0] + 1.0, | ||
2 => (vals[0] + vals[1]) * 0.5, | ||
_ => 0.0, | ||
}) | ||
} | ||
async fn insert_front(self) -> Result<f32, DbErr> { | ||
Entity::find() | ||
.filter(Column::ProblemId.eq(self.1.id)) | ||
.select_only() | ||
.column_as(Column::Order.min(), RetValue::default()) | ||
.into_values::<_, RetValue>() | ||
.one(self.0) | ||
.await | ||
.map(|x: Option<f32>| x.unwrap_or_default() - 1.0) | ||
} | ||
} | ||
} | ||
|
||
pub mod contest { | ||
use super::*; | ||
use crate::entity::contest; | ||
use crate::entity::problem::{Column, Entity}; | ||
|
||
impl WithDBTrait for contest::IdModel {} | ||
#[async_trait] | ||
impl ReOrder for WithDB<'_, contest::IdModel> { | ||
async fn insert_last(self) -> Result<f32, DbErr> { | ||
Entity::find() | ||
.filter(Column::ContestId.eq(self.1.id)) | ||
.select_only() | ||
.column_as(Column::Order.max(), RetValue::default()) | ||
.into_values::<_, RetValue>() | ||
.one(self.0) | ||
.await | ||
.map(|x: Option<f32>| x.unwrap_or_default() + 1.0) | ||
} | ||
async fn insert_after(self, pivot: i32) -> Result<f32, DbErr> { | ||
let vals: Vec<f32> = Entity::find() | ||
.filter(Column::ContestId.eq(self.1.id)) | ||
.filter(Column::Order.gte(pivot)) | ||
.select_only() | ||
.column_as(Column::Order.min(), RetValue::default()) | ||
.limit(2) | ||
.into_values::<_, RetValue>() | ||
.all(self.0) | ||
.await?; | ||
Ok(match vals.len() { | ||
1 => vals[0] + 1.0, | ||
2 => (vals[0] + vals[1]) * 0.5, | ||
_ => 0.0, | ||
}) | ||
} | ||
async fn insert_front(self) -> Result<f32, DbErr> { | ||
Entity::find() | ||
.filter(Column::ContestId.eq(self.1.id)) | ||
.select_only() | ||
.column_as(Column::Order.min(), RetValue::default()) | ||
.into_values::<_, RetValue>() | ||
.one(self.0) | ||
.await | ||
.map(|x: Option<f32>| x.unwrap_or_default() - 1.0) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.