diff --git a/src/api.ts b/src/api.ts index df07f44..54e5169 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,117 +1,163 @@ -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' }); - } -}); +import { Router } from 'express'; +import { + getGroupByID, + getPoolByID, + getPoolsWithUser, + getUserByID, + groups, + users, + pools +} 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.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.post('/pool', (req, res) => { + if(req.body.poolID in pools) { + res.json({status: 'error', error: 'already_exists'}); + } + else { + pools[req.body.poolID] = { + id: req.body.poolID, + title: req.body.title, + description: req.body.description, + participant_ids: [], + driver_id: 'no driver', + create_time: 'create time', + update_time: 'update time', + comments: [], + group_id: 'no group', + status: 'pending', + capacity: 0, + direction: 'pickup', + author_id: 'no author', + type: 'request' + } + res.json({status: 'success'}); + } +}); + +router.delete('/pool', (req, res) => { + delete pools[req.body.poolID]; + res.json({status: 'success'}); +}); + +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' }); + } +}); + +router.post('/group', (req, res) => { + if(req.body.groupID in groups) { + res.json({status: 'error', error: 'already_exists'}); + } + else { + groups[req.body.groupID] = { + id: req.body.groupID, + member_ids: [] + } + res.json({status: 'success'}); + } +}); + +router.delete('/group', (req, res) => { + delete groups[req.body.groupID]; + res.json({status: 'success'}); +}); + +router.get('/my_pools', (req, res) => { + if (typeof req.query.userID != 'string') { + return; + } + + let userID = req.query.userID; + let groupsWithUser = getPoolsWithUser(userID); + + if (groupsWithUser) { + res.json({ status: 'success', data: groupsWithUser }); + } else { + res.json({ status: 'error', error: 'not_found' }); + } +}); + + + + + + + + + + + + +router.post('/my_pools', (req, res) => { + if(req.body.groupID in groups) { + res.json({status: 'error', error: 'already_exists'}); + } + else { + groups[req.body.groupID] = { + id: req.body.groupID, + member_ids: [] + } + res.json({status: 'success'}); + } +}); \ No newline at end of file