Skip to content

Commit

Permalink
saveComment
Browse files Browse the repository at this point in the history
  • Loading branch information
Dobrunia committed Nov 21, 2023
1 parent 7b8f12c commit 192c3de
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
26 changes: 26 additions & 0 deletions controlers/message-controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getCommentsByPost, saveComment } from '../services/sqlwrapper.js';
import { addPost, saveMessage } from '../services/user-service.js';

import Multer from 'multer';
Expand Down Expand Up @@ -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 = {
Expand All @@ -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();
4 changes: 4 additions & 0 deletions router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
33 changes: 31 additions & 2 deletions services/sqlwrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ export async function findUserInfoById(userId): Promise<mysql.RowDataPacket[]> {
}
}

export async function getCommentsByPost(postId): Promise<mysql.RowDataPacket[]> {
try {
const results = await conn.query<RowDataPacket[]>(
`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<mysql.RowDataPacket[]> {
try {
const results = await conn.query<RowDataPacket[]>(
Expand Down Expand Up @@ -131,6 +146,18 @@ export async function setPost(DATA): Promise<mysql.RowDataPacket[]> {
}
}

export async function saveComment(DATA): Promise<mysql.RowDataPacket[]> {
try {
const results = await conn.query<RowDataPacket[]>(
'INSERT INTO comments SET ?',
DATA,
);
return results[0];
} catch (ex) {
console.log(ex);
}
}

export async function createUserInfoTable(
myId,
): Promise<mysql.RowDataPacket[]> {
Expand Down Expand Up @@ -287,10 +314,12 @@ export async function returnAllUserPost(
): Promise<mysql.RowDataPacket[]> {
try {
const results = await conn.query<RowDataPacket[]>(
`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];
Expand Down

0 comments on commit 192c3de

Please sign in to comment.