Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Ihor-MA committed Sep 6, 2023
1 parent b0d225a commit e979a96
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
5 changes: 3 additions & 2 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -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")
# Actor.objects.update(1, "Daniel", "Radcliffe")
# print(Actor.objects.all())
# Actor.objects.delete(1)
# print(Actor.objects.all())
49 changes: 49 additions & 0 deletions app/managers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import sqlite3

from models import Actor


class ActorManager:
def __init__(self) -> None:
self._connection = sqlite3.connect(
"C:/Users/Igor/PycharmProjects/"
"django ORM/py-actor-manager/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_cursor = self._connection.execute(
f"SELECT * FROM {self.table_name}"
)

return [Actor(*row) for row in actor_cursor]

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

def delete(self, id_to_delete: int) -> None:
self._connection.execute(
f"DELETE FROM {self.table_name} "
f"WHERE id = ?",
(id_to_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

0 comments on commit e979a96

Please sign in to comment.