From a60dcc21c1bb2d5541d156a24632d0b45efedac8 Mon Sep 17 00:00:00 2001 From: Marian Kovalyshyn Date: Wed, 6 Sep 2023 16:48:10 +0300 Subject: [PATCH] Created DB 'cinema' with table 'actors' and implemented manager for dataclass 'Actor' --- app/managers.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ app/models.py | 8 ++++++++ 2 files changed, 52 insertions(+) create mode 100644 app/managers.py create mode 100644 app/models.py diff --git a/app/managers.py b/app/managers.py new file mode 100644 index 00000000..34a7a0b7 --- /dev/null +++ b/app/managers.py @@ -0,0 +1,44 @@ +import sqlite3 + +from app.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]: + actors_cursor = self.connection.execute( + "SELECT * " + f"FROM {self.table_name}" + ) + return [Actor(*row) for row in actors_cursor] + + def update( + self, + id_to_update: int, + first_name_to_update: str, + last_name_to_update: str, + ) -> None: + self.connection.execute( + f"UPDATE {self.table_name} " + "SET first_name = ?, last_name = ? " + "WHERE id = ?", + (first_name_to_update, last_name_to_update, 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