From b980608f8659b70c7802f4d40463371466db1a13 Mon Sep 17 00:00:00 2001 From: Michael Fatemi Date: Sat, 10 Apr 2021 11:36:39 -0400 Subject: [PATCH] add basic API --- src/api.ts | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/api.ts diff --git a/src/api.ts b/src/api.ts new file mode 100644 index 0000000..77922fa --- /dev/null +++ b/src/api.ts @@ -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' }); + } +});