add basic API

This commit is contained in:
Michael Fatemi 2021-04-10 11:36:39 -04:00
parent 89c1742515
commit b980608f86

49
src/api.ts Normal file
View File

@ -0,0 +1,49 @@
import { Router } from 'express';
import { getGroupByID, getPostByID, getUserByID } from './data';
export const router = Router();
router.get('/user', (req, res) => {
if (typeof req.query.userID != 'string') {
return;
}
let userID = req.query.userID;
let user = getUserByID(userID);
if (user) {
res.json({ status: 'success', data: user });
} else {
res.json({ status: 'error', error: 'not_found' });
}
});
router.get('/post', (req, res) => {
if (typeof req.query.postID != 'string') {
return;
}
let postID = req.query.postID;
let post = getPostByID(postID);
if (post) {
res.json({ status: 'success', data: post });
} else {
res.json({ status: 'error', error: 'not_found' });
}
});
router.get('/group', (req, res) => {
if (typeof req.query.groupID != 'string') {
return;
}
let groupID = req.query.groupID;
let group = getGroupByID(groupID);
if (group) {
res.json({ status: 'success', data: group });
} else {
res.json({ status: 'error', error: 'not_found' });
}
});