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..d8ff4921 --- /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_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) " + "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