-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookFinder.ts
46 lines (37 loc) · 1.03 KB
/
BookFinder.ts
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
import DB from '../DB'
import IFinder from './IFinder'
import BookGateway, { BookGatewayProps } from './BookGateway'
export default class BookFinder implements IFinder {
private readonly findByIDStatementString = `
SELECT id, isbn, title, author
FROM books
WHERE id = $bookID
`
private readonly findAllStatementString = `
SELECT id, isbn, title, author
FROM books
`
public findByID(id: string): BookGateway {
const statement = DB.prepare(this.findByIDStatementString)
const row = statement.get({
bookID: id,
}) as BookGateway
return new BookGateway({
id: row.id,
isbn: row.isbn,
title: row.title,
author: row.author,
})
}
public findAll(): Array<BookGateway> {
const statement = DB.prepare(this.findAllStatementString)
const rows = statement.all() as Array<BookGatewayProps>
const books = rows.map((row) => new BookGateway({
id: row.id,
isbn: row.isbn,
title: row.title,
author: row.author,
}))
return books
}
}