improve validation

This commit is contained in:
Michael Fatemi 2021-04-10 21:18:08 -04:00
parent f6e8c8f357
commit 85d54333be
2 changed files with 43 additions and 40 deletions

View File

@ -59,46 +59,49 @@ router.get('/pool', (req, res) => {
}); });
router.post('/pool', (req, res) => { router.post('/pool', (req, res) => {
// if (req.body.poolID in pools) { if (req.session.accountID == null) {
// res.json({ status: 'error', error: 'already_exists' }); res.status(401);
// } else { return res.json({ status: 'error', error: 'need_login' });
// pools[req.body.poolID] = { }
// id: req.body.poolID,
// title: req.body.title,
// description: req.body.description,
// participant_ids: [],
// driver_id: 'no driver',
// create_time: 'create time',
// update_time: 'update time',
// comments: [],
// group_id: 'no group',
// status: 'pending',
// capacity: 0,
// direction: 'pickup',
// author_id: 'no author',
// type: 'request',
// };
// res.json({ status: 'success' });
// }
});
router.patch('/pool', (req, res) => { const userID = req.session.accountID;
// if (!(req.body.poolID in pools)) { const {
// res.json({ status: 'error', error: 'not_found' }); capacity,
// } else { description,
// let pool = pools[req.body.poolID]; direction,
// pool.title = req.body.title; end_time,
// pool.description = req.body.description; group_id,
// pool.driver_id = req.body.driver_id; start_time,
// pool.update_time = req.body.update_time; title,
// pool.status = req.body.status; type,
// pool.capacity = req.body.capacity; } = req.body;
// pool.direction = req.body.direction;
// res.json({ status: 'success' });
// }
});
router.delete('/pool', (req, res) => {}); const pool = new PoolModel();
Object.assign(pool, {
author_id: userID,
capacity,
description,
direction,
status: 'pending',
title,
type,
participant_ids: [],
comments: [],
create_time: new Date().toISOString(),
update_time: new Date().toISOString(),
group_id,
});
pool
.save()
.then((pool) => {
res.json({ status: 'success', id: pool._id });
})
.catch((err) => {
console.error('Error when creating a pool:', err);
res.json({ status: 'error' });
});
});
router.get('/group', async (req, res) => { router.get('/group', async (req, res) => {
if (typeof req.query.groupID != 'string') { if (typeof req.query.groupID != 'string') {

View File

@ -49,7 +49,7 @@ export interface Pool extends Document {
title: string; title: string;
description: string; description: string;
participant_ids: string[]; participant_ids: string[];
driver_id: string; driver_id?: string;
create_time: string; create_time: string;
update_time: string; update_time: string;
comments: Comment[]; comments: Comment[];
@ -65,7 +65,7 @@ const PoolSchema: Schema = new Schema({
title: { type: String, required: true }, title: { type: String, required: true },
description: { type: String, required: true }, description: { type: String, required: true },
participant_ids: { type: [String], required: true }, participant_ids: { type: [String], required: true },
driver_id: { type: String, required: true }, driver_id: { type: String, required: false },
create_time: { type: String, required: true }, create_time: { type: String, required: true },
update_time: { type: String, required: true }, update_time: { type: String, required: true },
comments: { type: [CommentSchema], required: true }, comments: { type: [CommentSchema], required: true },