Skip to content

Commit

Permalink
feat: add post question
Browse files Browse the repository at this point in the history
  • Loading branch information
zrll12 committed Aug 17, 2024
1 parent 9054d2f commit 7a0e917
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 57 deletions.
44 changes: 0 additions & 44 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@ diff = "0.1.13"
moka = { version = "0.12.8", features = ["future"] }
rand = "0.8.5"
reqwest = "0.12.5"
uuid = { version = "1.9.1", features = ["v4"] }

#database
migration = {path = "migration"}
sea-orm = { version = "0.12.15", features = ["macros", "sqlx-postgres", "runtime-tokio-rustls"] }
futures = "0.3.30"
chrono = "0.4.38"
uuid = { version = "1.9.1", features = ["v4"] }
ciborium = "0.2.2"


[build-dependencies]
Expand Down
2 changes: 2 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod m20240816_073728_create_answer_table;
mod m20240817_030711_add_required_to_question;
mod m20240817_035143_add_control_to_survey;
mod m20240817_100708_create_admin_table;
mod m20240817_154332_create_score_table;

pub struct Migrator;

Expand All @@ -21,6 +22,7 @@ impl MigratorTrait for Migrator {
Box::new(m20240817_030711_add_required_to_question::Migration),
Box::new(m20240817_035143_add_control_to_survey::Migration),
Box::new(m20240817_100708_create_admin_table::Migration),
Box::new(m20240817_154332_create_score_table::Migration),
]
}
}
4 changes: 0 additions & 4 deletions migration/src/m20240816_073728_create_answer_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ impl MigrationTrait for Migration {
)
.col(ColumnDef::new(Answer::Survey).integer().not_null())
.col(ColumnDef::new(Answer::User).big_unsigned().not_null())
.col(ColumnDef::new(Answer::Judge).big_unsigned().null())
.col(ColumnDef::new(Answer::Answers).json().not_null())
.col(ColumnDef::new(Answer::Score).integer().null())
.col(ColumnDef::new(Answer::CreateTime).timestamp().not_null().default(Expr::current_timestamp()))
.col(ColumnDef::new(Answer::JudgedTime).timestamp().null())
.col(ColumnDef::new(Answer::Completed).boolean().not_null().default(false))
.to_owned(),
)
Expand All @@ -44,10 +42,8 @@ enum Answer {
Id,
Survey,
User,
Judge,
Answers,
Score,
CreateTime,
JudgedTime,
Completed
}
47 changes: 47 additions & 0 deletions migration/src/m20240817_154332_create_score_table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use sea_orm_migration::prelude::*;

#[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(Score::Table)
.if_not_exists()
.col(
ColumnDef::new(Score::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Score::User).big_unsigned().not_null())
.col(ColumnDef::new(Score::Judge).big_unsigned().not_null())
.col(ColumnDef::new(Score::Survey).integer().not_null())
.col(ColumnDef::new(Score::Answer).integer().not_null())
.col(ColumnDef::new(Score::Scores).array(ColumnType::Json).not_null())
.to_owned(),
)
.await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Score::Table).to_owned())
.await
}
}

#[derive(DeriveIden)]
enum Score {
Table,
Id,
User,
Judge,
Survey,
Answer,
Scores,
}
17 changes: 11 additions & 6 deletions src/controller/question/modify.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
use crate::model::question::QuestionType;
use crate::model::question::{Condition, QuestionType};
use crate::service::questions::save_question;
use axum::Json;
use sea_orm::JsonValue;
use serde::Serialize;
use crate::model::ValueWithTitle;

pub async fn new_question(Json(question): Json<NewQuestionRequest>) -> String {
let id = save_question(question.content, question.r#type, question.values, question.condition, question.required, None).await;
let content = serde_json::to_value(question.content).unwrap();
let values = question.values.map(|values|
values.iter().map(|v| serde_json::to_value(v).unwrap()).collect());
let condition = question.condition.map(|c| serde_json::to_string(&c).unwrap());

let id = save_question(content, question.r#type, values, condition, question.required, None).await;
id.to_string()
}

#[derive(serde::Deserialize, Serialize)]
pub struct NewQuestionRequest {
pub content: JsonValue,
pub content: ValueWithTitle,
pub r#type: QuestionType,
pub values: Option<Vec<JsonValue>>,
pub condition: Option<String>,
pub values: Option<Vec<ValueWithTitle>>,
pub condition: Option<Vec<Condition>>,
pub required: bool,
}
2 changes: 1 addition & 1 deletion src/model/generated/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sea_orm::entity::prelude::*;
#[sea_orm(table_name = "admin")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: i32,
pub id: i64,
pub username: String,
pub disabled: bool,
}
Expand Down
1 change: 1 addition & 0 deletions src/model/generated/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ pub mod admin;
pub mod answer;
pub mod page;
pub mod question;
pub mod score;
pub mod survey;
1 change: 1 addition & 0 deletions src/model/generated/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ pub use super::admin::Entity as Admin;
pub use super::answer::Entity as Answer;
pub use super::page::Entity as Page;
pub use super::question::Entity as Question;
pub use super::score::Entity as Score;
pub use super::survey::Entity as Survey;
20 changes: 20 additions & 0 deletions src/model/generated/score.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15
use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "score")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub user: i64,
pub judge: i64,
pub survey: i32,
pub answer: i32,
pub scores: Vec<Json>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
3 changes: 3 additions & 0 deletions src/model/question.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ pub struct ConditionInner {

#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum ConditionType {
#[serde(rename = "and")]
And,
#[serde(rename = "or")]
Or,
#[serde(rename = "not")]
Not,
}

Expand Down

0 comments on commit 7a0e917

Please sign in to comment.