generated from nhcarrigan/template
feat: initial prototype works
I can log in and create a book! Woo!
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* @copyright 2026 NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { FastifyPluginAsync } from "fastify";
|
||||
import { Book, CreateBookDto, UpdateBookDto } from "@library/shared-types";
|
||||
import { BookService } from "../../services/book.service";
|
||||
import { adminGuard } from "../../middleware/admin-guard";
|
||||
|
||||
const booksRoutes: FastifyPluginAsync = async (app) => {
|
||||
const bookService = new BookService();
|
||||
|
||||
/**
|
||||
* Get all books (public route).
|
||||
*/
|
||||
app.get<{ Reply: Book[] }>("/", async () => {
|
||||
return bookService.getAllBooks();
|
||||
});
|
||||
|
||||
/**
|
||||
* Get single book by ID (public route).
|
||||
*/
|
||||
app.get<{ Params: { id: string }; Reply: Book | null }>(
|
||||
"/:id",
|
||||
async (request) => {
|
||||
const { id } = request.params;
|
||||
return bookService.getBookById(id);
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Create new book (admin only).
|
||||
*/
|
||||
app.post<{ Body: CreateBookDto; Reply: Book }>(
|
||||
"/",
|
||||
{
|
||||
preValidation: [app.authenticate, adminGuard],
|
||||
},
|
||||
async (request) => {
|
||||
return bookService.createBook(request.body);
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Update book by ID (admin only).
|
||||
*/
|
||||
app.put<{
|
||||
Params: { id: string };
|
||||
Body: UpdateBookDto;
|
||||
Reply: Book | null;
|
||||
}>(
|
||||
"/:id",
|
||||
{
|
||||
preValidation: [app.authenticate, adminGuard],
|
||||
},
|
||||
async (request) => {
|
||||
const { id } = request.params;
|
||||
return bookService.updateBook(id, request.body);
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Delete book by ID (admin only).
|
||||
*/
|
||||
app.delete<{ Params: { id: string }; Reply: { success: boolean } }>(
|
||||
"/:id",
|
||||
{
|
||||
preValidation: [app.authenticate, adminGuard],
|
||||
},
|
||||
async (request) => {
|
||||
const { id } = request.params;
|
||||
await bookService.deleteBook(id);
|
||||
return { success: true };
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export default booksRoutes;
|
||||
Reference in New Issue
Block a user