Files
yourpart3/backend/controllers/forumController.js
Torsten Schulz f74a16e58e Implemented Forum
2024-10-24 11:22:40 +02:00

90 lines
3.1 KiB
JavaScript

import forumService from '../services/forumService.js';
const forumController = {
async createForum(req, res) {
try {
const { userid: userId } = req.headers;
const { name, permissions } = req.body;
const forum = await forumService.createForum(userId, name, permissions);
res.status(201).json(forum);
} catch (error) {
console.error('Error in createForum:', error);
res.status(400).json({ error: error.message });
}
},
async deleteForum(req, res) {
try {
const { userid: userId } = req.headers;
const { forumId } = req.params;
await forumService.deleteForum(userId, forumId);
res.status(200).json({ message: 'Forum deleted successfully' });
} catch (error) {
console.error('Error in deleteForum:', error);
res.status(400).json({ error: error.message });
}
},
async getAllForums(req, res) {
try {
const { userid: userId } = req.headers;
const forums = await forumService.getAllForums(userId);
res.status(200).json(forums);
} catch (error) {
console.error('Error in getAllForums:', error);
res.status(400).json({ error: error.message });
}
},
async getForum(req, res) {
try {
const { userid: userId } = req.headers;
const { forumId, page } = req.params;
const forum = await forumService.getForum(userId, forumId, page);
res.status(200).json(forum);
} catch (error) {
console.error('Error in getForum:', error);
res.status(400).json({ error: error.message });
}
},
async createTopic(req, res) {
try {
const { userid: userId } = req.headers;
const { forumId, title, content } = req.body;
const result = await forumService.createTopic(userId, forumId, title, content);
res.status(201).json(result);
} catch (error) {
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 });
}
}
};
export default forumController;