11import sqlite3
22
3+ from app .core .enum .sender import SenderEnum
4+ from app .core .exceptions import APIException
5+ from app .core .status import CommonCode
36from app .core .utils import get_db_path
47from app .schemas .chat_message .db_model import ChatMessageInDB
8+ from app .schemas .chat_message .response_model import ALLChatMessagesResponseByTab , ChatMessagesResponse
59
610
711class ChatMessageRepository :
812 def create_chat_message (self , new_id : str , sender : str , chat_tab_id : str , message : str ) -> ChatMessageInDB :
9- """
10- 새로운 채팅을 데이터베이스에 저장하고, 저장된 객체를 반환합니다.
11- """
13+ """새로운 채팅을 데이터베이스에 저장하고, 저장된 객체를 반환합니다."""
1214
1315 db_path = get_db_path ()
1416 conn = None
@@ -43,7 +45,7 @@ def create_chat_message(self, new_id: str, sender: str, chat_tab_id: str, messag
4345 if conn :
4446 conn .close ()
4547
46- def get_chat_messages_by_tabId (self , id : str ) -> list [ ChatMessageInDB ] :
48+ def get_chat_tab_and_messages_by_id (self , tabId : str ) -> ALLChatMessagesResponseByTab :
4749 """주어진 chat_tab_id에 해당하는 모든 메시지를 가져옵니다."""
4850 db_path = get_db_path ()
4951 conn = None
@@ -52,17 +54,70 @@ def get_chat_messages_by_tabId(self, id: str) -> list[ChatMessageInDB]:
5254 conn .row_factory = sqlite3 .Row
5355 cursor = conn .cursor ()
5456
55- # chat_message 테이블에서 chat_tab_id에 해당하는 모든 메시지를 조회합니다.'
56- # 메시지가 없을 경우, 빈 리스트를 반환합니다.
57+ # 1. 채팅 탭 정보 조회
5758 cursor .execute (
58- "SELECT * FROM chat_message WHERE chat_tab_id = ? ORDER BY created_at ASC " ,
59- (id ,),
59+ "SELECT id, name, created_at, updated_at FROM chat_tab WHERE id = ?" ,
60+ (tabId ,),
6061 )
61- rows = cursor .fetchall ()
62+ tab_row = cursor .fetchone ()
6263
63- # 조회된 모든 행을 ChatMessageInDB 객체 리스트로 변환
64- return [ ChatMessageInDB . model_validate ( dict ( row )) for row in rows ]
64+ if not tab_row :
65+ raise APIException ( CommonCode . NO_CHAT_TAB_DATA )
6566
67+ # 2. 해당 탭의 메시지들 조회
68+ cursor .execute (
69+ """
70+ SELECT id, chat_tab_id, sender, message, created_at, updated_at
71+ FROM chat_message
72+ WHERE chat_tab_id = ?
73+ ORDER BY created_at ASC
74+ """ ,
75+ (tabId ,),
76+ )
77+ message_rows = cursor .fetchall ()
78+
79+ # 3. 메시지들을 ChatMessagesResponse로 변환
80+ messages = [
81+ ChatMessagesResponse (
82+ id = row ["id" ],
83+ chat_tab_id = row ["chat_tab_id" ],
84+ sender = SenderEnum (row ["sender" ]),
85+ message = row ["message" ],
86+ created_at = row ["created_at" ],
87+ updated_at = row ["updated_at" ],
88+ )
89+ for row in message_rows
90+ ]
91+
92+ # 4. ALLChatMessagesResponseByTab 객체 생성
93+ return ALLChatMessagesResponseByTab (
94+ id = tab_row ["id" ],
95+ name = tab_row ["name" ],
96+ created_at = tab_row ["created_at" ],
97+ updated_at = tab_row ["updated_at" ],
98+ messages = messages ,
99+ )
100+ except sqlite3 .Error as e :
101+ raise e
102+ finally :
103+ if conn :
104+ conn .close ()
105+
106+ def get_chat_tab_by_id (self , tabId : str ) -> None :
107+ """데이터베이스에 저장된 특정 Chat Tab ID를 조회합니다."""
108+ db_path = get_db_path ()
109+ conn = None
110+ try :
111+ conn = sqlite3 .connect (str (db_path ), timeout = 10 )
112+ conn .row_factory = sqlite3 .Row
113+ cursor = conn .cursor ()
114+
115+ cursor .execute (
116+ "SELECT id FROM chat_tab WHERE id = ?" ,
117+ (tabId ,),
118+ )
119+
120+ return None
66121 finally :
67122 if conn :
68123 conn .close ()
0 commit comments