diff --git a/server/infra/entities/src/answers.rs b/server/infra/entities/src/answers.rs index f5f12c37..da5890da 100644 --- a/server/infra/entities/src/answers.rs +++ b/server/infra/entities/src/answers.rs @@ -8,8 +8,7 @@ pub struct Model { #[sea_orm(primary_key)] pub id: i32, pub form_id: i32, - #[sea_orm(column_type = "Binary(BlobSize::Blob(Some(16)))")] - pub user: Vec, + pub user: i32, pub title: String, pub time_stamp: DateTimeUtc, } @@ -26,6 +25,14 @@ pub enum Relation { FormMetaData, #[sea_orm(has_many = "super::real_answers::Entity")] RealAnswers, + #[sea_orm( + belongs_to = "super::users::Entity", + from = "Column::User", + to = "super::users::Column::Id", + on_update = "NoAction", + on_delete = "NoAction" + )] + Users, } impl Related for Entity { @@ -40,4 +47,10 @@ impl Related for Entity { } } +impl Related for Entity { + fn to() -> RelationDef { + Relation::Users.def() + } +} + impl ActiveModelBehavior for ActiveModel {} diff --git a/server/infra/entities/src/form_meta_data.rs b/server/infra/entities/src/form_meta_data.rs index c9d7157f..7e7bb281 100644 --- a/server/infra/entities/src/form_meta_data.rs +++ b/server/infra/entities/src/form_meta_data.rs @@ -10,7 +10,9 @@ pub struct Model { pub title: String, pub description: Option, pub created_at: DateTimeUtc, + pub created_by: i32, pub updated_at: DateTimeUtc, + pub updated_by: i32, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] @@ -25,6 +27,22 @@ pub enum Relation { FormWebhooks, #[sea_orm(has_many = "super::response_period::Entity")] ResponsePeriod, + #[sea_orm( + belongs_to = "super::users::Entity", + from = "Column::CreatedBy", + to = "super::users::Column::Id", + on_update = "NoAction", + on_delete = "NoAction" + )] + Users2, + #[sea_orm( + belongs_to = "super::users::Entity", + from = "Column::UpdatedBy", + to = "super::users::Column::Id", + on_update = "NoAction", + on_delete = "NoAction" + )] + Users1, } impl Related for Entity { diff --git a/server/infra/entities/src/form_questions.rs b/server/infra/entities/src/form_questions.rs index eff0a305..3eacf384 100644 --- a/server/infra/entities/src/form_questions.rs +++ b/server/infra/entities/src/form_questions.rs @@ -1,8 +1,7 @@ //! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 -use sea_orm::entity::prelude::*; - use super::sea_orm_active_enums::QuestionType; +use sea_orm::entity::prelude::*; #[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] #[sea_orm(table_name = "form_questions")] diff --git a/server/infra/entities/src/mod.rs b/server/infra/entities/src/mod.rs index b024134e..5ea0f3b4 100644 --- a/server/infra/entities/src/mod.rs +++ b/server/infra/entities/src/mod.rs @@ -11,3 +11,4 @@ pub mod form_webhooks; pub mod real_answers; pub mod response_period; pub mod sea_orm_active_enums; +pub mod users; diff --git a/server/infra/entities/src/prelude.rs b/server/infra/entities/src/prelude.rs index 4c7f10a1..f88d589a 100644 --- a/server/infra/entities/src/prelude.rs +++ b/server/infra/entities/src/prelude.rs @@ -1,8 +1,11 @@ //! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 -pub use super::{ - answers::Entity as Answers, default_answer_titles::Entity as DefaultAnswerTitles, - form_choices::Entity as FormChoices, form_meta_data::Entity as FormMetaData, - form_questions::Entity as FormQuestions, form_webhooks::Entity as FormWebhooks, - real_answers::Entity as RealAnswers, response_period::Entity as ResponsePeriod, -}; +pub use super::answers::Entity as Answers; +pub use super::default_answer_titles::Entity as DefaultAnswerTitles; +pub use super::form_choices::Entity as FormChoices; +pub use super::form_meta_data::Entity as FormMetaData; +pub use super::form_questions::Entity as FormQuestions; +pub use super::form_webhooks::Entity as FormWebhooks; +pub use super::real_answers::Entity as RealAnswers; +pub use super::response_period::Entity as ResponsePeriod; +pub use super::users::Entity as Users; diff --git a/server/infra/entities/src/users.rs b/server/infra/entities/src/users.rs new file mode 100644 index 00000000..e9220b41 --- /dev/null +++ b/server/infra/entities/src/users.rs @@ -0,0 +1,27 @@ +//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.1 + +use sea_orm::entity::prelude::*; + +#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] +#[sea_orm(table_name = "users")] +pub struct Model { + #[sea_orm(primary_key)] + pub id: i32, + #[sea_orm(column_type = "Binary(BlobSize::Blob(Some(16)))")] + pub uuid: Vec, + pub name: String, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation { + #[sea_orm(has_many = "super::answers::Entity")] + Answers, +} + +impl Related for Entity { + fn to() -> RelationDef { + Relation::Answers.def() + } +} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index e2db47e4..2146d278 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -1,7 +1,6 @@ use async_trait::async_trait; -use chrono::Utc; use domain::form::models::{ - Answer, DefaultAnswerTitle, FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, + DefaultAnswerTitle, FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, FormUpdateTargets, OffsetAndLimit, PostedAnswers, }; use entities::{ @@ -19,11 +18,11 @@ use itertools::Itertools; use num_traits::cast::FromPrimitive; use regex::Regex; use sea_orm::{ - prelude::Uuid, sea_query::{Expr, SimpleExpr}, ActiveEnum, ActiveModelTrait, ActiveValue, ActiveValue::Set, - ColumnTrait, EntityTrait, ModelTrait, QueryFilter, QueryOrder, QuerySelect, + ColumnTrait, ConnectionTrait, DatabaseBackend, EntityTrait, ModelTrait, QueryFilter, + QueryOrder, QuerySelect, Statement, }; use crate::{ @@ -44,7 +43,9 @@ impl FormDatabase for ConnectionPool { title: Set(title.title().to_owned()), description: Set(description.to_owned()), created_at: Default::default(), + created_by: Set(1), updated_at: Default::default(), + updated_by: Set(1), } .insert(&self.pool) .await? @@ -405,36 +406,39 @@ impl FormDatabase for ConnectionPool { }, ); - let id = answers::ActiveModel { - id: Default::default(), - form_id: Set(answer.form_id.to_owned()), - user: Set(answer.uuid.to_owned().as_ref().to_vec()), - title: Set(embed_title), - time_stamp: Set(Utc::now()), - } - .insert(&self.pool) - .await? - .id; + let id = self.pool.execute(Statement::from_sql_and_values( + DatabaseBackend::MySql, + "INSERT INTO answers (form_id, user, title) VALUES (?, (SELECT id FROM users WHERE uuid = UUID_TO_BIN(?)), ?)", + [answer.form_id.to_owned().into(), answer.uuid.to_string().into(), embed_title.into()]) + ) + .await? + .last_insert_id(); - let real_answer_models = answer + let params = answer .answers .into_iter() - .map( - |Answer { - question_id, - answer, - .. - }| real_answers::ActiveModel { - id: Default::default(), - answer_id: Set(id), - question_id: Set(question_id.into()), - answer: Set(answer), - }, - ) + .map(|answer| { + vec![ + id.to_string(), + answer.question_id.to_string(), + answer.answer, + ] + }) .collect_vec(); - RealAnswers::insert_many(real_answer_models) - .exec(&self.pool) + self.pool + .execute(Statement::from_sql_and_values( + DatabaseBackend::MySql, + format!( + "INSERT INTO real_answers (answer_id, question_id, answer) VALUES {}", + vec!["(?, ?, ?)"; params.len()].iter().join(", ") + ), + params + .iter() + .flatten() + .map(|value| value.into()) + .collect_vec(), + )) .await?; Ok(()) @@ -466,7 +470,8 @@ impl FormDatabase for ConnectionPool { .collect_vec(); Ok(PostedAnswersDto { - uuid: Uuid::from_slice(answer.user.as_slice())?, + // uuid: Uuid::from_slice(answer.user.as_slice())?, + uuid: todo!(), timestamp: answer.time_stamp, form_id: answer.form_id, title: Some(answer.title),