Version control and retrieval endpoints
This commit is contained in:
parent
bca3b6b699
commit
8ef27f33d6
6 changed files with 261 additions and 5 deletions
|
@ -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 {
|
||||
|
|
|
@ -3,14 +3,18 @@ import {
|
|||
createTopic,
|
||||
getTopics,
|
||||
getTopicById,
|
||||
getTopicByIdVersion,
|
||||
getTopicByIdRecursive,
|
||||
updateTopic,
|
||||
deleteTopic,
|
||||
deleteTopic
|
||||
} from '../controllers/topicController';
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get('/', getTopics);
|
||||
router.get('/recursive/:id', getTopicByIdRecursive);
|
||||
router.get('/:id', getTopicById);
|
||||
router.get('/:id/:version', getTopicByIdVersion);
|
||||
router.get('/', getTopics);
|
||||
router.post('/', createTopic);
|
||||
router.put('/:id', updateTopic);
|
||||
router.delete('/:id', deleteTopic);
|
||||
|
|
33
src/utils.ts
Normal file
33
src/utils.ts
Normal file
|
@ -0,0 +1,33 @@
|
|||
import { topics, Topic } from './models/topic'
|
||||
|
||||
class TopicNode {
|
||||
node: Topic
|
||||
childNode?: [TopicNode?]
|
||||
|
||||
constructor(node: Topic, childNode: TopicNode) {
|
||||
this.node = node
|
||||
this.childNode = childNode ? [childNode] : []
|
||||
}
|
||||
}
|
||||
|
||||
// Search all topics' children and return them as a tree
|
||||
export const finder = (id: number) => {
|
||||
const recursive = (topics: Topic[], id: number, node?: TopicNode) => {
|
||||
const topic = topics.find((i) => i.id === id)
|
||||
if (!node) {
|
||||
node = new TopicNode(topic, null)
|
||||
}
|
||||
const topicChildren = topics.filter((i) => i.parentTopicId === id)
|
||||
|
||||
topicChildren.forEach(t => {
|
||||
const turn = recursive(topics, t.id)
|
||||
node.childNode.push(turn)
|
||||
})
|
||||
|
||||
return (node)
|
||||
}
|
||||
|
||||
const result = recursive(topics, id)
|
||||
|
||||
return (result)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue