diff --git a/src/api/group.ts b/src/api/group.ts new file mode 100644 index 0000000..7f9d442 --- /dev/null +++ b/src/api/group.ts @@ -0,0 +1,41 @@ +import { Router } from 'express'; +import { getGroupByID } from '../data'; +import { GroupModel, PoolModel } from '../models'; + +export const router = Router(); + +router.get('/:groupID/pools', async (req, res) => { + let groupID = req.params.groupID; + let pools = await PoolModel.find({ group_id: groupID }).exec(); + + res.json({ status: 'success', data: pools }); +}); + +router.get('/:groupID', async (req, res) => { + let groupID = req.params.groupID; + let group = await getGroupByID(groupID); + + if (group) { + res.json({ status: 'success', data: group }); + } else { + res.json({ status: 'error', error: 'not_found' }); + } +}); + +router.post('/', async (req, res) => { + const userID = req.session.accountID; + const name = req.body.name; + + const group = new GroupModel(); + group.set('name', name); + group.set('creator_id', userID); + group + .save() + .then((group) => { + res.json({ status: 'success', id: group._id }); + }) + .catch((err) => { + console.error('Error when creating a group:', err); + res.json({ status: 'error' }); + }); +}); diff --git a/src/api/index.ts b/src/api/index.ts index 0172191..8ad7c16 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,93 +1,15 @@ import { Router } from 'express'; import { createSessionFromCodeAndProvider } from '../auth'; -import { getGroupByID, getPoolByID, getPoolsWithUser } from '../data'; -import { GroupModel, PoolModel } from '../models'; import * as user from './user'; import * as pool from './pool'; +import * as group from './group'; export const router = Router(); router.use('/user', user.router); router.use('/pool', pool.router); - -router.get('/group', async (req, res) => { - if (typeof req.query.groupID != 'string') { - return res.json({ status: 'error' }); - } - - let groupID = req.query.groupID; - let group = await getGroupByID(groupID); - - if (group) { - res.json({ status: 'success', data: group }); - } else { - res.json({ status: 'error', error: 'not_found' }); - } -}); - -router.get('/group_pools', async (req, res) => { - if (typeof req.query.groupID != 'string') { - res.json({ status: 'error', error: 'need_group_id' }); - return; - } - - let groupID = req.query.groupID; - let pools = await PoolModel.find({ group_id: groupID }).exec(); - - res.json({ status: 'success', data: pools }); -}); - -router.post('/join_pool', async (req, res) => { - if (!req.session.accountID) { - return res.json({ status: 'error', error: 'need_login' }); - } else { - let poolID = req.body.id; - let userID = req.session.accountID; - - await PoolModel.findByIdAndUpdate(poolID, { - $addToSet: { participant_ids: userID }, - }).exec(); - - res.json({ status: 'success' }); - } -}); - -router.post('/group', (req, res) => { - if (req.session.accountID == null) { - res.status(401); - return res.json({ status: 'error', error: 'need_login' }); - } - - const userID = req.session.accountID; - const name = req.body.name; - - const group = new GroupModel(); - group.set('name', name); - group.set('creator_id', userID); - group - .save() - .then((group) => { - res.json({ status: 'success', id: group._id }); - }) - .catch((err) => { - console.error('Error when creating a group:', err); - res.json({ status: 'error' }); - }); -}); - -router.get('/my_pools', async (req, res) => { - if (req.session.accountID == null) { - res.status(401); - return res.json({ status: 'error', error: 'need_login' }); - } - let pools = await getPoolsWithUser(req.session.accountID); - if (pools) { - res.json({ status: 'success', data: pools }); - } else { - res.json({ status: 'error', error: 'not_found' }); - } -}); +router.use('/group', group.router); router.post('/create_session', (req, res) => { const { code, provider } = req.body; diff --git a/src/api/pool.ts b/src/api/pool.ts index 893bb4f..6614a3f 100644 --- a/src/api/pool.ts +++ b/src/api/pool.ts @@ -5,11 +5,22 @@ import requireApiAuth from '../requireApiAuth'; export const router = Router(); +router.post('/:poolID/join', async (req, res) => { + const userID = req.session.accountID; + const poolID = req.params.poolID; + + await PoolModel.findByIdAndUpdate(poolID, { + $addToSet: { participant_ids: userID }, + }).exec(); + + res.json({ status: 'success' }); +}); + router.get('/:poolID', async (req, res) => { const pool = await PoolModel.findById(new ObjectID(req.params.poolID)).exec(); if (pool) { - res.json({ status: 'success', data: pool }); + res.json({ status: 'success', data: pool.toJSON() }); } else { res.json({ status: 'error', error: 'not_found' }); }