Skip to content

Commit

Permalink
add teamComment controller
Browse files Browse the repository at this point in the history
  • Loading branch information
SimNed committed Sep 9, 2024
1 parent 422cf77 commit ed755c3
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions server/controllers/teamComment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { NextFunction, Request, Response } from 'express';

import { Comment } from '../entities/comment';
import { TeamComment } from '../entities/teamComment';
import { UserType } from '../entities/user';
import { AppDataSource } from '../utils/data-source';
import { Controller } from './controller';

const commentController = new Controller('/team-comments');

// --- Get all comments. ---
commentController.get({ path: '', userType: UserType.ADMIN }, async (req: Request, res: Response) => {
const teamComments = await AppDataSource.getRepository(TeamComment).find();
res.sendJSON(teamComments);
});

// --- Edit one comment. ---
commentController.put({ path: '/:commentId', userType: UserType.ADMIN }, async (req: Request, res: Response, next: NextFunction) => {
const data = req.body;
const id = parseInt(req.params.commentId, 10) ?? 0;
const teamComment = await AppDataSource.getRepository(TeamComment).findOne({ where: { id } });
if (!teamComment) {
next();
return;
}

const updatedTeamComment = new TeamComment();
updatedTeamComment.id = id;
updatedTeamComment.text = data.text;
await AppDataSource.getRepository(TeamComment).save(updatedTeamComment);
res.sendJSON(updatedTeamComment);
});

export { commentController };

0 comments on commit ed755c3

Please sign in to comment.