todo CR todo list
This commit is contained in:
parent
38bc0c55c8
commit
0dcf961cce
@ -1,6 +1,6 @@
|
||||
const { Schema, model } = require('mongoose')
|
||||
|
||||
const { TODO_ITEM_MODEL_NAME } = require('../../const')
|
||||
const { TODO_ITEM_MODEL_NAME, TODO_AUTH_USER_MODEL_NAME } = require('../../const')
|
||||
|
||||
const schema = new Schema({
|
||||
title: String,
|
||||
@ -9,6 +9,7 @@ const schema = new Schema({
|
||||
created: {
|
||||
type: Date, default: () => new Date().toISOString(),
|
||||
},
|
||||
createdBy: { type: Schema.Types.ObjectId, ref: TODO_AUTH_USER_MODEL_NAME },
|
||||
})
|
||||
|
||||
schema.set('toJSON', {
|
||||
|
@ -30,5 +30,33 @@ router.get('/list', async (req, res) => {
|
||||
res.send(getAnswer(null, items))
|
||||
})
|
||||
|
||||
router.post('/item', requiredValidate('todoId', 'title'), async (req, res) => {
|
||||
const { todoId, title } = req.body
|
||||
|
||||
const list = await ListModel.findById(todoId)
|
||||
|
||||
if (!list) {
|
||||
throw new Error('list not found')
|
||||
}
|
||||
|
||||
const userId = req.auth.id
|
||||
const item = await ItemModel.create({ title, createdBy: userId })
|
||||
|
||||
list.items.push(item)
|
||||
await list.save()
|
||||
})
|
||||
|
||||
router.get('/:todoId', async (req, res) => {
|
||||
const { todoId } = req.params
|
||||
|
||||
const list = await ListModel.findById(todoId).populate('items').exec()
|
||||
|
||||
if (!list) {
|
||||
throw new Error('list not found')
|
||||
}
|
||||
|
||||
res.send(getAnswer(null, list))
|
||||
})
|
||||
|
||||
|
||||
module.exports = router
|
||||
|
Loading…
Reference in New Issue
Block a user