Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ログを見る #226

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions crates/sos24-infrastructure/src/firebase/firebase_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ impl FirebaseUserRepository for FirebaseUserRepositoryImpl {
&self,
new_firebase_user: NewFirebaseUser,
) -> Result<FirebaseUserId, FirebaseUserRepositoryError> {
tracing::info!("Firebaseのユーザーを作成します");

let new_firebase_user = new_firebase_user.destruct();
let created_user = self
.auth
Expand All @@ -36,6 +38,7 @@ impl FirebaseUserRepository for FirebaseUserRepositoryImpl {
))
.await;

tracing::info!("Firebaseのユーザー作成が完了しました");
match created_user {
Ok(created_user) => Ok(FirebaseUserId::new(created_user.uid)),
Err(err) => match err.current_context() {
Expand All @@ -52,18 +55,34 @@ impl FirebaseUserRepository for FirebaseUserRepositoryImpl {
id: FirebaseUserId,
email: FirebaseUserEmail,
) -> Result<(), FirebaseUserRepositoryError> {
let update = UserUpdate::builder(id.value()).email(email.value()).build();
self.auth
.update_user(update)
.await
.map(|_| ())
.map_err(|err| anyhow::anyhow!("Failed to update firebase user: {err}").into())
tracing::info!("Firebaseのユーザーのメールアドレスを更新します: {id:?}");

let update = UserUpdate::builder(id.clone().value())
.email(email.value())
.build();

let res = self.auth.update_user(update).await;

match res {
Ok(_) => {
tracing::info!("Firebaseのユーザーのメールアドレスの更新が完了しました: {id:?}");
Ok(())
}
Err(err) => Err(anyhow::anyhow!("Failed to update firebase user: {err}").into()),
}
}

async fn delete_by_id(&self, id: FirebaseUserId) -> Result<(), FirebaseUserRepositoryError> {
self.auth
.delete_user(id.value())
.await
.map_err(|err| anyhow::anyhow!("Failed to delete firebase user: {err}").into())
tracing::info!("Firebaseのユーザーを削除します: {id:?}");

let res = self.auth.delete_user(id.clone().value()).await;

match res {
Ok(_) => {
tracing::info!("Firebaseのユーザーの削除が完了しました: {id:?}");
Ok(())
}
Err(err) => Err(anyhow::anyhow!("Failed to delete firebase user: {err}").into()),
}
}
}
24 changes: 22 additions & 2 deletions crates/sos24-infrastructure/src/mongodb/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ impl MongoFormRepository {

impl FormRepository for MongoFormRepository {
async fn list(&self) -> Result<Vec<WithDate<Form>>, FormRepositoryError> {
tracing::info!("申請一覧を取得します");

let form_list = self
.collection
.aggregate(
Expand All @@ -262,31 +264,43 @@ impl FormRepository for MongoFormRepository {
.map(|doc| WithDate::try_from(bson::from_document::<FormDoc>(doc?)?))
.try_collect()
.await?;

tracing::info!("申請一覧を取得しました");
Ok(forms)
}

async fn create(&self, form: Form) -> Result<(), FormRepositoryError> {
tracing::info!("申請を作成します");

let form_doc = FormDoc::from(form);
self.collection
.insert_one(form_doc, None)
.await
.context("Failed to create form")?;

tracing::info!("申請を作成しました");
Ok(())
}

async fn find_by_id(&self, id: FormId) -> Result<Option<WithDate<Form>>, FormRepositoryError> {
tracing::info!("申請を取得します: {id:?}");

let form_doc = self
.collection
.find_one(
doc! { "_id": id.value(), "deleted_at": None::<String> },
doc! { "_id": id.clone().value(), "deleted_at": None::<String> },
None,
)
.await
.context("Failed to find form")?;

tracing::info!("申請を取得しました: {id:?}");
Ok(form_doc.map(WithDate::try_from).transpose()?)
}

async fn update(&self, form: Form) -> Result<(), FormRepositoryError> {
tracing::info!("申請を更新します");

let form_doc = FormDoc::from(form);
self.collection
.update_one(
Expand All @@ -307,18 +321,24 @@ impl FormRepository for MongoFormRepository {
)
.await
.context("Failed to update form")?;

tracing::info!("申請を更新しました");
Ok(())
}

async fn delete_by_id(&self, id: FormId) -> Result<(), FormRepositoryError> {
tracing::info!("申請を削除します: {id:?}");

self.collection
.update_one(
doc! { "_id": id.value(), "deleted_at": None::<String> },
doc! { "_id": id.clone().value(), "deleted_at": None::<String> },
doc! { "$set": { "deleted_at": chrono::Utc::now() } },
None,
)
.await
.context("Failed to delete form")?;

tracing::info!("申請を削除しました: {id:?}");
Ok(())
}
}
36 changes: 32 additions & 4 deletions crates/sos24-infrastructure/src/mongodb/form_answer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ impl MongoFormAnswerRepository {

impl FormAnswerRepository for MongoFormAnswerRepository {
async fn list(&self) -> Result<Vec<WithDate<FormAnswer>>, FormAnswerRepositoryError> {
tracing::info!("申請回答一覧を取得します");

let form_answer_list = self
.collection
.aggregate(
Expand All @@ -189,38 +191,50 @@ impl FormAnswerRepository for MongoFormAnswerRepository {
.map(|doc| WithDate::try_from(bson::from_document::<FormAnswerDoc>(doc?)?))
.try_collect()
.await?;

tracing::info!("申請回答一覧を取得しました");
Ok(form_answers)
}

async fn create(&self, form_answer: FormAnswer) -> Result<(), FormAnswerRepositoryError> {
tracing::info!("申請回答を作成します");

let form_answer_doc = FormAnswerDoc::from(form_answer);
self.collection
.insert_one(form_answer_doc, None)
.await
.context("Failed to insert form answer")?;

tracing::info!("申請回答を作成しました");
Ok(())
}

async fn find_by_id(
&self,
id: FormAnswerId,
) -> Result<Option<WithDate<FormAnswer>>, FormAnswerRepositoryError> {
tracing::info!("申請回答を取得します: {id:?}");

let form_answer_doc = self
.collection
.find_one(doc! { "_id": id.value() }, None)
.find_one(doc! { "_id": id.clone().value() }, None)
.await
.context("Failed to find form answer")?;

tracing::info!("申請回答を取得しました: {id:?}");
Ok(form_answer_doc.map(WithDate::try_from).transpose()?)
}

async fn find_by_project_id(
&self,
project_id: ProjectId,
) -> Result<Vec<WithDate<FormAnswer>>, FormAnswerRepositoryError> {
tracing::info!("企画の申請回答を取得します: {project_id:?}");

let form_answer_list = self
.collection
.aggregate(vec![
doc! { "$match": { "project_id": project_id.value(), "deleted_at": None::<String> } },
doc! { "$match": { "project_id": project_id.clone().value(), "deleted_at": None::<String> } },
doc! { "$sort": { "created_at": 1 } },
], None)
.await
Expand All @@ -229,18 +243,22 @@ impl FormAnswerRepository for MongoFormAnswerRepository {
.map(|doc| WithDate::try_from(bson::from_document::<FormAnswerDoc>(doc?)?))
.try_collect()
.await?;

tracing::info!("企画の申請回答を取得しました: {project_id:?}");
Ok(form_answers)
}

async fn find_by_form_id(
&self,
form_id: FormId,
) -> Result<Vec<WithDate<FormAnswer>>, FormAnswerRepositoryError> {
tracing::info!("申請の回答を取得します: {form_id:?}");

let form_answer_list = self
.collection
.aggregate(
vec![
doc! { "$match": { "form_id": form_id.value(), "deleted_at": None::<String> } },
doc! { "$match": { "form_id": form_id.clone().value(), "deleted_at": None::<String> } },
doc! { "$sort": { "created_at": 1 } },
],
None,
Expand All @@ -251,6 +269,8 @@ impl FormAnswerRepository for MongoFormAnswerRepository {
.map(|doc| WithDate::try_from(bson::from_document::<FormAnswerDoc>(doc?)?))
.try_collect()
.await?;

tracing::info!("申請の回答を取得しました: {form_id:?}");
Ok(form_answers)
}

Expand All @@ -259,18 +279,24 @@ impl FormAnswerRepository for MongoFormAnswerRepository {
project_id: ProjectId,
form_id: FormId,
) -> Result<Option<WithDate<FormAnswer>>, FormAnswerRepositoryError> {
tracing::info!("企画の申請の回答を取得します: {project_id:?}, {form_id:?}");

let form_answer_doc = self
.collection
.find_one(
doc! { "project_id": project_id.value(), "form_id": form_id.value() },
doc! { "project_id": project_id.clone().value(), "form_id": form_id.clone().value() },
None,
)
.await
.context("Failed to find form answer")?;

tracing::info!("企画の申請の回答を取得しました: {project_id:?}, {form_id:?}");
Ok(form_answer_doc.map(WithDate::try_from).transpose()?)
}

async fn update(&self, form_answer: FormAnswer) -> Result<(), FormAnswerRepositoryError> {
tracing::info!("申請回答を更新します");

let form_answer_doc = FormAnswerDoc::from(form_answer);
self.collection
.update_one(
Expand All @@ -287,6 +313,8 @@ impl FormAnswerRepository for MongoFormAnswerRepository {
)
.await
.context("Failed to update form")?;

tracing::info!("申請回答を更新しました");
Ok(())
}
}
18 changes: 18 additions & 0 deletions crates/sos24-infrastructure/src/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ impl Postgresql {

let pool = PgPoolOptions::new()
.max_connections(8)
.after_connect(|_, _| {
Box::pin(async move {
tracing::info!("after connect");
Ok(())
})
})
.before_acquire(|_, _| {
Box::pin(async move {
tracing::info!("before acquire");
Ok(true)
})
})
.after_release(|_, _| {
Box::pin(async move {
tracing::info!("after release");
Ok(true)
})
})
.connect(db_url)
.await?;

Expand Down
23 changes: 19 additions & 4 deletions crates/sos24-infrastructure/src/postgresql/file_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ impl PgFileDataRepository {

impl FileDataRepository for PgFileDataRepository {
async fn list(&self) -> Result<Vec<WithDate<FileData>>, FileDataRepositoryError> {
tracing::info!("ファイルデータ一覧を取得しています");

let file_data_list = sqlx::query_as!(
FileDataRow,
r#"SELECT * FROM files WHERE deleted_at IS NULL"#
Expand All @@ -65,12 +67,14 @@ impl FileDataRepository for PgFileDataRepository {
.await
.context("Failed to fetch file data list")?;

tracing::info!("ファイルデータ一覧の取得が完了しました");
Ok(file_data_list)
}

async fn create(&self, file_data: FileData) -> Result<(), FileDataRepositoryError> {
let file_data = file_data.destruct();
tracing::info!("ファイルデータを作成しています");

let file_data = file_data.destruct();
sqlx::query!(
r#"INSERT INTO files (id, name, url, owner_project) VALUES ($1, $2, $3, $4)"#,
file_data.id.value(),
Expand All @@ -82,51 +86,62 @@ impl FileDataRepository for PgFileDataRepository {
.await
.context("Failed to create file data")?;

tracing::info!("ファイルデータの作成が完了しました");
Ok(())
}

async fn find_by_id(
&self,
id: FileId,
) -> Result<Option<WithDate<FileData>>, FileDataRepositoryError> {
tracing::info!("ファイルデータを取得しています: {id:?}");

let file_data_row = sqlx::query_as!(
FileDataRow,
r#"SELECT * FROM files WHERE id = $1 AND deleted_at IS NULL"#,
id.value()
id.clone().value()
)
.fetch_optional(&*self.db)
.await
.context("Failed to fetch file data")?;

tracing::info!("ファイルデータの取得が完了しました: {id:?}");
Ok(file_data_row.map(WithDate::try_from).transpose()?)
}

async fn find_by_owner_project(
&self,
owner_project: ProjectId,
) -> Result<Vec<WithDate<FileData>>, FileDataRepositoryError> {
tracing::info!("プロジェクトに紐づくファイルデータを取得しています: {owner_project:?}");

let file_data_list = sqlx::query_as!(
FileDataRow,
r#"SELECT * FROM files WHERE owner_project = $1 AND deleted_at IS NULL"#,
owner_project.value()
owner_project.clone().value()
)
.fetch(&*self.db)
.map(|row| WithDate::try_from(row?))
.try_collect()
.await
.context("Failed to fetch file data list by owner")?;

tracing::info!("プロジェクトに紐づくファイルデータの取得が完了しました: {owner_project:?}");
Ok(file_data_list)
}

async fn delete_by_id(&self, id: FileId) -> Result<(), FileDataRepositoryError> {
tracing::info!("ファイルデータを削除しています: {id:?}");

sqlx::query!(
r#"UPDATE files SET deleted_at = NOW() WHERE id = $1 AND deleted_at IS NULL"#,
id.value()
id.clone().value()
)
.execute(&*self.db)
.await
.context("Failed to delete file data")?;

tracing::info!("ファイルデータの削除が完了しました: {id:?}");
Ok(())
}
}
Loading