diff --git a/server/domain/src/repository/form_repository.rs b/server/domain/src/repository/form_repository.rs index a8db2cc7..2b8ed54f 100644 --- a/server/domain/src/repository/form_repository.rs +++ b/server/domain/src/repository/form_repository.rs @@ -2,16 +2,23 @@ use async_trait::async_trait; use errors::Error; use mockall::automock; -use crate::form::models::{ - Form, FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, FormUpdateTargets, - OffsetAndLimit, PostedAnswers, +use crate::{ + form::models::{ + Form, FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, FormUpdateTargets, + OffsetAndLimit, PostedAnswers, + }, + user::models::User, }; #[automock] #[async_trait] pub trait FormRepository: Send + Sync + 'static { - async fn create(&self, title: FormTitle, description: FormDescription) - -> Result; + async fn create( + &self, + title: FormTitle, + description: FormDescription, + user: User, + ) -> Result; async fn list(&self, offset_and_limit: OffsetAndLimit) -> Result, Error>; async fn get(&self, id: FormId) -> Result; async fn delete(&self, id: FormId) -> Result; diff --git a/server/domain/src/user/models.rs b/server/domain/src/user/models.rs index a7004330..bee62767 100644 --- a/server/domain/src/user/models.rs +++ b/server/domain/src/user/models.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use uuid::Uuid; -#[derive(Serialize, Deserialize, Debug, PartialEq)] +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub struct User { pub name: String, pub id: Uuid, diff --git a/server/infra/resource/src/database/components.rs b/server/infra/resource/src/database/components.rs index 443e4b97..b3b34c49 100644 --- a/server/infra/resource/src/database/components.rs +++ b/server/infra/resource/src/database/components.rs @@ -1,7 +1,10 @@ use async_trait::async_trait; -use domain::form::models::{ - FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, FormUpdateTargets, - OffsetAndLimit, PostedAnswers, +use domain::{ + form::models::{ + FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, FormUpdateTargets, + OffsetAndLimit, PostedAnswers, + }, + user::models::User, }; use errors::infra::InfraError; use mockall::automock; @@ -24,6 +27,7 @@ pub trait FormDatabase: Send + Sync { &self, title: FormTitle, description: FormDescription, + user: User, ) -> Result; async fn list(&self, offset_and_limit: OffsetAndLimit) -> Result, InfraError>; async fn get(&self, form_id: FormId) -> Result; diff --git a/server/infra/resource/src/database/form.rs b/server/infra/resource/src/database/form.rs index 4203a50a..4ccd769a 100644 --- a/server/infra/resource/src/database/form.rs +++ b/server/infra/resource/src/database/form.rs @@ -1,7 +1,10 @@ use async_trait::async_trait; -use domain::form::models::{ - DefaultAnswerTitle, FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, - FormUpdateTargets, OffsetAndLimit, PostedAnswers, +use domain::{ + form::models::{ + DefaultAnswerTitle, FormDescription, FormId, FormQuestionUpdateSchema, FormTitle, + FormUpdateTargets, OffsetAndLimit, PostedAnswers, + }, + user::models::User, }; use entities::{ default_answer_titles, form_choices, form_meta_data, form_questions, form_webhooks, @@ -34,19 +37,22 @@ impl FormDatabase for ConnectionPool { &self, title: FormTitle, description: FormDescription, + user: User, ) -> Result { - let form_id = form_meta_data::ActiveModel { - id: ActiveValue::NotSet, - 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? - .id; + let form_id = self + .pool + .execute(Statement::from_sql_and_values( + DatabaseBackend::MySql, + "INSERT INTO form_meta_data (title, description, created_by, updated_by) + SELECT ?, ?, users.id, users.id FROM users WHERE uuid = UUID_TO_BIN(?)", + [ + title.title().to_owned().into(), + description.to_owned().into(), + user.id.to_string().into(), + ], + )) + .await? + .last_insert_id() as i32; Ok(form_id.into()) } diff --git a/server/infra/resource/src/repository/form_repository_impl.rs b/server/infra/resource/src/repository/form_repository_impl.rs index 8f5aa1cb..00f82f2c 100644 --- a/server/infra/resource/src/repository/form_repository_impl.rs +++ b/server/infra/resource/src/repository/form_repository_impl.rs @@ -5,6 +5,7 @@ use domain::{ OffsetAndLimit, PostedAnswers, }, repository::form_repository::FormRepository, + user::models::User, }; use errors::Error; use futures::{stream, stream::StreamExt}; @@ -22,8 +23,9 @@ impl FormRepository for Repository &self, title: FormTitle, description: FormDescription, + user: User, ) -> Result { - let form_id = self.client.form().create(title, description).await?; + let form_id = self.client.form().create(title, description, user).await?; let form = self.client.form().get(form_id.to_owned().into()).await?; form_outgoing::create(form.try_into()?).await?; diff --git a/server/presentation/src/form_handler.rs b/server/presentation/src/form_handler.rs index f058db93..5b8f0359 100644 --- a/server/presentation/src/form_handler.rs +++ b/server/presentation/src/form_handler.rs @@ -2,13 +2,14 @@ use axum::{ extract::{Path, Query, State}, http::StatusCode, response::IntoResponse, - Json, + Extension, Json, }; use domain::{ form::models::{ Form, FormId, FormQuestionUpdateSchema, FormUpdateTargets, OffsetAndLimit, PostedAnswers, }, repository::Repositories, + user::models::User, }; use errors::{infra::InfraError, Error}; use resource::repository::RealInfrastructureRepository; @@ -16,6 +17,7 @@ use serde_json::json; use usecase::form::FormUseCase; pub async fn create_form_handler( + Extension(user): Extension, State(repository): State, Json(form): Json
, ) -> impl IntoResponse { @@ -24,7 +26,7 @@ pub async fn create_form_handler( }; match form_use_case - .create_form(form.title, form.description) + .create_form(form.title, form.description, user) .await { Ok(id) => (StatusCode::CREATED, Json(json!({ "id": id }))).into_response(), diff --git a/server/usecase/src/form.rs b/server/usecase/src/form.rs index bd03d29b..cb9fee59 100644 --- a/server/usecase/src/form.rs +++ b/server/usecase/src/form.rs @@ -4,6 +4,7 @@ use domain::{ OffsetAndLimit, PostedAnswers, }, repository::form_repository::FormRepository, + user::models::User, }; use errors::Error; @@ -16,8 +17,9 @@ impl FormUseCase<'_, R> { &self, title: FormTitle, description: FormDescription, + user: User, ) -> Result { - self.repository.create(title, description).await + self.repository.create(title, description, user).await } pub async fn form_list(&self, offset_and_limit: OffsetAndLimit) -> Result, Error> {