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 { Music, CreateMusicDto, UpdateMusicDto } from "@library/shared-types";
|
||||
import { MusicService } from "../../services/music.service";
|
||||
import { adminGuard } from "../../middleware/admin-guard";
|
||||
|
||||
const musicRoutes: FastifyPluginAsync = async (app) => {
|
||||
const musicService = new MusicService();
|
||||
|
||||
/**
|
||||
* Get all music (public route).
|
||||
*/
|
||||
app.get<{ Reply: Music[] }>("/", async () => {
|
||||
return musicService.getAllMusic();
|
||||
});
|
||||
|
||||
/**
|
||||
* Get single music item by ID (public route).
|
||||
*/
|
||||
app.get<{ Params: { id: string }; Reply: Music | null }>(
|
||||
"/:id",
|
||||
async (request) => {
|
||||
const { id } = request.params;
|
||||
return musicService.getMusicById(id);
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Create new music item (admin only).
|
||||
*/
|
||||
app.post<{ Body: CreateMusicDto; Reply: Music }>(
|
||||
"/",
|
||||
{
|
||||
preValidation: [app.authenticate, adminGuard],
|
||||
},
|
||||
async (request) => {
|
||||
return musicService.createMusic(request.body);
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Update music item by ID (admin only).
|
||||
*/
|
||||
app.put<{
|
||||
Params: { id: string };
|
||||
Body: UpdateMusicDto;
|
||||
Reply: Music | null;
|
||||
}>(
|
||||
"/:id",
|
||||
{
|
||||
preValidation: [app.authenticate, adminGuard],
|
||||
},
|
||||
async (request) => {
|
||||
const { id } = request.params;
|
||||
return musicService.updateMusic(id, request.body);
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Delete music item by ID (admin only).
|
||||
*/
|
||||
app.delete<{ Params: { id: string }; Reply: { success: boolean } }>(
|
||||
"/:id",
|
||||
{
|
||||
preValidation: [app.authenticate, adminGuard],
|
||||
},
|
||||
async (request) => {
|
||||
const { id } = request.params;
|
||||
await musicService.deleteMusic(id);
|
||||
return { success: true };
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export default musicRoutes;
|
||||
Reference in New Issue
Block a user