2025-04-02 16:34:16 -03:00
|
|
|
import { Request, Response, NextFunction } from 'express';
|
2025-04-05 01:15:42 -03:00
|
|
|
import { topics, Topic, TopicNode } from '../models/topic';
|
2025-04-04 16:28:06 -03:00
|
|
|
import { finder } from '../utils'
|
2025-04-02 16:34:16 -03:00
|
|
|
|
|
|
|
// Create a topic
|
|
|
|
export const createTopic = (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
try {
|
2025-04-05 01:15:42 -03:00
|
|
|
const { name, content, parentTopicId } = req.body;
|
|
|
|
const newTopic = new TopicNode(name, content, null, null, null, parentTopicId)
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-04-04 16:28:06 -03:00
|
|
|
// Retrieve all topics
|
2025-04-02 16:34:16 -03:00
|
|
|
export const getTopics = (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
try {
|
|
|
|
res.json(topics);
|
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2025-04-04 16:28:06 -03:00
|
|
|
// Retrieve single topic
|
2025-04-02 16:34:16 -03:00
|
|
|
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-04 16:28:06 -03:00
|
|
|
// Retrieve single version of a topic
|
|
|
|
export const getTopicByIdVersion = (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
try {
|
|
|
|
const id = parseInt(req.params.id, 10);
|
|
|
|
const version = parseInt(req.params.version, 10)
|
|
|
|
const topic = topics.find((i) => i.id === id && i.version === version);
|
|
|
|
if (!topic) {
|
|
|
|
res.status(404).json({ message: 'Topic not found' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
res.json(topic);
|
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Retrive topic and all subtopics recursively
|
|
|
|
export const getTopicByIdRecursive = (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;
|
|
|
|
}
|
|
|
|
const result = finder(topic.id)
|
|
|
|
|
|
|
|
res.json(result);
|
|
|
|
} 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);
|
2025-04-05 01:15:42 -03:00
|
|
|
const { name, content, parentTopicId } = 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]
|
2025-04-05 01:15:42 -03:00
|
|
|
const newTopic = oldTopic.update(name, content, parentTopicId)
|
2025-04-03 17:37:04 -03:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|