From d0bc86f1b5f2d3f31b6bc04f0501547dbac21b6c Mon Sep 17 00:00:00 2001 From: Oleh Chernous Date: Wed, 6 Sep 2023 20:17:13 +0200 Subject: [PATCH 1/2] Solution --- app/main.py | 26 +++++++++++++------------- app/managers.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ app/models.py | 8 ++++++++ 3 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 app/managers.py create mode 100644 app/models.py diff --git a/app/main.py b/app/main.py index 6f12a826..0dc0c43d 100644 --- a/app/main.py +++ b/app/main.py @@ -1,13 +1,13 @@ -# from models import Actor -# from 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") -# print(Actor.objects.all()) -# Actor.objects.delete(1) -# print(Actor.objects.all()) +from models import Actor +from 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") + print(Actor.objects.all()) + Actor.objects.delete(1) + print(Actor.objects.all()) diff --git a/app/managers.py b/app/managers.py new file mode 100644 index 00000000..eeb772ee --- /dev/null +++ b/app/managers.py @@ -0,0 +1,44 @@ +import sqlite3 + +from models import Actor + + +class ActorManager: + def __init__(self) -> None: + self._connection = sqlite3.connect("cinema") + 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]: + actors_cursor = self._connection.execute( + f"SELECT id, first_name, last_name FROM {self.table_name}" + ) + + return [Actor(*row) for row in actors_cursor] + + def update( + self, + id_to_update: int, + new_first_name: str, + new_second_name: str + ) -> None: + self._connection.execute( + f"UPDATE {self.table_name} " + "SET first_name = ?, last_name = ?" + "WHERE id = ?", + (new_first_name, new_second_name, id_to_update) + ) + self._connection.commit() + + def delete(self, id_to_delete: int) -> None: + self._connection.execute( + f"DELETE FROM {self.table_name}" + "WHERE id = ?", (id_to_delete,) + ) + self._connection.commit() diff --git a/app/models.py b/app/models.py new file mode 100644 index 00000000..c25144fd --- /dev/null +++ b/app/models.py @@ -0,0 +1,8 @@ +from dataclasses import dataclass + + +@dataclass +class Actor: + id: int + first_name: str + last_name: str From 0aabb9ec51027c05cc4c1f715564d43f9359e2f2 Mon Sep 17 00:00:00 2001 From: Oleh Chernous Date: Thu, 7 Sep 2023 12:14:38 +0200 Subject: [PATCH 2/2] fixed database name, fixed DB queries in methods --- app/managers.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/managers.py b/app/managers.py index eeb772ee..d8ff4921 100644 --- a/app/managers.py +++ b/app/managers.py @@ -5,12 +5,12 @@ class ActorManager: def __init__(self) -> None: - self._connection = sqlite3.connect("cinema") + self._connection = sqlite3.connect("cinema_db.sqlite3") 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)" + f"INSERT INTO {self.table_name} (first_name, last_name) " "VALUES (?, ?)", (first_name, last_name) ) self._connection.commit() @@ -30,7 +30,7 @@ def update( ) -> None: self._connection.execute( f"UPDATE {self.table_name} " - "SET first_name = ?, last_name = ?" + "SET first_name = ?, last_name = ? " "WHERE id = ?", (new_first_name, new_second_name, id_to_update) ) @@ -38,7 +38,7 @@ def update( def delete(self, id_to_delete: int) -> None: self._connection.execute( - f"DELETE FROM {self.table_name}" + f"DELETE FROM {self.table_name} " "WHERE id = ?", (id_to_delete,) ) self._connection.commit()