Skip to content

Commit

Permalink
feat: form_meta_dataテーブルにcreated_byとupdated_byを挿入できるように
Browse files Browse the repository at this point in the history
  • Loading branch information
rito528 committed Oct 13, 2023
1 parent aac4e25 commit 0e67db5
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 28 deletions.
17 changes: 12 additions & 5 deletions server/domain/src/repository/form_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FormId, Error>;
async fn create(
&self,
title: FormTitle,
description: FormDescription,
user: User,
) -> Result<FormId, Error>;
async fn list(&self, offset_and_limit: OffsetAndLimit) -> Result<Vec<Form>, Error>;
async fn get(&self, id: FormId) -> Result<Form, Error>;
async fn delete(&self, id: FormId) -> Result<FormId, Error>;
Expand Down
2 changes: 1 addition & 1 deletion server/domain/src/user/models.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
10 changes: 7 additions & 3 deletions server/infra/resource/src/database/components.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -24,6 +27,7 @@ pub trait FormDatabase: Send + Sync {
&self,
title: FormTitle,
description: FormDescription,
user: User,
) -> Result<FormId, InfraError>;
async fn list(&self, offset_and_limit: OffsetAndLimit) -> Result<Vec<FormDto>, InfraError>;
async fn get(&self, form_id: FormId) -> Result<FormDto, InfraError>;
Expand Down
36 changes: 21 additions & 15 deletions server/infra/resource/src/database/form.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -34,19 +37,22 @@ impl FormDatabase for ConnectionPool {
&self,
title: FormTitle,
description: FormDescription,
user: User,
) -> Result<FormId, InfraError> {
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())
}
Expand Down
4 changes: 3 additions & 1 deletion server/infra/resource/src/repository/form_repository_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use domain::{
OffsetAndLimit, PostedAnswers,
},
repository::form_repository::FormRepository,
user::models::User,
};
use errors::Error;
use futures::{stream, stream::StreamExt};
Expand All @@ -22,8 +23,9 @@ impl<Client: DatabaseComponents + 'static> FormRepository for Repository<Client>
&self,
title: FormTitle,
description: FormDescription,
user: User,
) -> Result<FormId, Error> {
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?;
Expand Down
6 changes: 4 additions & 2 deletions server/presentation/src/form_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ 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;
use serde_json::json;
use usecase::form::FormUseCase;

pub async fn create_form_handler(
Extension(user): Extension<User>,
State(repository): State<RealInfrastructureRepository>,
Json(form): Json<Form>,
) -> impl IntoResponse {
Expand All @@ -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(),
Expand Down
4 changes: 3 additions & 1 deletion server/usecase/src/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use domain::{
OffsetAndLimit, PostedAnswers,
},
repository::form_repository::FormRepository,
user::models::User,
};
use errors::Error;

Expand All @@ -16,8 +17,9 @@ impl<R: FormRepository> FormUseCase<'_, R> {
&self,
title: FormTitle,
description: FormDescription,
user: User,
) -> Result<FormId, Error> {
self.repository.create(title, description).await
self.repository.create(title, description, user).await
}

pub async fn form_list(&self, offset_and_limit: OffsetAndLimit) -> Result<Vec<Form>, Error> {
Expand Down

0 comments on commit 0e67db5

Please sign in to comment.