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 #997

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ venv/
.pytest_cache/
**__pycache__/
**db.sqlite3
*.db
*.sqlite
*.sqlite3
*.db3
22 changes: 22 additions & 0 deletions app/database_setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sqlite3


def setup_database() -> None:
connection = sqlite3.connect("cinema.db")
cursor = connection.cursor()

cursor.execute("""
CREATE TABLE IF NOT EXISTS actors (
id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL
);
""")

connection.commit()
connection.close()


if __name__ == "__main__":
setup_database()
print("Database and actors table created.")
42 changes: 29 additions & 13 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
# 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 managers import ActorManager


if __name__ == "__main__":
manager = ActorManager()

manager.create(first_name="Emma", last_name="Watson")
manager.create(first_name="Daniel", last_name="Radcliffe")

print("All actors after creation:")
actors = manager.all()
for actor in actors:
print(f"ID: {actor.id}, Name: {actor.first_name} {actor.last_name}")

print("\nUpdating actor with ID 2 (Daniel Radcliffe)...")
manager.update(2, "Daniel", "Radcliffe")

print("\nAll actors after update:")
actors = manager.all()
for actor in actors:
print(f"ID: {actor.id}, Name: {actor.first_name} {actor.last_name}")

print("\nDeleting actor with ID 1 (Emma Watson)...")
manager.delete(1)

print("\nAll actors after deletion:")
actors = manager.all()
for actor in actors:
print(f"ID: {actor.id}, Name: {actor.first_name} {actor.last_name}")
42 changes: 42 additions & 0 deletions app/managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import sqlite3
from models import Actor


class ActorManager:
def __init__(self) -> None:
self.table_name = "actors"
self.connection = sqlite3.connect("cinema.db")
self.cursor = self.connection.cursor()

def create(self, first_name: str, last_name: str) -> Actor:
self.cursor.execute(
f"INSERT INTO {self.table_name} "
f"(first_name, last_name) VALUES (?, ?)",
(first_name, last_name)
)
self.connection.commit()
actor_id = self.cursor.lastrowid
return Actor(id=actor_id, first_name=first_name, last_name=last_name)

def all(self) -> list[Actor]:
self.cursor.execute(f"SELECT * FROM {self.table_name}")
rows = self.cursor.fetchall()
return [Actor(id=row[0], first_name=row[1],
last_name=row[2]) for row in rows]

def update(self, actor_id: int, first_name: str,
last_name: str) -> None:
self.cursor.execute(
f"UPDATE {self.table_name} SET first_name = ?, "
f" last_name = ? WHERE id = ?",
(first_name, last_name, actor_id)
)
self.connection.commit()

def delete(self, actor_id: int) -> None:
self.cursor.execute(f"DELETE FROM {self.table_name} WHERE id = ?",
(actor_id,))
self.connection.commit()

def __del__(self) -> None:
self.connection.close()
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