diff --git a/src/api.ts b/src/api.ts index 2f2e8e5..bd1fba7 100644 --- a/src/api.ts +++ b/src/api.ts @@ -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') { diff --git a/src/models.ts b/src/models.ts index 39e9ec7..f1157da 100644 --- a/src/models.ts +++ b/src/models.ts @@ -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 },