-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fac3e4c
commit fbc876d
Showing
3 changed files
with
160 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,74 @@ | ||
import { Database } from "../../database.ts"; | ||
import { dustService } from "../../main.ts"; | ||
import { Author, AuthorWithId } from "./author.ts"; | ||
import { BookCrawler } from "./book-crawler.ts"; | ||
import type { Book } from "./book.ts"; | ||
import { addAuthorIfNotExists, addBookIfNotExists } from "./data.ts"; | ||
import type { Book, BookWithId } from "./book.ts"; | ||
import { | ||
addAuthorIfNotExists, | ||
addBookIfNotExists, | ||
getAllAuthors, | ||
getAllBooks, | ||
getAuthorById, | ||
getAuthorByName, | ||
getBook, | ||
} from "./data.ts"; | ||
import { FSWalker } from "./fs/fs-walker.ts"; | ||
|
||
export class BookService { | ||
async populateBooksDB(dirs: Array<string>) { | ||
const fsWalker = new FSWalker(dirs, { supportedFiletypes: ["pdf"] }); | ||
const crawler = new BookCrawler(fsWalker); | ||
const books = await crawler.crawlForBooks(); | ||
const groupedByAuthor = books.reduce((acc, cur) => { | ||
if (!acc.get(cur.author)) { | ||
acc.set(cur.author, []); | ||
} | ||
acc.get(cur.author)?.push(cur); | ||
return acc; | ||
}, new Map<string, Array<Omit<Book, "author">>>()); | ||
|
||
async populateBooksDB(dirs: Array<string>) { | ||
const fsWalker = new FSWalker(dirs, {supportedFiletypes: ['pdf']}); | ||
const crawler = new BookCrawler(fsWalker); | ||
const books = await crawler.crawlForBooks(); | ||
const groupedByAuthor = books.reduce((acc, cur) => { | ||
if (!acc.get(cur.author)) { | ||
acc.set(cur.author, []) | ||
} | ||
acc.get(cur.author)?.push(cur); | ||
return acc; | ||
}, new Map<string, Array<Omit<Book, "author">>>()); | ||
for (const [author, books] of groupedByAuthor.entries()) { | ||
const _author = await addAuthorIfNotExists(dustService.database, author); | ||
for (const book of books) { | ||
await addBookIfNotExists(dustService.database, { | ||
name: book.name, | ||
filepath: book.filepath, | ||
author: _author.id, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
async getBookById( | ||
database: Database, | ||
id: string | ||
): Promise<Omit<BookWithId, "author"> & { author: Author }> { | ||
const book = await getBook(database, id); | ||
const author = await getAuthorById(database, book.author); | ||
|
||
for (const [author, books] of groupedByAuthor.entries()) { | ||
const _author = await addAuthorIfNotExists(dustService.database, author); | ||
for (const book of books) { | ||
await addBookIfNotExists(dustService.database, {name: book.name, filepath: book.filepath, author: _author.id}); | ||
} | ||
} | ||
return { | ||
...book, | ||
author: author, | ||
}; | ||
} | ||
|
||
async getBooks( | ||
database: Database | ||
): Promise<(Omit<BookWithId, "author"> & { author: Author | undefined })[]> { | ||
const books = await getAllBooks(database); | ||
const authors = await getAllAuthors(database); | ||
const authorsById = new Map<number, AuthorWithId>(); | ||
for (const author of authors) { | ||
authorsById.set(author.id, author); | ||
} | ||
|
||
return books.map((book) => { | ||
return { | ||
...book, | ||
author: authorsById.get(book.author), | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
export const bookService: BookService = new BookService(); | ||
export const bookService: BookService = new BookService(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,52 @@ | ||
import type { Router } from "@oak/oak"; | ||
import { Status, type Router } from "@oak/oak"; | ||
import { bookService } from "./book-service.ts"; | ||
import { dustService } from "../../main.ts"; | ||
|
||
export const registerRoutes = (router: Router) => { | ||
router.get("/books/", async (ctx) => { | ||
ctx.response.body = { | ||
books: [], | ||
}; | ||
}); | ||
router.get("/books/:id", (ctx) => { | ||
ctx.response.body = { | ||
book: { | ||
id: ctx.params.id | ||
} | ||
} | ||
}); | ||
router.get("/books/:id/progress", (ctx) => { | ||
ctx.response.body = { | ||
book: { | ||
id: ctx.params.id, | ||
}, | ||
progress: { | ||
page: 0, | ||
} | ||
}; | ||
}); | ||
router.put("/books/:id/progress", (ctx) => { | ||
ctx.response.status = 200; | ||
}); | ||
} | ||
router.get("/books/", async (ctx) => { | ||
const books = await bookService.getBooks(dustService.database); | ||
console.log(books); | ||
ctx.response.body = { | ||
books: books, | ||
}; | ||
}); | ||
router.get("/books/:id", async (ctx) => { | ||
try { | ||
const book = await bookService.getBookById( | ||
dustService.database, | ||
ctx.params.id | ||
); | ||
console.log(book); | ||
ctx.response.status = Status.OK; | ||
ctx.response.body = { | ||
book: book, | ||
}; | ||
ctx.response.type = "json"; | ||
|
||
return; | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}); | ||
router.get("/books/:id/stream", async (ctx) => { | ||
const book = await bookService.getBookById( | ||
dustService.database, | ||
ctx.params.id | ||
); | ||
const file = await Deno.open(book.filepath, { read: true }); | ||
ctx.response.body = file; | ||
}); | ||
router.get("/books/:id/progress", (ctx) => { | ||
ctx.response.body = { | ||
book: { | ||
id: ctx.params.id, | ||
}, | ||
progress: { | ||
page: 0, | ||
}, | ||
}; | ||
}); | ||
router.put("/books/:id/progress", (ctx) => { | ||
ctx.response.status = 200; | ||
}); | ||
}; |