todo CR todo list

This commit is contained in:
Primakov Alexandr Alexandrovich 2025-01-18 17:08:09 +03:00
parent 38bc0c55c8
commit 0dcf961cce
2 changed files with 30 additions and 1 deletions

View File

@ -1,6 +1,6 @@
const { Schema, model } = require('mongoose') 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({ const schema = new Schema({
title: String, title: String,
@ -9,6 +9,7 @@ const schema = new Schema({
created: { created: {
type: Date, default: () => new Date().toISOString(), type: Date, default: () => new Date().toISOString(),
}, },
createdBy: { type: Schema.Types.ObjectId, ref: TODO_AUTH_USER_MODEL_NAME },
}) })
schema.set('toJSON', { schema.set('toJSON', {

View File

@ -30,5 +30,33 @@ router.get('/list', async (req, res) => {
res.send(getAnswer(null, items)) 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 module.exports = router