Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution actor manager #443

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added app/cinema.db3
Binary file not shown.
4 changes: 2 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# from models import Actor
# from managers import ActorManager
# from app.models import Actor
# from app.managers import ActorManager
#
# if __name__ == "__main__":
# Actor.objects = ActorManager()
Expand Down
36 changes: 36 additions & 0 deletions app/managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import sqlite3

from app.models import Actor


class ActorManager:
def __init__(self) -> None:
self._connection = sqlite3.connect("cinema.db3")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You haven't fixed the previous comment regarding the table name.

Create an attribute to store the table name and use it throughout your code.

Suggested change
self.table_name = "actors"

def all(self) -> None:
actors_cursor = self._connection.execute(
"SELECT id, first_name, last_name FROM actors"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use * to select all the columns from the table.

It is better to create an attribute to store the table name. By defining the table name as an attribute, you make the code more flexible and easier to update if the table name ever needs to be changed.

)

return [Actor(*line) for line in actors_cursor]

def create(self, first_name: str, last_name: str) -> None:
self._connection.execute(
"INSERT INTO actors(first_name, last_name) VALUES(?, ?)",
(first_name, last_name)
)
self._connection.commit()

def update(self, id_update: int, first_name: str, last_name: str) -> None:
self._connection.execute(
"UPDATE actors SET first_name = ?, last_name = ? WHERE id = ?",
(first_name, last_name, id_update)
)
self._connection.commit()

def delete(self, id_delete: int) -> None:
self._connection.execute(
"DELETE FROM actors WHERE id = ?",
(id_delete,)
)
self._connection.commit()
8 changes: 8 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from dataclasses import dataclass


@dataclass()
class Actor:
id: int
first_name: str
last_name: str
Loading