Skip to content

Commit

Permalink
update entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Aug 23, 2024
1 parent 40c6a83 commit 82c1bdb
Show file tree
Hide file tree
Showing 17 changed files with 196 additions and 52 deletions.
3 changes: 2 additions & 1 deletion backend/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ prepare:

# Apply the migration and generate the entity code for manual inspection
entity-codegen:
rm database/*
rm -r database
mkdir -p database
sea-orm-cli migrate -u sqlite://database/backend.sqlite?mode=rwc
sea-orm-cli generate entity -u sqlite://database/backend.sqlite?mode=rwc -o src/pending

Expand Down
3 changes: 2 additions & 1 deletion backend/migration/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
pub use sea_orm_migration::prelude::*;

mod m20231207_000001_create_table;
mod m20240821_000001_create_tag;

pub struct Migrator;

#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20231207_000001_create_table::Migration)]
vec![Box::new(m20231207_000001_create_table::Migration), Box::new(m20240821_000001_create_tag::Migration)]
}
}
3 changes: 2 additions & 1 deletion backend/migration/src/m20231207_000001_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ enum Education {
Content,
}
#[derive(Iden)]
enum Problem {
pub enum Problem {
Table,
Id,
UserId,
Expand Down Expand Up @@ -178,6 +178,7 @@ impl MigrationTrait for Migration {
.col(
ColumnDef::new(Announcement::Public)
.boolean()
.not_null()
.default(false),
)
.col(
Expand Down
81 changes: 81 additions & 0 deletions backend/migration/src/m20240821_000001_create_tag.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::m20231207_000001_create_table::Problem;
use sea_orm::{DatabaseBackend, Statement};
use sea_orm_migration::prelude::*;

#[derive(Iden)]
enum Tag {
Table,
Id,
Name,
}

#[derive(Iden)]
enum TagProblem {
Table,
Id,
ProblemId,
TagId,
}

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Tag::Table)
.if_not_exists()
.col(
ColumnDef::new(Tag::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Tag::Name).string().unique_key().not_null())
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.name("idx-tag-name".to_lowercase())
.table(Tag::Table)
.col(Tag::Name)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(TagProblem::Table)
.if_not_exists()
.col(
ColumnDef::new(TagProblem::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(TagProblem::ProblemId).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("fk-pivot-problem-tag")
.from(TagProblem::Table, TagProblem::ProblemId)
.to(Problem::Table, Problem::Id),
)
.col(ColumnDef::new(TagProblem::TagId).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("fk-pivot-tag-problem")
.from(TagProblem::Table, TagProblem::TagId)
.to(Tag::Table, Tag::Id),
)
.to_owned(),
)
.await
}
}
10 changes: 5 additions & 5 deletions backend/src/entity/announcement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ pub enum Relation {
User,
}

impl Related<super::contest::Entity> for Entity {
impl Related<contest::Entity> for Entity {
fn to() -> RelationDef {
Relation::Contest.def()
}
}

impl Related<super::user::Entity> for Entity {
impl Related<user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}

impl ActiveModelBehavior for ActiveModel {}

impl super::Filter for Entity {
impl Filter for Entity {
fn read_filter<S: QueryFilter + Send>(query: S, auth: &Auth) -> Result<S, Error> {
Ok(match auth.perm() {
RoleLv::Guest => query.filter(Column::Public.eq(true)),
Expand Down Expand Up @@ -182,7 +182,7 @@ impl SortSource<PartialModel> for ParentPagerTrait {
fn sort_col(_data: &Self::Data) -> impl ColumnTrait {
Column::UpdateAt
}
fn get_val(data: &Self::Data) -> impl Into<sea_orm::Value> + Clone + Send {
fn get_val(data: &Self::Data) -> impl Into<Value> + Clone + Send {
data.1
}
fn save_val(data: &mut Self::Data, model: &PartialModel) {
Expand Down Expand Up @@ -222,7 +222,7 @@ impl SortSource<PartialModel> for ColPagerTrait {
Sort::Public => Column::Public,
}
}
fn get_val(data: &Self::Data) -> impl Into<sea_orm::Value> + Clone + Send {
fn get_val(data: &Self::Data) -> impl Into<Value> + Clone + Send {
&data.1
}
fn save_val(data: &mut Self::Data, model: &PartialModel) {
Expand Down
8 changes: 4 additions & 4 deletions backend/src/entity/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@ pub enum Relation {
User,
}

impl Related<super::problem::Entity> for Entity {
impl Related<problem::Entity> for Entity {
fn to() -> RelationDef {
Relation::Problem.def()
}
}

impl Related<super::user::Entity> for Entity {
impl Related<user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}

impl ActiveModelBehavior for ActiveModel {}

impl super::Filter for Entity {
impl Filter for Entity {
#[instrument(skip_all, level = "debug")]
fn read_filter<S: QueryFilter + Send>(query: S, _: &Auth) -> Result<S, Error> {
Ok(query)
Expand Down Expand Up @@ -105,7 +105,7 @@ impl SortSource<Model> for ParentPagerTrait {
fn sort_col(_data: &Self::Data) -> impl ColumnTrait {
Column::CreateAt
}
fn get_val(data: &Self::Data) -> impl Into<sea_orm::Value> + Clone + Send {
fn get_val(data: &Self::Data) -> impl Into<Value> + Clone + Send {
data.1
}
fn save_val(data: &mut Self::Data, model: &Model) {
Expand Down
16 changes: 8 additions & 8 deletions backend/src/entity/contest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,37 +92,37 @@ pub enum Relation {
Hoster,
}

impl Related<super::announcement::Entity> for Entity {
impl Related<announcement::Entity> for Entity {
fn to() -> RelationDef {
Relation::Announcement.def()
}
}

impl Related<super::user_contest::Entity> for Entity {
impl Related<user_contest::Entity> for Entity {
fn to() -> RelationDef {
Relation::UserContest.def()
}
}

impl Related<super::problem::Entity> for Entity {
impl Related<problem::Entity> for Entity {
fn to() -> RelationDef {
Relation::Problem.def()
}
}

impl Related<super::user::Entity> for Entity {
impl Related<user::Entity> for Entity {
fn to() -> RelationDef {
super::user_contest::Relation::User.def()
user_contest::Relation::User.def()
}
fn via() -> Option<RelationDef> {
Some(super::user_contest::Relation::Contest.def().rev())
Some(user_contest::Relation::Contest.def().rev())
}
}

impl ActiveModelBehavior for ActiveModel {}

#[tonic::async_trait]
impl super::ParentalTrait<IdModel> for Entity {
impl ParentalTrait<IdModel> for Entity {
#[instrument(skip_all, level = "info")]
async fn related_read_by_id(
auth: &Auth,
Expand Down Expand Up @@ -168,7 +168,7 @@ impl super::ParentalTrait<IdModel> for Entity {
}
}

impl super::Filter for Entity {
impl Filter for Entity {
fn read_filter<S: QueryFilter + Send>(query: S, auth: &Auth) -> Result<S, Error> {
Ok(match auth.perm() {
RoleLv::Guest => query.filter(Column::Public.eq(true)),
Expand Down
6 changes: 3 additions & 3 deletions backend/src/entity/education.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ pub enum Relation {
User,
}

impl Related<super::problem::Entity> for Entity {
impl Related<problem::Entity> for Entity {
fn to() -> RelationDef {
Relation::Problem.def()
}
}

impl Related<super::user::Entity> for Entity {
impl Related<user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}

impl ActiveModelBehavior for ActiveModel {}

impl super::Filter for Entity {
impl Filter for Entity {
fn read_filter<S: QueryFilter + Send>(query: S, auth: &Auth) -> Result<S, Error> {
let (user_id, perm) = auth.assume_login()?;
Ok(match perm {
Expand Down
2 changes: 2 additions & 0 deletions backend/src/entity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub mod contest;
pub mod education;
pub mod problem;
pub mod submit;
pub mod tag;
pub mod tag_problem;
pub mod testcase;
pub mod token;
pub mod user;
Expand Down
11 changes: 11 additions & 0 deletions backend/src/entity/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ pub enum Relation {
Chat,
#[sea_orm(has_many = "super::testcase::Entity")]
Test,
#[sea_orm(has_many = "super::tag_problem::Entity")]
TagProblem,
#[sea_orm(
belongs_to = "super::user::Entity",
from = "Column::UserId",
Expand Down Expand Up @@ -149,6 +151,15 @@ impl Related<super::contest::Entity> for Entity {
}
}

impl Related<tag::Entity> for Entity {
fn to() -> RelationDef {
tag_problem::Relation::Tag.def()
}
fn via() -> Option<RelationDef> {
Some(tag_problem::Relation::Problem.def().rev())
}
}

impl ActiveModelBehavior for ActiveModel {}

#[async_trait]
Expand Down
10 changes: 5 additions & 5 deletions backend/src/entity/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ pub enum Relation {
User,
}

impl Related<super::problem::Entity> for Entity {
impl Related<problem::Entity> for Entity {
fn to() -> RelationDef {
Relation::Problem.def()
}
}

impl Related<super::user::Entity> for Entity {
impl Related<user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}

impl ActiveModelBehavior for ActiveModel {}

impl super::Filter for Entity {
impl Filter for Entity {
#[instrument(skip_all, level = "debug")]
fn read_filter<S: QueryFilter + Send>(query: S, _: &Auth) -> Result<S, Error> {
Ok(query)
Expand Down Expand Up @@ -144,7 +144,7 @@ impl SortSource<PartialModel> for ParentPagerTrait {
fn sort_col(_data: &Self::Data) -> impl ColumnTrait {
Column::UploadAt
}
fn get_val(data: &Self::Data) -> impl Into<sea_orm::Value> + Clone + Send {
fn get_val(data: &Self::Data) -> impl Into<Value> + Clone + Send {
data.1
}
fn save_val(data: &mut Self::Data, model: &PartialModel) {
Expand Down Expand Up @@ -180,7 +180,7 @@ impl SortSource<PartialModel> for ColPagerTrait {
fn sort_col(_data: &Self::Data) -> impl ColumnTrait {
Column::UploadAt
}
fn get_val(data: &Self::Data) -> impl Into<sea_orm::Value> + Clone + Send {
fn get_val(data: &Self::Data) -> impl Into<Value> + Clone + Send {
*data
}
fn save_val(data: &mut Self::Data, model: &PartialModel) {
Expand Down
Loading

0 comments on commit 82c1bdb

Please sign in to comment.