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) => {
// if (req.body.poolID in pools) {
// res.json({ status: 'error', error: 'already_exists' });
// } else {
// 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' });
// }
});
if (req.session.accountID == null) {
res.status(401);
return res.json({ status: 'error', error: 'need_login' });
}
router.patch('/pool', (req, res) => {
// if (!(req.body.poolID in pools)) {
// res.json({ status: 'error', error: 'not_found' });
// } else {
// let pool = pools[req.body.poolID];
// pool.title = req.body.title;
// pool.description = req.body.description;
// pool.driver_id = req.body.driver_id;
// pool.update_time = req.body.update_time;
// pool.status = req.body.status;
// pool.capacity = req.body.capacity;
// pool.direction = req.body.direction;
// res.json({ status: 'success' });
// }
});
const userID = req.session.accountID;
const {
capacity,
description,
direction,
end_time,
group_id,
start_time,
title,
type,
} = req.body;
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) => {
if (typeof req.query.groupID != 'string') {

View File

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