generated from nhcarrigan/template
feat: bunch of work done here, got comments and edit and delete
This commit is contained in:
@@ -5,12 +5,14 @@
|
||||
*/
|
||||
|
||||
import { FastifyPluginAsync } from "fastify";
|
||||
import { Game, CreateGameDto, UpdateGameDto } from "@library/shared-types";
|
||||
import { Game, CreateGameDto, UpdateGameDto, Comment, CreateCommentDto } from "@library/shared-types";
|
||||
import { GameService } from "../../services/game.service";
|
||||
import { CommentService } from "../../services/comment.service";
|
||||
import { adminGuard } from "../../middleware/admin-guard";
|
||||
|
||||
const gamesRoutes: FastifyPluginAsync = async (app) => {
|
||||
const gameService = new GameService();
|
||||
const commentService = new CommentService();
|
||||
|
||||
// Get all games (public route)
|
||||
app.get<{ Reply: Game[] }>("/", async () => {
|
||||
@@ -65,6 +67,41 @@ const gamesRoutes: FastifyPluginAsync = async (app) => {
|
||||
return { success: true };
|
||||
}
|
||||
);
|
||||
|
||||
// Get comments for a game (public route)
|
||||
app.get<{ Params: { id: string }; Reply: Comment[] }>(
|
||||
"/:id/comments",
|
||||
async (request) => {
|
||||
const { id } = request.params;
|
||||
return commentService.getCommentsForGame(id);
|
||||
}
|
||||
);
|
||||
|
||||
// Add comment to a game (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.createCommentForGame(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 gamesRoutes;
|
||||
Reference in New Issue
Block a user