diff --git a/.gitignore b/.gitignore index d4a2e5c5..86d6b793 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,6 @@ venv/ .pytest_cache/ **__pycache__/ -**db.sqlite3 +** db.sqlite3 +* .sqlite +* .db \ No newline at end of file diff --git a/app/main.py b/app/main.py index 6f12a826..7b64baca 100644 --- a/app/main.py +++ b/app/main.py @@ -1,13 +1,13 @@ -# from models import Actor -# from managers import ActorManager -# +# from modules.models import Actor +# from modules.managers import ActorManager + # if __name__ == "__main__": # Actor.objects = ActorManager() -# + # Actor.objects.create(first_name="Emma", last_name="Watson") # Actor.objects.create(first_name="Daniel", last_name="Radclife") # print(Actor.objects.all()) -# Actor.objects.update(2, "Daniel", "Radcliffe") +# Actor.objects.update(3, "Daniel", "Radcliffe") # print(Actor.objects.all()) -# Actor.objects.delete(1) +# Actor.objects.delete(2) # print(Actor.objects.all()) diff --git a/modules/__init__.py b/modules/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/modules/managers.py b/modules/managers.py new file mode 100644 index 00000000..22b1c0be --- /dev/null +++ b/modules/managers.py @@ -0,0 +1,35 @@ +import sqlite3 +from models import Actor + + +class ActorManager: + def __init__(self) -> None: + self._connection = sqlite3.connect('../cinema.sqlite') + self.table_name = 'actors' + + def create(self, first_name: str, last_name: str) -> None: + self._connection.execute( + f"INSERT INTO {self.table_name} (first_name, last_name) VALUES (?, ?)", + (first_name, last_name) + ) + self._connection.commit() + + def all(self) -> list[Actor]: + actor_format_cursor = self._connection.execute( + f"SELECT * FROM {self.table_name}" + ) + return [Actor(*actor) for actor in actor_format_cursor] + + def update(self, actor_id: int, first_name: str, last_name: str) -> None: + self._connection.execute( + f"UPDATE {self.table_name} SET first_name=?, last_name=? WHERE id=?", + (first_name, last_name, actor_id) + ) + self._connection.commit() + + def delete(self, actor_id: int) -> None: + self._connection.execute( + f"DELETE FROM {self.table_name} WHERE id=?", + (actor_id,) + ) + self._connection.commit() diff --git a/modules/models.py b/modules/models.py new file mode 100644 index 00000000..bfc66351 --- /dev/null +++ b/modules/models.py @@ -0,0 +1,8 @@ +from dataclasses import dataclass + + +@dataclass +class Actor: + id: int + first_name: str + last_name: str \ No newline at end of file