diff --git a/3 - PB/MVP/src/backend/adapter/_in/web/get_chat_messages_controller.py b/3 - PB/MVP/src/backend/adapter/_in/web/get_chat_messages_controller.py new file mode 100644 index 00000000..e29da985 --- /dev/null +++ b/3 - PB/MVP/src/backend/adapter/_in/web/get_chat_messages_controller.py @@ -0,0 +1,11 @@ +from application.port._in.get_chat_messages_use_case import GetChatMessagesUseCase +from domain.chat.chat import Chat +from domain.chat.chat_id import ChatId + + +class GetChatMessagesController: + def __init__(self, useCase: GetChatMessagesUseCase): + self.useCase = useCase + + def getChatMessages(self, chatId: int)->Chat: + return self.useCase.getChatMessages(ChatId(chatId)) \ No newline at end of file diff --git a/3 - PB/MVP/src/backend/adapter/_in/web/get_chats_controller.py b/3 - PB/MVP/src/backend/adapter/_in/web/get_chats_controller.py new file mode 100644 index 00000000..39d5645c --- /dev/null +++ b/3 - PB/MVP/src/backend/adapter/_in/web/get_chats_controller.py @@ -0,0 +1,15 @@ +from typing import List + +from application.port._in.get_chats_use_case import GetChatsUseCase +from domain.chat.chat_filter import ChatFilter +from domain.chat.chat_preview import ChatPreview + + + +class GetChatsController: + def __init__(self, getChatsUseCase:GetChatsUseCase): + self.useCase = getChatsUseCase + + def getChats(self, searchFilter:str)-> List[ChatPreview]: + filter = ChatFilter(searchFilter) + return self.useCase.getChats(filter) \ No newline at end of file diff --git a/3 - PB/MVP/src/backend/adapter/out/get_chat_messages/get_chat_messages_postgres.py b/3 - PB/MVP/src/backend/adapter/out/get_chat_messages/get_chat_messages_postgres.py new file mode 100644 index 00000000..b79a43dd --- /dev/null +++ b/3 - PB/MVP/src/backend/adapter/out/get_chat_messages/get_chat_messages_postgres.py @@ -0,0 +1,14 @@ +from application.port.out.get_chat_messages_port import GetChatMessagesPort +from adapter.out.persistence.postgres.postgres_chat_orm import PostgresChatORM +from domain.chat.chat import Chat +from domain.chat.chat_id import ChatId + + +class GetChatMessagesPostgres(GetChatMessagesPort): + def __init__(self, postgresORM: PostgresChatORM): + self.postgresORM = postgresORM + + def getChatMessages(self, chatId:ChatId)->Chat: + chatMessages = self.postgresORM.getChatMessages(chatId.id) + chat = chatMessages.toChat() + return chat \ No newline at end of file diff --git a/3 - PB/MVP/src/backend/adapter/out/get_chats/get_chats_postgres.py b/3 - PB/MVP/src/backend/adapter/out/get_chats/get_chats_postgres.py new file mode 100644 index 00000000..ef6f01ed --- /dev/null +++ b/3 - PB/MVP/src/backend/adapter/out/get_chats/get_chats_postgres.py @@ -0,0 +1,18 @@ +from typing import List + +from application.port.out.get_chats_port import GetChatsPort +from domain.chat.chat_filter import ChatFilter +from domain.chat.chat_preview import ChatPreview +from adapter.out.persistence.postgres.postgres_chat_orm import PostgresChatORM + + +class GetChatsPostgres(GetChatsPort): + def __init__(self, postgresORM: PostgresChatORM): + self.postgresORM = postgresORM + def getChats(self, chatFilter: ChatFilter) -> List[ChatPreview]: + chatsPreview = [] + listOfChatPreview = self.postgresORM.getChats(chatFilter.searchFilter) + for chatPreview in listOfChatPreview: + previewOfChat = chatPreview.getChatPreview() + chatsPreview.append(previewOfChat) + return chatsPreview diff --git a/3 - PB/MVP/src/backend/adapter/out/persistence/postgres/postgres_chat.py b/3 - PB/MVP/src/backend/adapter/out/persistence/postgres/postgres_chat.py new file mode 100644 index 00000000..abfb568b --- /dev/null +++ b/3 - PB/MVP/src/backend/adapter/out/persistence/postgres/postgres_chat.py @@ -0,0 +1,20 @@ +from datetime import datetime +from typing import List + +from adapter.out.persistence.postgres.postgres_message import PostgresMessage +from domain.chat.chat import Chat +from domain.chat.chat_id import ChatId + + + +class PostgresChat: + def __init__(self, id:int, title:str, timestamp:datetime, messages: List[PostgresMessage]): + self.id = id + self.title = title + self.timestamp = timestamp + self.messages = messages + def toChat(self): + listOfMessages = [] + for message in self.messages: + listOfMessages.append(message.toMessage()) + return Chat(title=self.title, chatId= ChatId(self.id), messages=listOfMessages) diff --git a/3 - PB/MVP/src/backend/adapter/out/persistence/postgres/postgres_chat_orm.py b/3 - PB/MVP/src/backend/adapter/out/persistence/postgres/postgres_chat_orm.py index 0427d8a1..7c06ef94 100644 --- a/3 - PB/MVP/src/backend/adapter/out/persistence/postgres/postgres_chat_orm.py +++ b/3 - PB/MVP/src/backend/adapter/out/persistence/postgres/postgres_chat_orm.py @@ -1,6 +1,11 @@ +from datetime import datetime from typing import List -from adapter.out.persistence.postgres.postgres_message import PostgresMessage +from adapter.out.persistence.postgres.postgres_message import PostgresMessage, PostgresMessageSenderType from adapter.out.persistence.postgres.postgres_chat_operation_response import PostgresChatOperationResponse +from adapter.out.persistence.postgres.postgres_chat_preview import PostgresChatPreview +from adapter.out.persistence.postgres.postgres_configuration_orm import db_session +from adapter.out.persistence.postgres.postgres_chat import PostgresChat + class PostgresChatORM: def __init__(self) -> None: @@ -47,4 +52,21 @@ def renameChat(self, chatId: int, newName: str) -> PostgresChatOperationResponse # db_session.commit() # except Exception as e: # return PostgresChatOperationResponse(False, f"Errore nella rinominazione della chat: {str(e)}", None) - return PostgresChatOperationResponse(True, "Chat rinominata correttamente.", chatId) \ No newline at end of file + return PostgresChatOperationResponse(True, "Chat rinominata correttamente.", chatId) + def getChats(self, chatFilter:str) -> List[PostgresChatPreview]: + #try: + # listOfPostgresChatPreview = db_session.query(chatFilter).filter(chatFilter).all() + #except Exception as e: + # return #todo da capire come fare + return [PostgresChatPreview(1, "titolo", + PostgresMessage("content",datetime(2020,2,12), ["relevant docs"], PostgresMessageSenderType.USER)), + PostgresChatPreview(1, "titolo2", + PostgresMessage("content2",datetime(2020,2,12), ["relevant docs"], PostgresMessageSenderType.USER))] + def getChatMessages(self, chatId: int) -> PostgresChat: + #try: + # listOfPostgresMessage = db_session.query(PostgresMessage).filter(PostgresMessage.chatId == chatId).all() + #except Exception as e: + # return #todo da capire come fare + return PostgresChat(1, "titolo", datetime(2020,12,3), + [PostgresMessage("content", datetime(2020,12,3), ["relevantDocs"], PostgresMessageSenderType.USER), + PostgresMessage("content2", datetime(2020,12,3), ["relevantDocs2"], PostgresMessageSenderType.CHATBOT)]) diff --git a/3 - PB/MVP/src/backend/adapter/out/persistence/postgres/postgres_chat_preview.py b/3 - PB/MVP/src/backend/adapter/out/persistence/postgres/postgres_chat_preview.py new file mode 100644 index 00000000..73bd4741 --- /dev/null +++ b/3 - PB/MVP/src/backend/adapter/out/persistence/postgres/postgres_chat_preview.py @@ -0,0 +1,16 @@ +from datetime import datetime + +from domain.chat.chat_id import ChatId +from domain.chat.chat_preview import ChatPreview +from adapter.out.persistence.postgres.postgres_message import PostgresMessage + + + +class PostgresChatPreview: + def __init__(self, id: int, title:str, postgresMessage: PostgresMessage): + self.id = id + self.title = title + self.lastMessage = postgresMessage + + def getChatPreview(self) -> ChatPreview: + return ChatPreview(id=ChatId(self.id), title=self.title, lastMessage=self.lastMessage.toMessage()) \ No newline at end of file diff --git a/3 - PB/MVP/src/backend/adapter/out/persistence/postgres/postgres_message.py b/3 - PB/MVP/src/backend/adapter/out/persistence/postgres/postgres_message.py index eb01f082..1c3e0635 100644 --- a/3 - PB/MVP/src/backend/adapter/out/persistence/postgres/postgres_message.py +++ b/3 - PB/MVP/src/backend/adapter/out/persistence/postgres/postgres_message.py @@ -3,6 +3,10 @@ from datetime import datetime from enum import Enum +from domain.chat.message import Message, MessageSender +from domain.document.document_id import DocumentId + + @dataclass class PostgresMessageSenderType(Enum): USER = 1 @@ -13,4 +17,11 @@ class PostgresMessage: content: str timestamp: datetime relevantDocuments: List[str] - sender: PostgresMessageSenderType \ No newline at end of file + sender: PostgresMessageSenderType + + def toMessage(self) -> Message: + return Message(self.content, + self.timestamp, + [DocumentId(relevantDocument) for relevantDocument in self.relevantDocuments], + MessageSender.USER if self.sender == PostgresMessageSenderType.USER else MessageSender.CHATBOT + ) \ No newline at end of file diff --git a/3 - PB/MVP/src/backend/app.py b/3 - PB/MVP/src/backend/app.py index b22289a0..d13322f0 100644 --- a/3 - PB/MVP/src/backend/app.py +++ b/3 - PB/MVP/src/backend/app.py @@ -2,6 +2,8 @@ from flask_cors import CORS from api_exceptions import APIBadRequest +from blueprints.get_chat_messages import getChatMessagesBlueprint +from blueprints.get_chats import getChatsBlueprint from blueprints.get_document_content import getDocumentContentBlueprint from blueprints.upload_documents import uploadDocumentsBlueprint @@ -40,6 +42,8 @@ def shutdown_session(exception=None): app.register_blueprint(changeConfigurationBlueprint) app.register_blueprint(getConfigurationOptionsBlueprint) app.register_blueprint(askChatbotBlueprint) +app.register_blueprint(getChatsBlueprint) +app.register_blueprint(getChatMessagesBlueprint) app.register_blueprint(deleteChatsBlueprint) app.register_blueprint(renameChatBlueprint) diff --git a/3 - PB/MVP/src/backend/application/port/_in/get_chat_messages_use_case.py b/3 - PB/MVP/src/backend/application/port/_in/get_chat_messages_use_case.py new file mode 100644 index 00000000..868a18ab --- /dev/null +++ b/3 - PB/MVP/src/backend/application/port/_in/get_chat_messages_use_case.py @@ -0,0 +1,7 @@ +from domain.chat.chat import Chat +from domain.chat.chat_id import ChatId + + +class GetChatMessagesUseCase: + def getChatMessages(self, chatId: ChatId)->Chat: + pass \ No newline at end of file diff --git a/3 - PB/MVP/src/backend/application/port/_in/get_chats_use_case.py b/3 - PB/MVP/src/backend/application/port/_in/get_chats_use_case.py new file mode 100644 index 00000000..5f925848 --- /dev/null +++ b/3 - PB/MVP/src/backend/application/port/_in/get_chats_use_case.py @@ -0,0 +1,9 @@ +from typing import List + +from domain.chat.chat_filter import ChatFilter +from domain.chat.chat_preview import ChatPreview + + +class GetChatsUseCase: + def getChats(self, chatFilter: ChatFilter) -> List[ChatPreview]: + pass \ No newline at end of file diff --git a/3 - PB/MVP/src/backend/application/port/out/get_chat_messages_port.py b/3 - PB/MVP/src/backend/application/port/out/get_chat_messages_port.py new file mode 100644 index 00000000..8c71effe --- /dev/null +++ b/3 - PB/MVP/src/backend/application/port/out/get_chat_messages_port.py @@ -0,0 +1,7 @@ +from domain.chat.chat import Chat +from domain.chat.chat_id import ChatId + + +class GetChatMessagesPort: + def getChatMessages(self, chatId: ChatId)->Chat: + pass \ No newline at end of file diff --git a/3 - PB/MVP/src/backend/application/port/out/get_chats_port.py b/3 - PB/MVP/src/backend/application/port/out/get_chats_port.py new file mode 100644 index 00000000..3e4ca2f6 --- /dev/null +++ b/3 - PB/MVP/src/backend/application/port/out/get_chats_port.py @@ -0,0 +1,9 @@ +from typing import List + +from domain.chat.chat_filter import ChatFilter +from domain.chat.chat_preview import ChatPreview + + +class GetChatsPort: + def getChats(self, chatFilter: ChatFilter) -> List[ChatPreview]: + pass \ No newline at end of file diff --git a/3 - PB/MVP/src/backend/application/service/get_chat_messages_service.py b/3 - PB/MVP/src/backend/application/service/get_chat_messages_service.py new file mode 100644 index 00000000..e259acfc --- /dev/null +++ b/3 - PB/MVP/src/backend/application/service/get_chat_messages_service.py @@ -0,0 +1,11 @@ +from application.port._in.get_chat_messages_use_case import GetChatMessagesUseCase +from domain.chat.chat import Chat +from domain.chat.chat_id import ChatId +from application.port.out.get_chat_messages_port import GetChatMessagesPort + + +class GetChatMessagesService(GetChatMessagesUseCase): + def __init__(self, outPort: GetChatMessagesPort): + self.outPort = outPort + def getChatMessages(self, chatId: ChatId)->Chat: + return self.outPort.getChatMessages(chatId) \ No newline at end of file diff --git a/3 - PB/MVP/src/backend/application/service/get_chats_service.py b/3 - PB/MVP/src/backend/application/service/get_chats_service.py new file mode 100644 index 00000000..b0fd896c --- /dev/null +++ b/3 - PB/MVP/src/backend/application/service/get_chats_service.py @@ -0,0 +1,15 @@ +from typing import List + +from application.port._in.get_chats_use_case import GetChatsUseCase +from application.port.out.get_chats_port import GetChatsPort +from domain.chat.chat_filter import ChatFilter +from domain.chat.chat_preview import ChatPreview + + +class GetChatsService(GetChatsUseCase): + def __init__(self, getChatsPort: GetChatsPort): + self.getChatsPort = getChatsPort + + def getChats(self, chatFilter: ChatFilter) -> List[ChatPreview]: + return self.getChatsPort.getChats(chatFilter) + diff --git a/3 - PB/MVP/src/backend/blueprints/get_chat_messages.py b/3 - PB/MVP/src/backend/blueprints/get_chat_messages.py new file mode 100644 index 00000000..c1333379 --- /dev/null +++ b/3 - PB/MVP/src/backend/blueprints/get_chat_messages.py @@ -0,0 +1,35 @@ +from flask import Blueprint, jsonify + +from adapter._in.web.get_chat_messages_controller import GetChatMessagesController +from adapter.out.persistence.postgres.postgres_configuration_orm import PostgresConfigurationORM +from adapter.out.configuration_manager import ConfigurationManager +from api_exceptions import InsufficientParameters +from adapter.out.persistence.postgres.postgres_chat_orm import PostgresChatORM +from application.service.get_chat_messages_service import GetChatMessagesService +from adapter.out.get_chat_messages.get_chat_messages_postgres import GetChatMessagesPostgres + +getChatMessagesBlueprint = Blueprint("getChatMessages", __name__) + +@getChatMessagesBlueprint.route('/getChatMessages', defaults={'chatId': ''}, methods=['GET']) +def getChatMessages(chatId): + if chatId is None: + raise InsufficientParameters() + + configurationManager = ConfigurationManager(postgresConfigurationORM=PostgresConfigurationORM()) + + controller = GetChatMessagesController( + GetChatMessagesService( + GetChatMessagesPostgres( + PostgresChatORM() + ) + ) + ) + + chatMessages = controller.getChatMessages(chatId) + return jsonify({ + "title": chatMessages.title, + "id": chatMessages.chatId.id, + "messages": [{"content": chatMessage.content, + "time": chatMessage.timestamp, + "sender": chatMessage.sender.name} for chatMessage in chatMessages.messages] + }) \ No newline at end of file diff --git a/3 - PB/MVP/src/backend/blueprints/get_chats.py b/3 - PB/MVP/src/backend/blueprints/get_chats.py new file mode 100644 index 00000000..93c2734b --- /dev/null +++ b/3 - PB/MVP/src/backend/blueprints/get_chats.py @@ -0,0 +1,39 @@ +from flask import Blueprint, jsonify + +from adapter._in.web.get_chats_controller import GetChatsController +from adapter.out.persistence.postgres.postgres_configuration_orm import PostgresConfigurationORM +from adapter.out.configuration_manager import ConfigurationManager +from api_exceptions import InsufficientParameters +from adapter.out.get_chats.get_chats_postgres import GetChatsPostgres +from adapter.out.persistence.postgres.postgres_chat_orm import PostgresChatORM +from application.service.get_chats_service import GetChatsService + +getChatsBlueprint = Blueprint("getChats", __name__) + + +@getChatsBlueprint.route('/getChats', defaults={'filter': ''}, methods=['GET']) +@getChatsBlueprint.route("/getChats/", methods=['GET']) +def getDocuments(filter): + if filter is None: + raise InsufficientParameters() + + configurationManager = ConfigurationManager(postgresConfigurationORM=PostgresConfigurationORM()) + + controller = GetChatsController( + GetChatsService( + GetChatsPostgres( + PostgresChatORM() + ) + ) + ) + + chats = controller.getChats(filter) + + if len(chats) == 0: + return jsonify([]), 404 + + return jsonify([{ + "title": chat.title, + "lastMessage": { "content": chat.lastMessage.content, + "sender":chat.lastMessage.sender.name, + "time": chat.lastMessage.timestamp}} for chat in chats]) \ No newline at end of file diff --git a/3 - PB/MVP/src/backend/domain/chat/chat.py b/3 - PB/MVP/src/backend/domain/chat/chat.py new file mode 100644 index 00000000..2b93206f --- /dev/null +++ b/3 - PB/MVP/src/backend/domain/chat/chat.py @@ -0,0 +1,11 @@ +from typing import List + +from domain.chat.chat_id import ChatId +from domain.chat.message import Message + + +class Chat: + def __init__(self, title:str, chatId: ChatId, messages: List[Message]): + self.title = title + self.chatId = chatId + self.messages = messages \ No newline at end of file diff --git a/3 - PB/MVP/src/backend/domain/chat/chat_filter.py b/3 - PB/MVP/src/backend/domain/chat/chat_filter.py new file mode 100644 index 00000000..818d75d7 --- /dev/null +++ b/3 - PB/MVP/src/backend/domain/chat/chat_filter.py @@ -0,0 +1,4 @@ + +class ChatFilter: + def __init__(self, searchFilter:str): + self.searchFilter = searchFilter \ No newline at end of file diff --git a/3 - PB/MVP/src/backend/domain/chat/chat_info.py b/3 - PB/MVP/src/backend/domain/chat/chat_info.py new file mode 100644 index 00000000..0b633429 --- /dev/null +++ b/3 - PB/MVP/src/backend/domain/chat/chat_info.py @@ -0,0 +1,6 @@ +from datetime import datetime + +class ChatInfo: + def __init__(self, title: str, timestamp: datetime): + self.title = title + self.timestamp = timestamp \ No newline at end of file diff --git a/3 - PB/MVP/src/backend/domain/chat/chat_preview.py b/3 - PB/MVP/src/backend/domain/chat/chat_preview.py new file mode 100644 index 00000000..41fa9665 --- /dev/null +++ b/3 - PB/MVP/src/backend/domain/chat/chat_preview.py @@ -0,0 +1,12 @@ +from dataclasses import dataclass + +from domain.chat.chat_id import ChatId +from domain.chat.message import Message + + +@dataclass +class ChatPreview: + def __init__(self, id:ChatId, title: str, lastMessage: Message): + self.id = id.id + self.title = title + self.lastMessage = lastMessage \ No newline at end of file