2025-04-02 16:34:16 -03:00
|
|
|
import { Request, Response, NextFunction } from 'express';
|
|
|
|
import { topics, Topic } from '../models/topic';
|
|
|
|
|
|
|
|
// Create a topic
|
|
|
|
export const createTopic = (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
try {
|
|
|
|
const { name, content } = req.body;
|
2025-04-03 17:37:04 -03:00
|
|
|
|
2025-04-02 16:34:16 -03:00
|
|
|
const newTopic: Topic = {
|
|
|
|
id: Date.now(),
|
|
|
|
name,
|
|
|
|
content,
|
|
|
|
createdAt: new Date().toString(),
|
|
|
|
updatedAt: new Date().toString(),
|
|
|
|
version: 1,
|
2025-04-03 17:37:04 -03:00
|
|
|
parentTopicId: req.body.parentTopicId ?? undefined
|
2025-04-02 16:34:16 -03:00
|
|
|
};
|
2025-04-03 17:37:04 -03:00
|
|
|
|
2025-04-02 16:34:16 -03:00
|
|
|
topics.push(newTopic);
|
2025-04-03 17:37:04 -03:00
|
|
|
|
2025-04-02 16:34:16 -03:00
|
|
|
res.status(201).json(newTopic);
|
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Read all topics
|
|
|
|
export const getTopics = (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
try {
|
|
|
|
res.json(topics);
|
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Read single topic
|
|
|
|
export const getTopicById = (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
try {
|
|
|
|
const id = parseInt(req.params.id, 10);
|
|
|
|
const topic = topics.find((i) => i.id === id);
|
|
|
|
if (!topic) {
|
|
|
|
res.status(404).json({ message: 'Topic not found' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
res.json(topic);
|
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-04-03 17:37:04 -03:00
|
|
|
// Update a topic (create nenw version)
|
2025-04-02 16:34:16 -03:00
|
|
|
export const updateTopic = (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
try {
|
|
|
|
const id = parseInt(req.params.id, 10);
|
|
|
|
const { name } = req.body;
|
2025-04-03 17:37:04 -03:00
|
|
|
|
|
|
|
const oldTopicArray = topics.filter(t => t.id === id)
|
|
|
|
|
|
|
|
if (oldTopicArray.length === 0) {
|
2025-04-02 16:34:16 -03:00
|
|
|
res.status(404).json({ message: 'Topic not found' });
|
|
|
|
return;
|
|
|
|
}
|
2025-04-03 17:37:04 -03:00
|
|
|
|
|
|
|
const oldTopic = oldTopicArray[oldTopicArray.length - 1]
|
|
|
|
|
|
|
|
const newTopic: Topic = {
|
|
|
|
id: oldTopic.id,
|
|
|
|
name: oldTopic.name,
|
|
|
|
content: oldTopic.content,
|
|
|
|
createdAt: oldTopic.createdAt,
|
|
|
|
updatedAt: new Date().toString(),
|
|
|
|
version: oldTopic.version + 1,
|
|
|
|
parentTopicId: oldTopic.parentTopicId ?? undefined
|
|
|
|
};
|
|
|
|
|
|
|
|
topics.push(newTopic)
|
|
|
|
|
|
|
|
res.status(201).json(newTopic)
|
2025-04-02 16:34:16 -03:00
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Delete a topic
|
|
|
|
export const deleteTopic = (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
try {
|
|
|
|
const id = parseInt(req.params.id, 10);
|
|
|
|
const topicIndex = topics.findIndex((i) => i.id === id);
|
|
|
|
if (topicIndex === -1) {
|
|
|
|
res.status(404).json({ message: 'Topic not found' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const deletedTopic = topics.splice(topicIndex, 1)[0];
|
|
|
|
res.json(deletedTopic);
|
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
};
|