mirror of
https://github.com/myfatemi04/wheelshare-old-backend.git
synced 2025-04-21 20:19:49 -04:00
Add code for creating a group
This commit is contained in:
parent
70a32955e6
commit
f6e8c8f357
31
src/api.ts
31
src/api.ts
|
@ -6,7 +6,7 @@ import {
|
||||||
getPoolsWithUser,
|
getPoolsWithUser,
|
||||||
getUserByID,
|
getUserByID,
|
||||||
} from './data';
|
} from './data';
|
||||||
import { PoolModel } from './models';
|
import { GroupModel, PoolModel } from './models';
|
||||||
|
|
||||||
export const router = Router();
|
export const router = Router();
|
||||||
|
|
||||||
|
@ -143,15 +143,26 @@ router.post('/join_pool', async (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('/group', (req, res) => {
|
router.post('/group', (req, res) => {
|
||||||
// if (req.body.groupID in groups) {
|
if (req.session.accountID == null) {
|
||||||
// res.json({ status: 'error', error: 'already_exists' });
|
res.status(401);
|
||||||
// } else {
|
return res.json({ status: 'error', error: 'need_login' });
|
||||||
// groups[req.body.groupID] = {
|
}
|
||||||
// id: req.body.groupID,
|
|
||||||
// member_ids: [],
|
const userID = req.session.accountID;
|
||||||
// };
|
const name = req.body.name;
|
||||||
// res.json({ status: 'success' });
|
|
||||||
// }
|
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) => {
|
router.get('/my_pools', async (req, res) => {
|
||||||
|
|
|
@ -33,9 +33,10 @@ const UserModel: Model<User> = model('User', UserSchema);
|
||||||
const GroupSchema: Schema = new Schema({
|
const GroupSchema: Schema = new Schema({
|
||||||
name: { type: String, required: true },
|
name: { type: String, required: true },
|
||||||
member_ids: { type: [String], required: true },
|
member_ids: { type: [String], required: true },
|
||||||
|
creator_id: { type: String, required: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
const GroupModel = model('Group', GroupSchema);
|
const GroupModel: Model<Group> = model('Group', GroupSchema);
|
||||||
|
|
||||||
const CommentSchema: Schema = new Schema({
|
const CommentSchema: Schema = new Schema({
|
||||||
text: { type: String, required: true },
|
text: { type: String, required: true },
|
||||||
|
|
Loading…
Reference in New Issue
Block a user