From ed755c3e884df113fc02ebb1ac309046ec34f694 Mon Sep 17 00:00:00 2001 From: Simon Nedjari Date: Mon, 9 Sep 2024 13:06:24 +0200 Subject: [PATCH 1/2] add teamComment controller --- server/controllers/teamComment.ts | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 server/controllers/teamComment.ts diff --git a/server/controllers/teamComment.ts b/server/controllers/teamComment.ts new file mode 100644 index 000000000..39bd3bc07 --- /dev/null +++ b/server/controllers/teamComment.ts @@ -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 }; From 940acbc07c19dd2662a2ec305dbd4863a028a032 Mon Sep 17 00:00:00 2001 From: Simon Nedjari Date: Mon, 9 Sep 2024 13:11:49 +0200 Subject: [PATCH 2/2] reference teamCommentController in controllers index --- server/controllers/index.ts | 2 ++ server/controllers/teamComment.ts | 9 ++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/server/controllers/index.ts b/server/controllers/index.ts index 5c91ffd7a..cb87d8fb8 100644 --- a/server/controllers/index.ts +++ b/server/controllers/index.ts @@ -17,6 +17,7 @@ import { statisticsController } from './statistics'; import { storyController } from './story'; import { studentController } from './student'; import { teacherController } from './teacher'; +import { teamCommentController } from './teamComment'; import { userController } from './user'; import { videoController } from './video'; import { villageController } from './village'; @@ -45,6 +46,7 @@ const controllers = [ studentController, featureFlagController, statisticsController, + teamCommentController, pelicoController, mediathequeController, ]; diff --git a/server/controllers/teamComment.ts b/server/controllers/teamComment.ts index 39bd3bc07..2184a9dd3 100644 --- a/server/controllers/teamComment.ts +++ b/server/controllers/teamComment.ts @@ -1,21 +1,20 @@ 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'); +const teamCommentController = new Controller('/team-comments'); // --- Get all comments. --- -commentController.get({ path: '', userType: UserType.ADMIN }, async (req: Request, res: Response) => { +teamCommentController.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) => { +teamCommentController.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 } }); @@ -31,4 +30,4 @@ commentController.put({ path: '/:commentId', userType: UserType.ADMIN }, async ( res.sendJSON(updatedTeamComment); }); -export { commentController }; +export { teamCommentController };