-
Notifications
You must be signed in to change notification settings - Fork 0
面接の情報を保存するDBをFirestoreに変更した #174
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
Merged
+317
−14
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a03611c
add: google-cloud-firestore
kimurash c3b88c5
add: firestore-emulator service to docker compose
kimurash bacd011
add: firestore env variable
kimurash 5e4cdf1
feat: firestore interview repository
kimurash 7cd1461
add: compose container name
kimurash cb143ca
fix: use case mock in router test
kimurash 8e665cc
fix: README
kimurash File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
GOOGLE_API_KEY= | ||
GOOGLE_MODEL_NAME= | ||
|
||
FIRESTORE_EMULATOR_HOST= | ||
GCP_PROJECT_ID= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
backend/app/infrastructure/repositories/firestore/interview_repository.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import os | ||
|
||
from app.domain.entities.interview_question import InterviewQuestion | ||
from app.domain.repositories.interview_repository import InterviewRepository | ||
from google.cloud import firestore | ||
|
||
|
||
class FirestoreInterviewRepository(InterviewRepository): | ||
def __init__(self) -> None: | ||
"""コンストラクタ""" | ||
self.db = firestore.Client(project=os.getenv("GCP_PROJECT_ID")) | ||
self.interviews_collection = self.db.collection("interviews") | ||
|
||
def create_interview(self, questions: list[InterviewQuestion]) -> None: | ||
"""面接を作成する | ||
|
||
Args: | ||
questions (list[InterviewQuestion]): 質問のリスト | ||
""" | ||
# Firestoreのバッチ書き込みを使用して効率的にデータを保存 | ||
batch = self.db.batch() | ||
|
||
for question in questions: | ||
# 面接IDをドキュメントIDとして使用 | ||
# 質問をサブコレクションとして保存 | ||
interview_doc = self.interviews_collection.document(question.interview_id) | ||
question_doc = interview_doc.collection("questions").document( | ||
question.question_id | ||
) | ||
|
||
# 質問データを辞書形式で保存 | ||
question_data = question.to_dict() | ||
batch.set(question_doc, question_data) | ||
|
||
# バッチ書き込みを実行 | ||
batch.commit() | ||
|
||
def get_question( | ||
self, | ||
interview_id: str, | ||
question_id: str, | ||
) -> InterviewQuestion | None: | ||
"""質問を取得する | ||
|
||
Args: | ||
interview_id (str): 面接ID | ||
question_id (str): 質問ID | ||
|
||
Returns: | ||
InterviewQuestion | None: 質問 | ||
""" | ||
try: | ||
# 特定の質問ドキュメントを取得 | ||
question_doc = ( | ||
self.interviews_collection.document(interview_id) | ||
.collection("questions") | ||
.document(question_id) | ||
.get() | ||
) | ||
|
||
if not question_doc.exists: | ||
return None | ||
|
||
# ドキュメントデータを辞書形式で取得 | ||
question_data = question_doc.to_dict() | ||
if question_data is None: | ||
return None | ||
|
||
return InterviewQuestion.from_dict(question_data) | ||
|
||
except Exception: | ||
return None | ||
|
||
def update_question( | ||
self, | ||
interview_id: str, | ||
question_id: str, | ||
question: InterviewQuestion, | ||
) -> InterviewQuestion: | ||
"""質問の情報を更新する | ||
|
||
Args: | ||
interview_id (str): 面接ID | ||
question_id (str): 質問ID | ||
question (InterviewQuestion): 更新後の質問 | ||
|
||
Returns: | ||
InterviewQuestion: 更新後の質問 | ||
""" | ||
# 質問ドキュメントを更新 | ||
question_doc = ( | ||
self.interviews_collection.document(interview_id) | ||
.collection("questions") | ||
.document(question_id) | ||
) | ||
|
||
# 質問データを辞書形式で更新 | ||
question_data = question.to_dict() | ||
question_doc.set(question_data) | ||
|
||
return question | ||
|
||
def get_all_questions( | ||
self, | ||
interview_id: str, | ||
) -> list[InterviewQuestion]: | ||
"""面接の質問をすべて取得する | ||
|
||
Args: | ||
interview_id (str): 面接ID | ||
|
||
Returns: | ||
list[InterviewQuestion]: 面接の質問のリスト | ||
""" | ||
try: | ||
# 面接の全質問ドキュメントを取得 | ||
questions_docs = ( | ||
self.interviews_collection.document(interview_id) | ||
.collection("questions") | ||
.stream() | ||
) | ||
|
||
questions = [] | ||
for doc in questions_docs: | ||
question_data = doc.to_dict() | ||
if question_data is not None: | ||
questions.append(InterviewQuestion.from_dict(question_data)) | ||
|
||
return questions | ||
|
||
except Exception: | ||
return [] | ||
kimurash marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.