Skip to content

Commit bc93cf1

Browse files
authored
Merge pull request #9 from leozqin/recency
Add sort by recent and book count stats
2 parents b04739a + 2e65c97 commit bc93cf1

File tree

7 files changed

+35
-7
lines changed

7 files changed

+35
-7
lines changed

lib.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class Book(BaseModel):
5252
creators: List[str]
5353
local_path: str
5454
thumbnail_name: Optional[str] = None
55+
last_updated: float
5556
language: str = "en"
5657

5758
@computed_field
@@ -83,7 +84,9 @@ def from_record(cls, id: str) -> Self:
8384

8485
def make_book(path: Path) -> Book:
8586
try:
86-
book = read_epub(str(path.resolve()))
87+
file = path.resolve()
88+
book = read_epub(str(file))
89+
last_updated = file.stat().st_mtime
8790
except Exception as e:
8891
raise BookNotParseableException(
8992
f"Book at path {path} was not parseable!"
@@ -113,6 +116,7 @@ def make_book(path: Path) -> Book:
113116
local_path=str(path.resolve()),
114117
creators=creators,
115118
language=language,
119+
last_updated=last_updated
116120
)
117121

118122
cover_items = [i for i in cover_items if i]

main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class SortMethod(str, Enum):
1414
random = "random"
1515
az = "az"
1616
za = "za"
17+
recent = "recent"
1718

1819
class ListBooksQuery(BaseModel):
1920
sort: Optional[SortMethod] = Field(Query(default=SortMethod.random))
@@ -31,7 +32,7 @@ async def reparse_library():
3132
app = FastAPI(lifespan=lifespan)
3233

3334
@app.get("/list-books")
34-
def list_books(sort: SortMethod = SortMethod.random):
35+
def list_books(sort: SortMethod = SortMethod.recent):
3536
books = lib.list_books()
3637

3738
if sort is SortMethod.random:
@@ -40,6 +41,8 @@ def list_books(sort: SortMethod = SortMethod.random):
4041
books.sort(key=attrgetter("title"), reverse=False)
4142
elif sort is SortMethod.za:
4243
books.sort(key=attrgetter("title"), reverse=True)
44+
elif sort is SortMethod.recent:
45+
books.sort(key=attrgetter("last_updated"), reverse=True)
4346

4447
return books
4548

web/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "web",
33
"type": "module",
4-
"version": "0.0.6",
4+
"version": "0.0.7",
55
"scripts": {
66
"dev": "astro dev",
77
"build": "astro build",

web/src/components/Books.astro

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
import BookCard from "./BookCard.astro";
3+
import Stats from "./Stats.astro";
34
45
type Book = {
56
id: string;
@@ -8,16 +9,17 @@ type Book = {
89
creators: Array<string>;
910
};
1011
11-
const sort = Astro.url.searchParams.get("sort") || "random";
12+
const sort = Astro.url.searchParams.get("sort") || "recent";
1213
1314
const apiEndpoint: string = import.meta.env.API_BASE_URL
1415
? import.meta.env.API_BASE_URL
1516
: "http://api";
1617
1718
const response = await fetch(`${apiEndpoint}/list-books?sort=${sort}`);
1819
const data: Array<Book> = await response.json();
20+
const books: Number = data.length
1921
---
20-
22+
<Stats books={books} />
2123
<section class="books">
2224
<div class="book-card-grid">
2325
{

web/src/components/SortOption.astro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<li class="header-item sort-button"><a href="/?sort=random">Sort Random</a></li>
33
<li class="header-item sort-button"><a href="/?sort=az">Sort A-Z</a></li>
44
<li class="header-item sort-button"><a href="/?sort=za">Sort Z-A</a></li>
5+
<li class="header-item sort-button"><a href="/?sort=recent">Sort Recent</a></li>
56
</ul>
67

78
<style>

web/src/components/Stats.astro

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
const { books } = Astro.props
3+
---
4+
<div class="stats-block">
5+
<p>You have {books} books!</p>
6+
</div>
7+
8+
<style>
9+
.stats-block {
10+
width: 50%;
11+
align-self: auto;
12+
align-items: center;
13+
margin-right: auto;
14+
margin-left: auto;
15+
display: flex;
16+
justify-content: center;
17+
}
18+
</style>

0 commit comments

Comments
 (0)