todo list

This commit is contained in:
Primakov Alexandr Alexandrovich
2024-12-05 19:44:54 +03:00
parent ed9eb93013
commit 9723c825f7
11 changed files with 397 additions and 7 deletions
+66
View File
@@ -0,0 +1,66 @@
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._id);
res.send(item)
})