create todo list
This commit is contained in:
@@ -1,49 +1,24 @@
|
||||
const { Router } = require('express')
|
||||
const { expressjwt } = require('express-jwt')
|
||||
|
||||
const { getAnswer } = require('../../utils/common')
|
||||
|
||||
const { ListModel } = require('./model/todo/list')
|
||||
const { ItemModel } = require('./model/todo/item')
|
||||
const { getAnswer } = require('../../utils/common')
|
||||
const { TOKEN_KEY } = require('./const')
|
||||
const { requiredValidate } = require('./utils')
|
||||
|
||||
const router = Router()
|
||||
|
||||
// test - http://localhost:8033/todo/list
|
||||
router.get('/list', async (req, res) => {
|
||||
const items = await ListModel
|
||||
.find({})
|
||||
.populate('items')
|
||||
.exec()
|
||||
router.use(expressjwt({ secret: TOKEN_KEY, algorithms: ['HS256'] }))
|
||||
|
||||
res.send(getAnswer(null, items))
|
||||
})
|
||||
router.post('/', requiredValidate('title'), async (req, res) => {
|
||||
const { title } = req.body
|
||||
const userId = req.auth.id
|
||||
|
||||
// test - http://localhost:8033/todo/list/create/new%20List%20Name
|
||||
router.get('/list/create/:title', async (req, res) => {
|
||||
const { title } = req.params
|
||||
|
||||
const list = await ListModel.create({ title })
|
||||
const list = await ListModel.create({ title, createdBy: userId })
|
||||
|
||||
res.send(getAnswer(null, list))
|
||||
})
|
||||
|
||||
// test - http://localhost:8033/todo/item/create/:listId/new%20one
|
||||
router.get('/item/create/:listId/:name', async (req, res, next) => {
|
||||
const { name, listId } = req.params
|
||||
|
||||
try {
|
||||
const list = await ListModel.findById(listId)
|
||||
|
||||
if (!list) {
|
||||
throw new Error('no such list')
|
||||
}
|
||||
|
||||
const item = await ItemModel.create({ name })
|
||||
|
||||
list.addItem(item.id)
|
||||
|
||||
res.send(getAnswer(null, await ListModel.findById(listId)))
|
||||
} catch (error) {
|
||||
next(error)
|
||||
}
|
||||
})
|
||||
|
||||
module.exports = router
|
||||
|
||||
Reference in New Issue
Block a user