mirror of
https://github.com/myfatemi04/wheelshare-old-backend.git
synced 2025-04-21 12:10:17 -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,
|
||||
getUserByID,
|
||||
} from './data';
|
||||
import { PoolModel } from './models';
|
||||
import { GroupModel, PoolModel } from './models';
|
||||
|
||||
export const router = Router();
|
||||
|
||||
|
@ -143,15 +143,26 @@ router.post('/join_pool', async (req, res) => {
|
|||
});
|
||||
|
||||
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' });
|
||||
// }
|
||||
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) => {
|
||||
|
|
|
@ -33,9 +33,10 @@ const UserModel: Model<User> = model('User', UserSchema);
|
|||
const GroupSchema: Schema = new Schema({
|
||||
name: { 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({
|
||||
text: { type: String, required: true },
|
||||
|
|
Loading…
Reference in New Issue
Block a user