diff --git a/backend/controllers/forumController.js b/backend/controllers/forumController.js index 25ef95e..a37c9d3 100644 --- a/backend/controllers/forumController.js +++ b/backend/controllers/forumController.js @@ -58,6 +58,31 @@ const forumController = { console.error('Error in createTopic:', error); res.status(400).json({ error: error.message }); } + }, + + async getTopic(req, res) { + try { + const { userid: userId } = req.headers; + const { id: topicId } = req.params; + const topic = await forumService.getTopic(userId, topicId); + res.status(200).json(topic); + } catch (error) { + console.error('Error in getTopic:', error); + res.status(400).json({ error: error.message }); + } + }, + + async addMessage(req, res) { + try { + const { userid: userId } = req.headers; + const { id: topicId } = req.params; + const { content } = req.body; + const result = await forumService.addMessage(userId, topicId, content); + res.status(201).json(result); + } catch (error) { + console.error('Error in addMessage:', error); + res.status(400).json({ error: error.message }); + } } }; diff --git a/backend/models/forum/title.js b/backend/models/forum/title.js index 5463e27..2a2e21e 100644 --- a/backend/models/forum/title.js +++ b/backend/models/forum/title.js @@ -2,6 +2,11 @@ import { sequelize } from '../../utils/sequelize.js'; import { DataTypes } from 'sequelize'; const Title = sequelize.define('title', { + id: { + type: DataTypes.INTEGER, + primaryKey: true, + autoIncrement: true + }, title: { type: DataTypes.STRING, allowNull: false diff --git a/backend/routers/forumRouter.js b/backend/routers/forumRouter.js index e50ae44..6452e17 100644 --- a/backend/routers/forumRouter.js +++ b/backend/routers/forumRouter.js @@ -6,7 +6,9 @@ const forumRouter = Router(); forumRouter.use(authenticate); +forumRouter.post('/topic/:id/message', forumController.addMessage); forumRouter.post('/topic', forumController.createTopic); +forumRouter.get('/topic/:id', forumController.getTopic); forumRouter.post('/', forumController.createForum); forumRouter.delete('/:forumId', forumController.deleteForum); forumRouter.get('/:forumId/:page', forumController.getForum); diff --git a/backend/services/forumService.js b/backend/services/forumService.js index 73605fa..c221954 100644 --- a/backend/services/forumService.js +++ b/backend/services/forumService.js @@ -196,6 +196,103 @@ class ForumService extends BaseService { return this.getForum(hashedUserId, forumId, 1); } + async getTopic(hashedUserId, topicId) { + console.log('[ForumService.getTopic] - start'); + const user = await User.findOne({ + where: { + hashedId: hashedUserId + } + }); + if (!user) { + throw new Error('User not found.'); + } + const topic = await Title.findByPk(topicId, { + include: [ + { + model: Message, + as:'messages', + include: [ + { + model: User, + as: 'lastMessageUser', + attributes: ['hashedId', 'username'], + order: [['createdAt', 'ASC']] + } + ] + }, + { + model: User, + as: 'createdByUser', + attributes: ['username', 'hashedId'] + }, + { + model: Forum, + as: 'forum', + attributes: ['id', 'name'] + } + ] + }); + if (!topic) { + throw new Error('Topic not found.'); + } + console.log('[ForumService.getTopic] - check user permissions'); + const hasAccess = await this.checkForumAccess(topic.forum, user); + if (!hasAccess) { + throw new Error('Access denied.'); + } + console.log('[ForumService.getTopic] - return topic'); + return topic; + } + + async addMessage(hashedUserId, topicId, content) { + console.log('[ForumService.addMessage] - start'); + const user = await User.findOne({ + where: { + hashedId: hashedUserId + } + }); + if (!user) { + throw new Error('User not found.'); + } + const topic = await Title.findByPk(topicId, { + include: [ + { + model: Message, + as:'messages', + include: [ + { + model: User, + as: 'lastMessageUser', + attributes: ['hashedId', 'username'], + order: [['createdAt', 'ASC']] + } + ] + }, + { + model: User, + as: 'createdByUser', + attributes: ['username', 'hashedId'] + }, + { + model: Forum, + as: 'forum', + attributes: ['id', 'name'] + } + ] + }); + if (!topic) { + throw new Error('Topic not found.'); + } + const hasAccess = await this.checkForumAccess(topic.forum, user); + if (!hasAccess) { + throw new Error('Access denied.'); + } + console.log('[ForumService.addMessage] - create new message'); + await Message.create({ titleId: topicId, text: content, createdBy: user.id }); + console.log('[ForumService.addMessage] - return topic'); + return this.getTopic(hashedUserId, topicId); + } + async checkForumAccess(forum, user) { console.log('[ForumService.checkForumAccess] - start'); const { age } = user; diff --git a/frontend/src/i18n/locales/de/socialnetwork.json b/frontend/src/i18n/locales/de/socialnetwork.json index 1a8f899..b32677e 100644 --- a/frontend/src/i18n/locales/de/socialnetwork.json +++ b/frontend/src/i18n/locales/de/socialnetwork.json @@ -229,7 +229,8 @@ "next": "Nächste Seite", "last": "Letzte Seite", "page": "Seite <> von <>" - } + }, + "createNewMesssage": "Antwort senden" } } } \ No newline at end of file diff --git a/frontend/src/router/index.js b/frontend/src/router/index.js index 8353587..1398cce 100644 --- a/frontend/src/router/index.js +++ b/frontend/src/router/index.js @@ -16,6 +16,7 @@ import GuestbookView from '../views/social/GuestbookView.vue'; import DiaryView from '../views/social/DiaryView.vue'; import ForumAdminView from '../dialogues/admin/ForumAdminView.vue'; import ForumView from '../views/social/ForumView.vue'; +import ForumTopicView from '../views/social/ForumTopicView.vue'; const routes = [ { @@ -52,6 +53,12 @@ const routes = [ component: ForumView, meta: { requiresAuth: true } }, + { + path: '/socialnetwork/forumtopic/:id', + name: 'ForumTopic', + component: ForumTopicView, + meta: { requiresAuth: true } + }, { path: '/socialnetwork/diary', name: 'Diary', diff --git a/frontend/src/views/social/ForumTopicView.vue b/frontend/src/views/social/ForumTopicView.vue new file mode 100644 index 0000000..b878523 --- /dev/null +++ b/frontend/src/views/social/ForumTopicView.vue @@ -0,0 +1,127 @@ + + + + + \ No newline at end of file diff --git a/frontend/src/views/social/ForumView.vue b/frontend/src/views/social/ForumView.vue index 722860f..7e8f7dd 100644 --- a/frontend/src/views/social/ForumView.vue +++ b/frontend/src/views/social/ForumView.vue @@ -11,7 +11,7 @@ - @@ -148,7 +148,10 @@ export default { openProfile(id) { this.$root.$refs.userProfileDialog.userId = id; this.$root.$refs.userProfileDialog.open(); - } + }, + openTopic(topicId) { + this.$router.push(`/socialnetwork/forumtopic/${topicId}`); + }, } }