Version control and retrieval endpoints

This commit is contained in:
Rodrigo Pinto 2025-04-04 16:28:06 -03:00
commit 8ef27f33d6
6 changed files with 261 additions and 5 deletions

View file

@ -1,5 +1,6 @@
import { Request, Response, NextFunction } from 'express';
import { topics, Topic } from '../models/topic';
import { finder } from '../utils'
// Create a topic
export const createTopic = (req: Request, res: Response, next: NextFunction) => {
@ -24,7 +25,7 @@ export const createTopic = (req: Request, res: Response, next: NextFunction) =>
}
};
// Read all topics
// Retrieve all topics
export const getTopics = (req: Request, res: Response, next: NextFunction) => {
try {
res.json(topics);
@ -33,7 +34,7 @@ export const getTopics = (req: Request, res: Response, next: NextFunction) => {
}
};
// Read single topic
// Retrieve single topic
export const getTopicById = (req: Request, res: Response, next: NextFunction) => {
try {
const id = parseInt(req.params.id, 10);
@ -48,6 +49,39 @@ export const getTopicById = (req: Request, res: Response, next: NextFunction) =>
}
};
// 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);
}
};
// Update a topic (create nenw version)
export const updateTopic = (req: Request, res: Response, next: NextFunction) => {
try {