diff --git a/app/main.py b/app/main.py index 6f12a826..2035d9f6 100644 --- a/app/main.py +++ b/app/main.py @@ -1,13 +1,14 @@ -# 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..1fa94bbf --- /dev/null +++ b/app/managers.py @@ -0,0 +1,42 @@ +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) " + f"VALUES (?, ?)", + (first_name, last_name) + ) + self._connection.commit() + + def all(self) -> list: + actors_cursor = self._connection.execute( + f"SELECT * FROM {self.table_name}" + ) + return [ + Actor(*row) for row in actors_cursor + ] + + def update( + self, actor_id: int, new_first_name: str, new_last_name: str + ) -> None: + self._connection.execute( + f"UPDATE {self.table_name} " + f"SET first_name=?, last_name=? WHERE id=?", + (new_first_name, new_last_name, actor_id) + ) + 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..3e228ce3 --- /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