Skip to content
This repository was archived by the owner on Sep 26, 2023. It is now read-only.

Commit c4830d7

Browse files
committed
Return author on GET book endpoints
1 parent 3d5c262 commit c4830d7

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/routes/books/get.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { FastifyInstance } from "fastify";
22
import { Type, Static } from "@sinclair/typebox";
33

4+
import * as Authors from "../../services/authors";
45
import * as Books from "../../services/books";
56
import * as Errors from "../../services/errors";
67

@@ -9,24 +10,33 @@ const GetBookParams = Type.Object({
910
});
1011
type GetBookParams = Static<typeof GetBookParams>;
1112

13+
const GetBookResponse = Type.Composite([
14+
Books.BookResponse,
15+
Type.Object({
16+
author: Type.Optional(Authors.AuthorResponse),
17+
}),
18+
]);
19+
type GetBookResponse = Static<typeof GetBookResponse>;
20+
1221
export const registerGetBook = (app: FastifyInstance) => {
1322
app.get<{
1423
Params: GetBookParams;
15-
Reply: Books.BookResponse | Errors.NotFound;
24+
Reply: GetBookResponse | Errors.NotFound;
1625
}>(
1726
`/books/:bookId`,
1827
{
1928
schema: {
2029
params: GetBookParams,
21-
response: { 200: Books.BookResponse, 404: Errors.NotFound },
30+
response: { 200: GetBookResponse, 404: Errors.NotFound },
2231
},
2332
},
2433
(request, reply) => {
2534
const book = Books.get(request.params.bookId);
2635
if (!book) {
2736
reply.code(404).send({ message: "Not Found" });
2837
} else {
29-
reply.code(200).send(book);
38+
const author = Authors.get(book.author_id) ?? undefined;
39+
reply.code(200).send({ ...book, author: author });
3040
}
3141
}
3242
);

src/routes/books/getMany.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import { FastifyInstance } from "fastify";
22
import { Type, Static } from "@sinclair/typebox";
33

4+
import * as Authors from "../../services/authors";
45
import * as Books from "../../services/books";
56
import { PaginatedTypeBox } from "../../services/paginated";
67

7-
const GetBooksResponse = PaginatedTypeBox(Books.BookResponse);
8+
const BookWithAuthor = Type.Composite([
9+
Books.BookResponse,
10+
Type.Object({
11+
author: Type.Optional(Authors.AuthorResponse),
12+
}),
13+
]);
14+
type BookWithAuthor = Static<typeof BookWithAuthor>;
15+
16+
const GetBooksResponse = PaginatedTypeBox(BookWithAuthor);
817
type GetBooksResponse = Static<typeof GetBooksResponse>;
918

1019
const GetBooksQuery = Type.Object({
@@ -41,7 +50,10 @@ export const registerGetBooks = (app: FastifyInstance) => {
4150
const books = Books.getMany(sort);
4251
reply.code(200).send({
4352
has_more_data: false,
44-
data: books,
53+
data: books.map((b) => ({
54+
...b,
55+
author: Authors.get(b.author_id) ?? undefined,
56+
})),
4557
next: null,
4658
});
4759
}

0 commit comments

Comments
 (0)