This commit is contained in:
Primakov Alexandr Alexandrovich 2025-01-18 17:45:16 +03:00
parent d2d8b63aba
commit 86b79a7f49
7 changed files with 118 additions and 69 deletions

View File

@ -0,0 +1,53 @@
const { Router } = require('express')
const { expressjwt } = require('express-jwt')
const { getAnswer } = require('../../utils/common')
const { CommentModel } = require('./model/todo/comment')
const { ListModel } = require('./model/todo/list')
const { ItemModel } = require('./model/todo/item')
const { TOKEN_KEY } = require('./const')
const router = Router()
router.use(expressjwt({ secret: TOKEN_KEY, algorithms: ['HS256'] }))
router.post('/:todoId/:itemId', async (req, res) => {
const { text } = req.body
const { todoId, itemId } = req.params
const todo = await ListModel.findById(todoId)
if (!todo) {
return res.send(getAnswer(new Error('no such todo')))
}
const item = await ItemModel.findById(itemId)
if (!item) {
return res.send(getAnswer(new Error('no such item')))
}
const userId = req.auth.id
const comment = await CommentModel.create({ text, author: userId, createdBy: userId })
await item.addComment(comment.id)
res.send(getAnswer(null, comment))
})
router.get('/:todoId/:itemId', async (req, res) => {
const { todoId, itemId } = req.params
const todo = await ListModel.findById(todoId)
if (!todo) {
return res.send(getAnswer(new Error('no such todo')))
}
const item = await ItemModel.findById(itemId).populate('comments').exec()
if (!item) {
return res.send(getAnswer(new Error('no such item')))
}
res.send(getAnswer(null, item.comments))
})
module.exports = router

View File

@ -3,5 +3,5 @@ exports.TODO_ITEM_MODEL_NAME = 'TODO_ITEM'
exports.TODO_AUTH_PASSWD_MODEL_NAME = 'TODO_AUTH_PASSWD' exports.TODO_AUTH_PASSWD_MODEL_NAME = 'TODO_AUTH_PASSWD'
exports.TODO_AUTH_USER_MODEL_NAME = 'TODO_AUTH_USER' exports.TODO_AUTH_USER_MODEL_NAME = 'TODO_AUTH_USER'
exports.TODO_AUTH_CHAT_MODEL_NAME = 'TODO_AUTH_CHAT' exports.TODO_AUTH_COMMENTS_MODEL_NAME = 'TODO_AUTH_COMMENTS'
exports.TOKEN_KEY = process.env.TOKEN_KEY || "asdfhoa-podh829438132-iahda98gauj-dj2i3-111" exports.TOKEN_KEY = process.env.TOKEN_KEY || "asdfhoa-podh829438132-iahda98gauj-dj2i3-111"

View File

@ -4,8 +4,10 @@ const router = Router()
const todoRouter = require('./routes') const todoRouter = require('./routes')
const authRouter = require('./auth') const authRouter = require('./auth')
const commentRouter = require('./comment')
router.use('/auth', authRouter) router.use('/auth', authRouter)
router.use('/comment', commentRouter)
router.use(todoRouter) router.use(todoRouter)

View File

@ -0,0 +1,27 @@
const { Schema, model } = require('mongoose')
const { TODO_AUTH_COMMENTS_MODEL_NAME, TODO_AUTH_USER_MODEL_NAME } = require('../../const')
const schema = new Schema({
text: String,
created: {
type: Date, default: () => new Date().toISOString(),
},
answerTo: { type: Schema.Types.ObjectId, ref: TODO_AUTH_COMMENTS_MODEL_NAME },
createdBy: { type: Schema.Types.ObjectId, ref: TODO_AUTH_USER_MODEL_NAME },
author: { type: Schema.Types.ObjectId, ref: TODO_AUTH_USER_MODEL_NAME },
})
schema.set('toJSON', {
virtuals: true,
versionKey: false,
transform: function (doc, ret) {
delete ret._id
}
})
schema.virtual('id').get(function () {
return this._id.toHexString()
})
exports.CommentModel = model(TODO_AUTH_COMMENTS_MODEL_NAME, schema)

View File

@ -1,24 +1,37 @@
const { Schema, model } = require('mongoose') const { Schema, model } = require("mongoose");
const { TODO_ITEM_MODEL_NAME, TODO_AUTH_USER_MODEL_NAME } = require('../../const') const {
TODO_ITEM_MODEL_NAME,
TODO_AUTH_USER_MODEL_NAME,
TODO_AUTH_COMMENTS_MODEL_NAME,
} = require("../../const");
const schema = new Schema({ const schema = new Schema({
title: String, title: String,
done: { type: Boolean, default: false }, done: { type: Boolean, default: false },
closed: Date, closed: Date,
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 }, },
comments: [
{ type: Schema.Types.ObjectId, ref: TODO_AUTH_COMMENTS_MODEL_NAME },
],
createdBy: { type: Schema.Types.ObjectId, ref: TODO_AUTH_USER_MODEL_NAME },
});
schema.set("toJSON", {
virtuals: true,
versionKey: false,
});
schema.virtual("id").get(function () {
return this._id.toHexString();
});
schema.method('addComment', async function (commentId) {
this.comments.push(commentId)
await this.save()
}) })
schema.set('toJSON', { exports.ItemModel = model(TODO_ITEM_MODEL_NAME, schema);
virtuals: true,
versionKey: false,
})
schema.virtual('id').get(function () {
return this._id.toHexString()
})
exports.ItemModel = model(TODO_ITEM_MODEL_NAME, schema)

View File

@ -1,49 +0,0 @@
const { Router } = require('express')
const { ListModel } = require('./model/todo/list')
const { ItemModel } = require('./model/todo/item')
const { getAnswer } = require('../../utils/common')
const router = Router()
// test - http://localhost:8033/todo/list
router.get('/list', async (req, res) => {
const items = await ListModel
.find({})
.populate('items')
.exec()
res.send(getAnswer(null, items))
})
// test - http://localhost:8033/todo/list/create/new%20List%20Name
router.get('/list/create/:title', async (req, res) => {
const { title } = req.params
const list = await ListModel.create({ title })
res.send(getAnswer(null, list))
})
// test - http://localhost:8033/todo/item/create/:listId/new%20one
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

View File

@ -51,7 +51,10 @@ router.post('/item', requiredValidate('todoId', 'title'), async (req, res) => {
router.get('/:todoId', async (req, res) => { router.get('/:todoId', async (req, res) => {
const { todoId } = req.params const { todoId } = req.params
const list = await ListModel.findById(todoId).populate('items').exec() const list = await ListModel
.findById(todoId)
.populate('items')
.exec()
if (!list) { if (!list) {
throw new Error('list not found') throw new Error('list not found')