Shortest path algorithm, tests and Postman

- Added algorithm and endpoint to find the shortest path between two
  topics;
- Added respective test;
- Added a JSON collection with API calls that can be imported into
  Postman for the convenience of the user.
This commit is contained in:
Rodrigo Pinto 2025-04-06 01:41:54 -03:00
commit d7748229ef
9 changed files with 419 additions and 118 deletions

View file

@ -1,110 +1,127 @@
import { Request, Response, NextFunction } from 'express';
import { topics, Topic, TopicNode } from '../models/topic';
import { finder } from '../utils'
import { Request, Response, NextFunction } from 'express'
import { topics, Topic, TopicNode } from '../models/topic'
import { finder, shortestPath } from '../utils'
// Create a topic
export const createTopic = (req: Request, res: Response, next: NextFunction) => {
try {
const { name, content, parentTopicId } = req.body;
const { name, content, parentTopicId } = req.body
const newTopic = new TopicNode(name, content, null, null, null, parentTopicId)
topics.push(newTopic);
topics.push(newTopic)
res.status(201).json(newTopic);
res.status(201).json(newTopic)
} catch (error) {
next(error);
next(error)
}
};
}
// Retrieve all topics
export const getTopics = (req: Request, res: Response, next: NextFunction) => {
try {
res.json(topics);
res.json(topics)
} catch (error) {
next(error);
next(error)
}
};
}
// Retrieve 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);
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.status(404).json({ message: 'Topic not found' })
return
}
res.json(topic);
res.status(302).json(topic)
} catch (error) {
next(error);
next(error)
}
};
}
// Retrieve single version of a topic
export const getTopicByIdVersion = (req: Request, res: Response, next: NextFunction) => {
try {
const id = parseInt(req.params.id, 10);
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);
const topic = topics.find((i) => i.id === id && i.version === version)
if (!topic) {
res.status(404).json({ message: 'Topic not found' });
return;
res.status(404).json({ message: 'Topic not found' })
return
}
res.json(topic);
res.status(302).json(topic)
} catch (error) {
next(error);
next(error)
}
};
}
// Retrive topic and all subtopics recursively
// Retrieve 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);
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.status(404).json({ message: 'Topic not found' })
return
}
const result = finder(topic.id)
res.json(result);
res.status(302).json(result)
} catch (error) {
next(error);
next(error)
}
};
}
// Update a topic (create nenw version)
export const updateTopic = (req: Request, res: Response, next: NextFunction) => {
try {
const id = parseInt(req.params.id, 10);
const { name, content, parentTopicId } = req.body;
const id = parseInt(req.params.id, 10)
const { name, content, parentTopicId } = req.body
const oldTopicArray = topics.filter(t => t.id === id)
if (oldTopicArray.length === 0) {
res.status(404).json({ message: 'Topic not found' });
return;
res.status(404).json({ message: 'Topic not found' })
return
}
const oldTopic = oldTopicArray[oldTopicArray.length - 1]
const newTopic = oldTopic.update(name, content, parentTopicId)
res.status(201).json(newTopic)
res.status(202).json(newTopic)
} catch (error) {
next(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);
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;
res.status(404).json({ message: 'Topic not found' })
return
}
const deletedTopic = topics.splice(topicIndex, 1)[0];
res.json(deletedTopic);
const deletedTopic = topics.splice(topicIndex, 1)[0]
res.status(202).json(deletedTopic)
} catch (error) {
next(error);
next(error)
}
};
}
// Find shortest path between topics
export const getShortestPath = (req: Request, res: Response, next: NextFunction) => {
try {
const idA = parseInt(req.params.idA, 10)
const idB = parseInt(req.params.idB, 10)
const result = shortestPath(idA, idB)
if (result.length === 0 || result.name) {
res.status(404).json('path does not exist')
} else {
res.status(302).json(result)
}
} catch (error) {
next(error)
}
}

View file

@ -57,4 +57,4 @@ export class TopicNode implements Topic {
}
}
export const topics: Topic[] = [];
export const topics: Topic[] = []

View file

@ -1,4 +1,4 @@
import { Router } from 'express';
import { Router } from 'express'
import {
createTopic,
getTopics,
@ -6,17 +6,19 @@ import {
getTopicByIdVersion,
getTopicByIdRecursive,
updateTopic,
deleteTopic
} from '../controllers/topicController';
deleteTopic,
getShortestPath
} from '../controllers/topicController'
const router = Router();
const router = Router({ mergeParams: true })
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);
router.get('/recursive/:id', getTopicByIdRecursive)
router.get('/shortest/:idA/:idB', getShortestPath)
router.get('/:id', getTopicById)
router.get('/:id/:version', getTopicByIdVersion)
router.get('/', getTopics)
router.post('/', createTopic)
router.put('/:id', updateTopic)
router.delete('/:id', deleteTopic)
export default router;
export default router

View file

@ -31,3 +31,50 @@ export const finder = (id: number) => {
return (result)
}
// Find the shortest path between topics in a tree
export const shortestPath = (idA: number, idB: number) => {
try {
// Find topics of each input id
const topicA = topics.find(t => t.id === idA)
const topicB = topics.find(t => t.id === idB)
if (!topicA || !topicB) throw new Error('Topic not found')
// Create arrays to hold paths from both topics up to their roots
const pathA = [topicA]
const pathB = [topicB]
// Create array to hold solution
let shortestPath = []
// Function to build paths from topic to its root
const buildPath = (topic: Topic, path: string) => {
if (topic.parentTopicId) {
const parent = topics.find(t => t.id === topic.parentTopicId)
if (path === 'a') pathA.push(parent)
if (path === 'b') pathB.push(parent)
buildPath(parent, path)
} else {
return
}
}
// Build both paths
buildPath(topicA, 'a')
buildPath(topicB, 'b')
// If there is a path linking both topics, save it as solution
pathA.forEach((p: Topic, i: number) => {
if (pathB.map(p => p.id).indexOf(p.id) !== -1) {
shortestPath = pathA.slice(0, i)
pathB.reverse()
shortestPath = shortestPath.concat(pathB.slice(pathB.indexOf(p)))
}
})
return (shortestPath)
} catch (error) {
return (error)
}
}