Skip to content

Commit

Permalink
Update divide get_all_cites into subfunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
virhe committed Nov 30, 2023
1 parent 66e5292 commit 06a6200
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions src/repositories/cite_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,50 @@ def add_cite(self, cite: Cite) -> None:
self._database.connection.commit()

def get_all_cites(self) -> list[Cite]:
"""Hakee tietokannasta kaikki viitteet."""
cites = self._database.cursor.execute("SELECT id, type FROM Cites").fetchall()
"""Palauttaa listan kaikista viitteistä"""
ids = self.get_all_ids()
types = self.get_all_types()
fields = self.get_all_fields()
authors = self.get_all_authors()

all_cites = []
for id, type in cites:
for id in ids:
cite = Cite(id, types[id], authors[id], fields[id])
all_cites.append(cite)

return all_cites

def get_all_ids(self) -> list[str]:
"""Hakee tietokannasta viitteiden id:t"""
ids = self._database.cursor.execute("SELECT id FROM Cites").fetchall()
return [id[0] for id in ids]

def get_all_types(self) -> dict[str, str]:
"""Hakee tietokannasta viitteen tyypit"""
types = self._database.cursor.execute("SELECT id, type FROM Cites").fetchall()
return dict(types)

def get_all_authors(self) -> dict[str, list[str]]:
"""Hakee tietokannasta viitteen kirjailijat"""
authors = {}
for id in self.get_all_ids():
authors_query = self._database.cursor.execute(
"SELECT name FROM Authors WHERE cite_id = ?", (id,)
)
authors = [author[0] for author in authors_query.fetchall()]
authors[id] = [author[0] for author in authors_query]

return authors

def get_all_fields(self) -> dict[str, dict[str, str]]:
"""Hakee tietokannasta viitteen tiedot"""
fields = {}
for id in self.get_all_ids():
fields_query = self._database.cursor.execute(
"SELECT name, content FROM Fields WHERE cite_id = ?", (id,)
)
fields = dict(fields_query.fetchall())
fields[id] = dict(fields_query.fetchall())

cite = Cite(id, type, authors, fields)
all_cites.append(cite)

return all_cites
return fields

def remove_all_cites(self) -> None:
"""Poistaa kaikki viitteet."""
Expand Down

0 comments on commit 06a6200

Please sign in to comment.