File size: 3,044 Bytes
d72e3a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from Model.Database import Database
from Exceptions.UserNotConnectedException import UserNotConnectedException
from Services.Service_Chat_Message import Service_Chat_Message

class Service_Chat:
    def __init__(self):
        pass

    def get_chat(self, chat_id, user_id, is_connected):
        db = Database()

        if is_connected == False:
            raise UserNotConnectedException("User not connected")
        
        try:
            chat = db.select_chat_by_id_and_user_id(chat_id, user_id)
            db.close()
        except Exception as e:
            db.close()
            raise e
        
        print(chat)
        return chat
    
    def create_chat(self, user_id, chat_title, chat_file_content, is_connected, model):
        db = Database()

        if is_connected == False:
            raise UserNotConnectedException("User not connected")
        
        try:
            db.begin_transaction()
            chat_id = db.insert_chat(user_id, chat_title, chat_file_content, model)
            db.commit_transaction() 
        except Exception as e:
            db.rollback_transaction() 
        finally:
            db.close()
        
        return chat_id
    
    def update_chat_file_content(self, user_id, chat_id, chat_file_content, is_connected):
        db = Database()

        if is_connected == False:
            raise UserNotConnectedException("User not connected")
        
        try:
            old_content = db.select_chat_by_id(chat_id)

            if old_content[3] != chat_file_content:
                chat_file_content = old_content[3] + " " + chat_file_content
                db.update_chat_file_content_by_id(chat_id, chat_file_content)

            db.close()
        except Exception as e:
            db.close()
            raise e
    
    def delete_chat(self, chat_id, is_connected, user_id):
        db = Database()
        serviceChatMessage = Service_Chat_Message()

        if is_connected == False:
            raise UserNotConnectedException("User not connected")
        
        try:
            serviceChatMessage.delete_chat_message(chat_id, is_connected)
            db.delete_chat_by_id_and_by_user_id(chat_id, user_id)
            db.close()
        except Exception as e:
            db.close()
            raise e
        
    def get_all_user_chats(self, user_id, is_connected):
        datas={}

        db = Database()
    
        if is_connected == False:
            raise UserNotConnectedException("User not connected")

        try:
            chats = db.select_all_user_chats(user_id)

            for chat in chats:
                data = {
                    'chat_id': chat[0],
                    'chat_title': chat[1],
                    'chat_file_content': chat[2],
                    'chat_date': chat[3],
                    'chat_model': chat[4]
                }

                datas.update({chat[0]: data})

            db.close()
        except Exception as e:
            db.close()
            raise e
        
        return datas