From 0dcf961ccedcf260067c4bff84f73876e3518b78 Mon Sep 17 00:00:00 2001 From: Primakov Alexandr Alexandrovich Date: Sat, 18 Jan 2025 17:08:09 +0300 Subject: [PATCH] todo CR todo list --- server/routers/todo/model/todo/item.js | 3 ++- server/routers/todo/routes.js | 28 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/server/routers/todo/model/todo/item.js b/server/routers/todo/model/todo/item.js index e777be2..59669ab 100644 --- a/server/routers/todo/model/todo/item.js +++ b/server/routers/todo/model/todo/item.js @@ -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', { diff --git a/server/routers/todo/routes.js b/server/routers/todo/routes.js index cf2139f..d4d2fff 100644 --- a/server/routers/todo/routes.js +++ b/server/routers/todo/routes.js @@ -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