chore: initial commit — monorepo ReadaBook (API NestJS, web PWA, Docker)
This commit is contained in:
43
apps/api/src/books/books.controller.ts
Normal file
43
apps/api/src/books/books.controller.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import { Controller, Get, Param, Query, Res, UseGuards } from "@nestjs/common";
|
||||
import { FastifyReply } from "fastify";
|
||||
import { lookup } from "mime-types";
|
||||
import { BookQueryDto, BookQuerySchema } from "@readabook/shared";
|
||||
import { AuthGuard } from "../auth/auth.guard.js";
|
||||
import { ZodValidationPipe } from "../common/zod-validation.pipe.js";
|
||||
import { BooksService } from "./books.service.js";
|
||||
|
||||
@Controller("books")
|
||||
@UseGuards(AuthGuard)
|
||||
export class BooksController {
|
||||
constructor(private readonly books: BooksService) {}
|
||||
|
||||
@Get()
|
||||
list(@Query(new ZodValidationPipe(BookQuerySchema)) query: BookQueryDto) {
|
||||
return this.books.list(query);
|
||||
}
|
||||
|
||||
@Get("search")
|
||||
search(@Query(new ZodValidationPipe(BookQuerySchema)) query: BookQueryDto) {
|
||||
return query.q ? this.books.search(query.q, query.limit, query.offset) : [];
|
||||
}
|
||||
|
||||
@Get(":id")
|
||||
get(@Param("id") id: string) {
|
||||
return this.books.get(Number(id));
|
||||
}
|
||||
|
||||
@Get(":id/file")
|
||||
file(@Param("id") id: string, @Res() reply: FastifyReply) {
|
||||
const { book, stream } = this.books.streamFile(Number(id));
|
||||
reply.header("Content-Type", lookup(book.filePath) || "application/octet-stream");
|
||||
reply.header("Content-Disposition", `inline; filename="${encodeURIComponent(book.title)}.${book.format}"`);
|
||||
return reply.send(stream);
|
||||
}
|
||||
|
||||
@Get(":id/cover")
|
||||
cover(@Param("id") id: string, @Res() reply: FastifyReply) {
|
||||
const { coverPath, stream } = this.books.streamCover(Number(id));
|
||||
reply.header("Content-Type", lookup(coverPath) || "image/jpeg");
|
||||
return reply.send(stream);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user