2024-10-16 11:06:23 +03:00
|
|
|
const { Router } = require('express')
|
|
|
|
|
|
|
|
const { ListModel } = require('../../data/model/todo/list')
|
2024-10-16 11:12:16 +03:00
|
|
|
const { ItemModel } = require('../../data/model/todo/item')
|
2024-10-16 11:06:23 +03:00
|
|
|
const { getAnswer } = require('../../utils/common')
|
|
|
|
|
|
|
|
const router = Router()
|
|
|
|
|
2024-10-16 11:08:03 +03:00
|
|
|
// test - http://localhost:8033/todo/list
|
2024-10-16 11:06:23 +03:00
|
|
|
router.get('/list', async (req, res) => {
|
|
|
|
const items = await ListModel
|
|
|
|
.find({})
|
|
|
|
.populate('items')
|
|
|
|
.exec()
|
|
|
|
|
|
|
|
res.send(getAnswer(null, items))
|
|
|
|
})
|
|
|
|
|
2024-10-16 11:08:03 +03:00
|
|
|
// test - http://localhost:8033/todo/list/create/new%20List%20Name
|
2024-10-16 11:06:23 +03:00
|
|
|
router.get('/list/create/:title', async (req, res) => {
|
|
|
|
const { title } = req.params
|
|
|
|
|
|
|
|
const list = await ListModel.create({ title })
|
|
|
|
|
|
|
|
res.send(getAnswer(null, list))
|
|
|
|
})
|
|
|
|
|
2024-10-16 11:08:03 +03:00
|
|
|
// test - http://localhost:8033/todo/item/create/:listId/new%20one
|
2024-10-16 11:06:23 +03:00
|
|
|
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
|