-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
46 lines (28 loc) · 1.04 KB
/
service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from typing import List
from pydantic import TypeAdapter
from model import Book, Chapter
from tinydb import Query, TinyDB
db = TinyDB("db.json")
def add_chapter(chapter: Chapter) -> None:
chapters = db.table("chapters")
Chapter = Query()
chapters.remove(Chapter.number == chapter.number)
chapters.insert(chapter.model_dump())
def get_chapter(book_id: str, number: int) -> Chapter:
chapters = db.table("chapters")
Chapter = Query()
return chapters.get((Chapter.book_id == book_id) & (Chapter.number == number)) # type: ignore
def list_books():
books = db.table("books")
listed = books.all()
return TypeAdapter(type=List[Book]).validate_python(listed)
def list_chapters(book_id: str):
chapters = db.table("chapters")
listed = chapters.search(Query().book_id == book_id)
# trim data
for item in listed:
item["translated"] = ""
item["original"] = ""
# sort
listed.sort( key=lambda ch: ch['number'] )
return TypeAdapter(type=List[Chapter]).validate_python(listed)