can add images to forum posts and comments
This commit is contained in:
@@ -2,6 +2,7 @@ const express = require('express');
|
||||
const { Op } = require('sequelize');
|
||||
const { ForumPost, ForumComment, PostTag, User } = require('../models');
|
||||
const { authenticateToken } = require('../middleware/auth');
|
||||
const { uploadForumPostImages, uploadForumCommentImages } = require('../middleware/upload');
|
||||
const logger = require('../utils/logger');
|
||||
const router = express.Router();
|
||||
|
||||
@@ -83,7 +84,7 @@ router.get('/posts', async (req, res) => {
|
||||
{
|
||||
model: PostTag,
|
||||
as: 'tags',
|
||||
attributes: ['tagName']
|
||||
attributes: ['id', 'tagName']
|
||||
}
|
||||
];
|
||||
|
||||
@@ -143,7 +144,7 @@ router.get('/posts/:id', async (req, res) => {
|
||||
{
|
||||
model: PostTag,
|
||||
as: 'tags',
|
||||
attributes: ['tagName']
|
||||
attributes: ['id', 'tagName']
|
||||
},
|
||||
{
|
||||
model: ForumComment,
|
||||
@@ -195,15 +196,28 @@ router.get('/posts/:id', async (req, res) => {
|
||||
});
|
||||
|
||||
// POST /api/forum/posts - Create new post
|
||||
router.post('/posts', authenticateToken, async (req, res) => {
|
||||
router.post('/posts', authenticateToken, uploadForumPostImages, async (req, res) => {
|
||||
try {
|
||||
const { title, content, category, tags } = req.body;
|
||||
let { title, content, category, tags } = req.body;
|
||||
|
||||
// Parse tags if they come as JSON string (from FormData)
|
||||
if (typeof tags === 'string') {
|
||||
try {
|
||||
tags = JSON.parse(tags);
|
||||
} catch (e) {
|
||||
tags = [];
|
||||
}
|
||||
}
|
||||
|
||||
// Extract image filenames if uploaded
|
||||
const images = req.files ? req.files.map(file => file.filename) : [];
|
||||
|
||||
const post = await ForumPost.create({
|
||||
title,
|
||||
content,
|
||||
category,
|
||||
authorId: req.user.id
|
||||
authorId: req.user.id,
|
||||
images
|
||||
});
|
||||
|
||||
// Create tags if provided
|
||||
@@ -227,7 +241,7 @@ router.post('/posts', authenticateToken, async (req, res) => {
|
||||
{
|
||||
model: PostTag,
|
||||
as: 'tags',
|
||||
attributes: ['tagName']
|
||||
attributes: ['id', 'tagName']
|
||||
}
|
||||
]
|
||||
});
|
||||
@@ -297,7 +311,7 @@ router.put('/posts/:id', authenticateToken, async (req, res) => {
|
||||
{
|
||||
model: PostTag,
|
||||
as: 'tags',
|
||||
attributes: ['tagName']
|
||||
attributes: ['id', 'tagName']
|
||||
}
|
||||
]
|
||||
});
|
||||
@@ -388,7 +402,7 @@ router.patch('/posts/:id/status', authenticateToken, async (req, res) => {
|
||||
{
|
||||
model: PostTag,
|
||||
as: 'tags',
|
||||
attributes: ['tagName']
|
||||
attributes: ['id', 'tagName']
|
||||
}
|
||||
]
|
||||
});
|
||||
@@ -464,7 +478,7 @@ router.patch('/posts/:id/accept-answer', authenticateToken, async (req, res) =>
|
||||
{
|
||||
model: PostTag,
|
||||
as: 'tags',
|
||||
attributes: ['tagName']
|
||||
attributes: ['id', 'tagName']
|
||||
}
|
||||
]
|
||||
});
|
||||
@@ -490,7 +504,7 @@ router.patch('/posts/:id/accept-answer', authenticateToken, async (req, res) =>
|
||||
});
|
||||
|
||||
// POST /api/forum/posts/:id/comments - Add comment/reply
|
||||
router.post('/posts/:id/comments', authenticateToken, async (req, res) => {
|
||||
router.post('/posts/:id/comments', authenticateToken, uploadForumCommentImages, async (req, res) => {
|
||||
try {
|
||||
const { content, parentCommentId } = req.body;
|
||||
const post = await ForumPost.findByPk(req.params.id);
|
||||
@@ -507,11 +521,15 @@ router.post('/posts/:id/comments', authenticateToken, async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
// Extract image filenames if uploaded
|
||||
const images = req.files ? req.files.map(file => file.filename) : [];
|
||||
|
||||
const comment = await ForumComment.create({
|
||||
postId: req.params.id,
|
||||
authorId: req.user.id,
|
||||
content,
|
||||
parentCommentId: parentCommentId || null
|
||||
parentCommentId: parentCommentId || null,
|
||||
images
|
||||
});
|
||||
|
||||
// Increment comment count
|
||||
@@ -653,7 +671,7 @@ router.get('/my-posts', authenticateToken, async (req, res) => {
|
||||
{
|
||||
model: PostTag,
|
||||
as: 'tags',
|
||||
attributes: ['tagName']
|
||||
attributes: ['id', 'tagName']
|
||||
}
|
||||
],
|
||||
order: [['createdAt', 'DESC']]
|
||||
|
||||
Reference in New Issue
Block a user