From 86b79a7f497c8e818d04640ac05f45bd7fd0ba10 Mon Sep 17 00:00:00 2001 From: Primakov Alexandr Alexandrovich Date: Sat, 18 Jan 2025 17:45:16 +0300 Subject: [PATCH] comments --- server/routers/todo/comment.js | 53 +++++++++++++++++++++++ server/routers/todo/const.js | 2 +- server/routers/todo/index.js | 2 + server/routers/todo/model/todo/comment.js | 27 ++++++++++++ server/routers/todo/model/todo/item.js | 49 +++++++++++++-------- server/routers/todo/routes copy.js | 49 --------------------- server/routers/todo/routes.js | 5 ++- 7 files changed, 118 insertions(+), 69 deletions(-) create mode 100644 server/routers/todo/comment.js create mode 100644 server/routers/todo/model/todo/comment.js delete mode 100644 server/routers/todo/routes copy.js diff --git a/server/routers/todo/comment.js b/server/routers/todo/comment.js new file mode 100644 index 0000000..6b19ffe --- /dev/null +++ b/server/routers/todo/comment.js @@ -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 diff --git a/server/routers/todo/const.js b/server/routers/todo/const.js index 4b8fa36..ae0f7a3 100644 --- a/server/routers/todo/const.js +++ b/server/routers/todo/const.js @@ -3,5 +3,5 @@ exports.TODO_ITEM_MODEL_NAME = 'TODO_ITEM' exports.TODO_AUTH_PASSWD_MODEL_NAME = 'TODO_AUTH_PASSWD' 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" diff --git a/server/routers/todo/index.js b/server/routers/todo/index.js index 3e55066..b7ca0dc 100644 --- a/server/routers/todo/index.js +++ b/server/routers/todo/index.js @@ -4,8 +4,10 @@ const router = Router() const todoRouter = require('./routes') const authRouter = require('./auth') +const commentRouter = require('./comment') router.use('/auth', authRouter) +router.use('/comment', commentRouter) router.use(todoRouter) diff --git a/server/routers/todo/model/todo/comment.js b/server/routers/todo/model/todo/comment.js new file mode 100644 index 0000000..e535063 --- /dev/null +++ b/server/routers/todo/model/todo/comment.js @@ -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) diff --git a/server/routers/todo/model/todo/item.js b/server/routers/todo/model/todo/item.js index 59669ab..32ade76 100644 --- a/server/routers/todo/model/todo/item.js +++ b/server/routers/todo/model/todo/item.js @@ -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({ - title: String, - done: { type: Boolean, default: false }, - closed: Date, - created: { - type: Date, default: () => new Date().toISOString(), - }, - createdBy: { type: Schema.Types.ObjectId, ref: TODO_AUTH_USER_MODEL_NAME }, -}) + title: String, + done: { type: Boolean, default: false }, + closed: Date, + created: { + type: Date, + default: () => new Date().toISOString(), + }, + 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.set("toJSON", { + virtuals: true, + versionKey: false, +}); -schema.virtual('id').get(function () { - return this._id.toHexString() -}) +schema.virtual("id").get(function () { + return this._id.toHexString(); +}); -exports.ItemModel = model(TODO_ITEM_MODEL_NAME, schema) +schema.method('addComment', async function (commentId) { + this.comments.push(commentId) + await this.save() +}) + +exports.ItemModel = model(TODO_ITEM_MODEL_NAME, schema); diff --git a/server/routers/todo/routes copy.js b/server/routers/todo/routes copy.js deleted file mode 100644 index 736b1d5..0000000 --- a/server/routers/todo/routes copy.js +++ /dev/null @@ -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 diff --git a/server/routers/todo/routes.js b/server/routers/todo/routes.js index a9878f6..fbf474b 100644 --- a/server/routers/todo/routes.js +++ b/server/routers/todo/routes.js @@ -51,7 +51,10 @@ router.post('/item', requiredValidate('todoId', 'title'), async (req, res) => { router.get('/:todoId', async (req, res) => { const { todoId } = req.params - const list = await ListModel.findById(todoId).populate('items').exec() + const list = await ListModel + .findById(todoId) + .populate('items') + .exec() if (!list) { throw new Error('list not found')