import { Router } from "express"; import ListsModel from '../../model/todo-list'; import ItemModel from '../../model/todo-item'; export const router = Router(); router.get('/lists', async (req, res) => { const lists = await ListsModel.find({}); res.json(lists); }) router.post('/list', async (req, res) => { const { name } = req.body const list = await ListsModel.create({ name }); res.json(list); }) router.delete('/item/:itemId', async (req, res) => { const { itemId } = req.params; const item = await ItemModel.findById(itemId); if (!item) throw new Error('Item not found'); await ItemModel.findByIdAndDelete(itemId); res.send(item); }) router.get('/list/:listId', async (req, res) => { const { listId } = req.params; const list = await ListsModel .findById(listId) .populate('items') .exec(); if (!list) throw new Error('List not found'); res.json(list); }) router.post('/:listId/item', async (req, res) => { const { listId } = req.params const { title, description = '' } = req.body const list = await ListsModel.findById(listId); if (!list) { throw new Error('List not found'); } const item = await ItemModel.create({ title, description }); await (list as any).addItem(item); res.send(item) })