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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
33 changes: 20 additions & 13 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# 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 app.models import Actor
from app.managers import ActorManager

if __name__ == "__main__":
Actor.objects = ActorManager()

# Create new actors
Actor.objects.create(first_name="Emma", last_name="Watson")
Actor.objects.create(first_name="Daniel", last_name="Radclife")

# Get all actors
print(Actor.objects.all())

# Update actor's data
Actor.objects.update(2, "Daniel", "Radcliffe")
print(Actor.objects.all())

# Delete an actor
Actor.objects.delete(1)
print(Actor.objects.all())
43 changes: 43 additions & 0 deletions app/managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import sqlite3
from typing import List
from models import Actor


class ActorManager:

def __init__(self) -> None:
# Connection to DB
self.connection = sqlite3.connect("сinema.sqlite")
self.cursor = self.connection.cursor()

def create(self, first_name: str, last_name: str) -> None:
# add new data
self.cursor.execute(
"INSERT INTO actors (first_name, last_name) VALUES (?, ?)",
(first_name, last_name)
)
self.connection.commit()

def all(self) -> List[Actor]:
# get all data
self.cursor.execute("SELECT * FROM actors")
records = self.cursor.fetchall()
return [Actor(id=row[0], first_name=row[1], last_name=row[2]) for row
in records]

def update(self, actor_id: int, first_name: str, last_name: str) -> None:
# update data
self.cursor.execute(
"UPDATE actors SET first_name=?, last_name=? WHERE id=?",
(first_name, last_name, actor_id)
)
self.connection.commit()

def delete(self, actor_id: int) -> None:
# delete data
self.cursor.execute("DELETE FROM actors WHERE id=?", (actor_id,))
self.connection.commit()

def __del__(self) -> None:
# Close connection with DB to deleting
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
Binary file added cinema.sqlite
Binary file not shown.
Empty file added sinima.sqlite
Empty file.
Loading