mirror of
https://github.com/myfatemi04/wheelshare-old-backend.git
synced 2025-04-21 12:10:17 -04:00
backend revamp: group
This commit is contained in:
parent
6b5bc4f405
commit
1e398b1318
41
src/api/group.ts
Normal file
41
src/api/group.ts
Normal 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' });
|
||||||
|
});
|
||||||
|
});
|
|
@ -1,93 +1,15 @@
|
||||||
import { Router } from 'express';
|
import { Router } from 'express';
|
||||||
import { createSessionFromCodeAndProvider } from '../auth';
|
import { createSessionFromCodeAndProvider } from '../auth';
|
||||||
import { getGroupByID, getPoolByID, getPoolsWithUser } from '../data';
|
|
||||||
import { GroupModel, PoolModel } from '../models';
|
|
||||||
|
|
||||||
import * as user from './user';
|
import * as user from './user';
|
||||||
import * as pool from './pool';
|
import * as pool from './pool';
|
||||||
|
import * as group from './group';
|
||||||
|
|
||||||
export const router = Router();
|
export const router = Router();
|
||||||
|
|
||||||
router.use('/user', user.router);
|
router.use('/user', user.router);
|
||||||
router.use('/pool', pool.router);
|
router.use('/pool', pool.router);
|
||||||
|
router.use('/group', group.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.post('/create_session', (req, res) => {
|
router.post('/create_session', (req, res) => {
|
||||||
const { code, provider } = req.body;
|
const { code, provider } = req.body;
|
||||||
|
|
|
@ -5,11 +5,22 @@ import requireApiAuth from '../requireApiAuth';
|
||||||
|
|
||||||
export const router = Router();
|
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) => {
|
router.get('/:poolID', async (req, res) => {
|
||||||
const pool = await PoolModel.findById(new ObjectID(req.params.poolID)).exec();
|
const pool = await PoolModel.findById(new ObjectID(req.params.poolID)).exec();
|
||||||
|
|
||||||
if (pool) {
|
if (pool) {
|
||||||
res.json({ status: 'success', data: pool });
|
res.json({ status: 'success', data: pool.toJSON() });
|
||||||
} else {
|
} else {
|
||||||
res.json({ status: 'error', error: 'not_found' });
|
res.json({ status: 'error', error: 'not_found' });
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user