-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepository.py
127 lines (103 loc) · 3.62 KB
/
repository.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from abc import ABC, abstractmethod
import os
import sqlite3
from typing import Any
class Database(ABC):
@abstractmethod
def create(self, item)-> Any: ...
@abstractmethod
def read(self) -> list[Any]: ...
@abstractmethod
def read_one(self, id) -> Any: ...
@abstractmethod
def update(self, item, id) -> Any: ...
@abstractmethod
def delete(self, id) -> None: ...
class Sqlite(Database):
def __init__(self, table: str, database_path) -> None:
self.table = table
self.path = database_path
self.__create_database()
def __create_database(self):
if os.path.exists(self.path) == False:
sqlite3.connect(self.path)
def open_connection(self):
self.connection = sqlite3.connect(self.path)
self.cursor = self.connection.cursor()
def close_connection(self):
self.connection.close()
def create(self, item) -> Any: ...
def read(self) -> list[Any]:
self.open_connection()
response = self.cursor.execute(f'SELECT * FROM {self.table};')
itens = response.fetchall()
self.close_connection()
return itens
def read_one(self, id) -> Any:
self.open_connection()
response = self.cursor.execute(f'SELECT * FROM {self.table} WHERE id = {id};')
item = response.fetchone()
self.close_connection()
return item
def update(self, item, id) -> Any: ...
def delete(self, id) -> Any:
deleted_item = self.read_one(id)
self.open_connection()
self.cursor.execute(f'DELETE FROM {self.table} WHERE id = {id};')
self.connection.commit()
self.close_connection()
return deleted_item
class SqliteTopicsRepository(Sqlite):
def __init__(self, database_path) -> None:
super().__init__('topics', database_path=database_path)
def create(self, item) -> Any:
self.open_connection()
self.cursor.execute(f'INSERT INTO {self.table} VALUES(?, ?, ?);', item)
self.connection.commit()
self.close_connection()
new_topic = self.read_one(item[0])
return new_topic
def update(self, item, id) -> Any:
self.open_connection()
self.cursor.execute(
f"""
UPDATE topics
SET name = '{item[0]}',
link = '{item[1]}'
WHERE id = {id};
"""
)
self.connection.commit()
self.close_connection()
new_topic = self.read_one(id)
return new_topic
def read_one_by_name(self, name):
self.open_connection()
response = self.cursor.execute(f"SELECT * FROM {self.table} WHERE name = '{name}';")
item = response.fetchone()
self.close_connection()
return item
class SqliteImagesModelRepository(Sqlite):
def __init__(self, database_path) -> None:
super().__init__('images', database_path=database_path)
def create(self, item) -> Any:
self.open_connection()
self.cursor.execute(f'INSERT INTO {self.table} VALUES(?, ?, ?, ?);', item)
self.connection.commit()
self.close_connection()
new_image = self.read_one(item[0])
return new_image
def update(self, item, id) -> Any:
self.open_connection()
self.cursor.execute(
f"""
UPDATE {self.table}
SET title = '{item[0]}',
link = '{item[1]}'
WHERE id = {id};
"""
)
self.connection.commit()
self.close_connection()
new_image = self.read_one(id)
return new_image