wheelshare-old-backend/src/api.ts

118 lines
2.3 KiB
TypeScript

import { Router } from 'express';
import {
getGroupByID,
getPoolByID,
getPoolsWithUser,
getUserByID,
groups,
users,
} from './data';
export const router = Router();
router.get('/user/:userID', (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.post('/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.post('/user', (req, res) => {
if (req.body.userID in users) {
res.json({ status: 'error', error: 'already_exists' });
} else {
users[req.body.userID] = {
id: req.body.userID,
username: req.body.username,
first_name: req.body.first_name,
last_name: req.body.last_name,
};
res.json({ status: 'success' });
}
});
router.delete('/user', (req, res) => {
delete users[req.body.userID];
res.json({ status: 'success' });
});
router.get('/pool', (req, res) => {
if (typeof req.query.poolID != 'string') {
return;
}
let poolID = req.query.poolID;
let pool = getPoolByID(poolID);
if (pool) {
res.json({ status: 'success', data: pool });
} else {
res.json({ status: 'error', error: 'not_found' });
}
});
router.get('/group/:groupID', (req, res) => {
if (!(req.params.groupID in groups)) {
res.send('invalid group id');
return;
}
res.json(groups[req.params.groupID]);
});
router.post('/group', (req, res) => {
groups[req.body.title] = {
id: 'temp id 0',
member_ids: [],
};
});
router.patch('/group', (req, res) => {});
router.delete('/group', (req, res) => {
delete groups[req.body.groupID];
});
router.get('/my_pools', (req, res) => {
let userID = 'myfatemi04';
let poolIDs = getPoolsWithUser(userID);
res.json({ status: 'success', data: poolIDs.map((id) => getPoolByID(id)) });
});
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' });
}
});