generated from nhcarrigan/template
feat: add art component
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* @copyright 2026 NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { FastifyPluginAsync } from "fastify";
|
||||
import { Art, CreateArtDto, UpdateArtDto, Comment, CreateCommentDto } from "@library/shared-types";
|
||||
import { ArtService } from "../../services/art.service";
|
||||
import { CommentService } from "../../services/comment.service";
|
||||
import { adminGuard } from "../../middleware/admin-guard";
|
||||
|
||||
const artRoutes: FastifyPluginAsync = async (app) => {
|
||||
const artService = new ArtService();
|
||||
const commentService = new CommentService();
|
||||
|
||||
/**
|
||||
* Get all art (public route).
|
||||
*/
|
||||
app.get<{ Reply: Art[] }>("/", async () => {
|
||||
return artService.getAllArt();
|
||||
});
|
||||
|
||||
/**
|
||||
* Get single art piece by ID (public route).
|
||||
*/
|
||||
app.get<{ Params: { id: string }; Reply: Art | null }>(
|
||||
"/:id",
|
||||
async (request) => {
|
||||
const { id } = request.params;
|
||||
return artService.getArtById(id);
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Create new art piece (admin only).
|
||||
*/
|
||||
app.post<{ Body: CreateArtDto; Reply: Art }>(
|
||||
"/",
|
||||
{
|
||||
preValidation: [app.authenticate, adminGuard],
|
||||
},
|
||||
async (request) => {
|
||||
return artService.createArt(request.body);
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Update art by ID (admin only).
|
||||
*/
|
||||
app.put<{
|
||||
Params: { id: string };
|
||||
Body: UpdateArtDto;
|
||||
Reply: Art | null;
|
||||
}>(
|
||||
"/:id",
|
||||
{
|
||||
preValidation: [app.authenticate, adminGuard],
|
||||
},
|
||||
async (request) => {
|
||||
const { id } = request.params;
|
||||
return artService.updateArt(id, request.body);
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Delete art by ID (admin only).
|
||||
*/
|
||||
app.delete<{ Params: { id: string }; Reply: { success: boolean } }>(
|
||||
"/:id",
|
||||
{
|
||||
preValidation: [app.authenticate, adminGuard],
|
||||
},
|
||||
async (request) => {
|
||||
const { id } = request.params;
|
||||
await artService.deleteArt(id);
|
||||
return { success: true };
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Get comments for an art piece (public route).
|
||||
*/
|
||||
app.get<{ Params: { id: string }; Reply: Comment[] }>(
|
||||
"/:id/comments",
|
||||
async (request) => {
|
||||
const { id } = request.params;
|
||||
return commentService.getCommentsForArt(id);
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Add comment to an art piece (authenticated users).
|
||||
*/
|
||||
app.post<{ Params: { id: string }; Body: CreateCommentDto; Reply: Comment }>(
|
||||
"/:id/comments",
|
||||
{
|
||||
preValidation: [app.authenticate],
|
||||
},
|
||||
async (request) => {
|
||||
const { id } = request.params;
|
||||
const userId = request.user.id;
|
||||
return commentService.createCommentForArt(id, userId, request.body);
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Delete comment (admin only).
|
||||
*/
|
||||
app.delete<{ Params: { id: string; commentId: string }; Reply: { success: boolean } }>(
|
||||
"/:id/comments/:commentId",
|
||||
{
|
||||
preValidation: [app.authenticate, adminGuard],
|
||||
},
|
||||
async (request) => {
|
||||
const { commentId } = request.params;
|
||||
await commentService.deleteComment(commentId);
|
||||
return { success: true };
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
export default artRoutes;
|
||||
Reference in New Issue
Block a user