From 192c3de16fe1787bc22812f3062bdc30ad76fe23 Mon Sep 17 00:00:00 2001 From: Dobrunia Date: Tue, 21 Nov 2023 12:35:17 +0300 Subject: [PATCH] saveComment --- controlers/message-controller.ts | 26 +++++++++++++++++++++++++ router/router.ts | 4 ++++ services/sqlwrapper.ts | 33 ++++++++++++++++++++++++++++++-- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/controlers/message-controller.ts b/controlers/message-controller.ts index abf3529..bddc6fa 100644 --- a/controlers/message-controller.ts +++ b/controlers/message-controller.ts @@ -1,3 +1,4 @@ +import { getCommentsByPost, saveComment } from '../services/sqlwrapper.js'; import { addPost, saveMessage } from '../services/user-service.js'; import Multer from 'multer'; @@ -28,6 +29,22 @@ class MessageController { } } + async saveComment(request, response, next) { + try { + const DATA = { + postId: request.body.postId, + authorId: request.body.authorId === null ? request.body.authorId : request.user.id, + commentText: request.body.commentText, + }; + const res = await saveComment(DATA); + if (res) { + response.json(res); + } + } catch (error) { + next(error); + } + } + async saveMessage(request, response, next) { try { const DATA = { @@ -44,6 +61,15 @@ class MessageController { next(error); } } + + async getCommentsByPostId(request, response, next) { + try { + const users_response = await getCommentsByPost(request.params.postId); + response.json(users_response); + } catch (error) { + next(error); + } + } } export const messageController = new MessageController(); diff --git a/router/router.ts b/router/router.ts index 0b9c824..de5aee1 100644 --- a/router/router.ts +++ b/router/router.ts @@ -32,9 +32,13 @@ router.get('/getAllMyFriends', checkHeader, userController.getAllMyFriends); //messageController router.post('/addPost', checkHeader, multer.single('photo'), messageController.addPost); +router.post('/saveComment', checkHeader, messageController.saveComment); router.post('/saveMessage', checkHeader, messageController.saveMessage); //router.post('/saveNewMessageNotification', checkHeader, messageController.saveNewMessageNotification); +router.get('/getCommentsByPostId/:postId', checkHeader, messageController.getCommentsByPostId); + + //chatController router.post('/createNewChat', checkHeader, chatController.createNewChat); router.post('/writeNewUserInChat', checkHeader, chatController.writeNewUserInChat); diff --git a/services/sqlwrapper.ts b/services/sqlwrapper.ts index fbb1d9b..cdd05d1 100644 --- a/services/sqlwrapper.ts +++ b/services/sqlwrapper.ts @@ -48,6 +48,21 @@ export async function findUserInfoById(userId): Promise { } } +export async function getCommentsByPost(postId): Promise { + try { + const results = await conn.query( + `SELECT c.*, u.avatar, u.id AS userId + FROM comments c + LEFT JOIN users u ON c.authorId = u.id + WHERE c.postId = ?`, + [postId], + ); + return await results[0]; + } catch (ex) { + console.log(ex); + } +} + export async function returnAllPrivateChats(): Promise { try { const results = await conn.query( @@ -131,6 +146,18 @@ export async function setPost(DATA): Promise { } } +export async function saveComment(DATA): Promise { + try { + const results = await conn.query( + 'INSERT INTO comments SET ?', + DATA, + ); + return results[0]; + } catch (ex) { + console.log(ex); + } +} + export async function createUserInfoTable( myId, ): Promise { @@ -287,10 +314,12 @@ export async function returnAllUserPost( ): Promise { try { const results = await conn.query( - `SELECT posts.id AS postsid, posts.wallId, posts.authorId, posts.text, posts.photos, posts.files, posts.postTime, users.* + `SELECT posts.id AS postsid, posts.wallId, posts.authorId, posts.text, posts.photos, posts.files, posts.postTime, users.*, COUNT(comments.postId) AS commentCount FROM posts JOIN users ON posts.authorId = users.id - WHERE posts.wallId LIKE ?`, + LEFT JOIN comments ON posts.id = comments.postId + WHERE posts.wallId LIKE ? + GROUP BY posts.id`, [wallId], ); return results[0];