This repository was archived by the owner on Sep 26, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +27
-5
lines changed
Expand file tree Collapse file tree 2 files changed +27
-5
lines changed Original file line number Diff line number Diff line change 11import { FastifyInstance } from "fastify" ;
22import { Type , Static } from "@sinclair/typebox" ;
33
4+ import * as Authors from "../../services/authors" ;
45import * as Books from "../../services/books" ;
56import * as Errors from "../../services/errors" ;
67
@@ -9,24 +10,33 @@ const GetBookParams = Type.Object({
910} ) ;
1011type 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+
1221export 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 ) ;
Original file line number Diff line number Diff line change 11import { FastifyInstance } from "fastify" ;
22import { Type , Static } from "@sinclair/typebox" ;
33
4+ import * as Authors from "../../services/authors" ;
45import * as Books from "../../services/books" ;
56import { 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 ) ;
817type GetBooksResponse = Static < typeof GetBooksResponse > ;
918
1019const 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 }
You can’t perform that action at this time.
0 commit comments