backend revamp: group

This commit is contained in:
Michael Fatemi 2021-04-10 21:48:45 -04:00
parent 6b5bc4f405
commit 1e398b1318
3 changed files with 55 additions and 81 deletions

41
src/api/group.ts Normal file
View File

@ -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' });
});
});

View File

@ -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;

View File

@ -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' });
}