From f25aa298c75addece8fa05201f6e061ad325286f Mon Sep 17 00:00:00 2001 From: rlacko <rlacko@sch.bme.hu> Date: Mon, 3 Aug 2020 00:20:29 +0200 Subject: [PATCH] comment documentation --- src/resources/comment/commentControllers.js | 16 ++- src/resources/comment/commentDocs.yml | 128 ++++++++++++++++++ src/resources/comment/commentModel.js | 3 +- src/resources/comment/commentRouter.js | 4 +- src/resources/solution/solutionControllers.js | 2 +- 5 files changed, 148 insertions(+), 5 deletions(-) create mode 100644 src/resources/comment/commentDocs.yml diff --git a/src/resources/comment/commentControllers.js b/src/resources/comment/commentControllers.js index 759cd8b..3dc1169 100644 --- a/src/resources/comment/commentControllers.js +++ b/src/resources/comment/commentControllers.js @@ -1,10 +1,11 @@ const { crudControllers } = require('../../utils/crud') +const { pick } = require('lodash') + const { Comment } = require('./commentModel') const { Solution } = require('../solution/solutionModel') const { Task } = require('../task/taskModel') const { Attendance } = require('../attendance/attendanceModel') const { Activity } = require('../activity/activityModel') -const { omit, pick } = require('lodash') const pickedKeys = [ '_id', @@ -119,10 +120,23 @@ exports.default.getMany = async (req, res) => { exports.default.createOne = async (req, res) => { try { + if (req.user.role != 'mentor' || !req.body.creator) + req.body.creator = req.user.schacc var comment = await Comment.create({ ...req.body, }) + // Add to parent + let updateParent = await getParentModel(comment).findById(comment.parentId) + if (updateParent.comments.indexOf(comment._id) == -1) { + updateParent.comments.push(comment._id) + try { + await updateParent.save() + } catch (error) { + await Comment.findByIdAndRemove(comment._id) + } + } + comment = await comment .populate('_creator', 'fullName nickName schacc') .execPopulate() diff --git a/src/resources/comment/commentDocs.yml b/src/resources/comment/commentDocs.yml new file mode 100644 index 0000000..2354352 --- /dev/null +++ b/src/resources/comment/commentDocs.yml @@ -0,0 +1,128 @@ +openapi: '3.0.2' +info: + title: 'Comment Endpoint' + version: '1.0' + +paths: + /comment: + get: + tags: + - 'Comment' + summary: 'Get a List of comments' + description: 'This can only be get by a mentor.' + operationId: 'getAllComments' + responses: + '200': + description: OK + content: + application/json: + schema: + type: 'array' + items: + $ref: '#/components/schemas/Comment' + post: + tags: + - 'Comment' + summary: 'Create a comment' + description: 'Only logged in users can create a comment. + The creator only can be set by Mentor. + By default the creator is own user. + Automatically adds itself to parent object.' + operationId: 'createComment' + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/Comment' + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Comment' + /comment/id/{id}: + get: + tags: + - 'Comment' + summary: 'Get a comment by ID' + description: 'If not mentor only own comment + or comment on own solution can be get.' + operationId: 'getComment' + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Comment' + delete: + tags: + - 'Comment' + summary: 'Delete a comment by ID' + description: 'Only logged in users can delete a comment. + To delete has to be the owner or mentor.' + operationId: 'deleteComment' + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Comment' + put: + tags: + - 'Comment' + summary: 'Update a comment by ID' + description: 'Only logged in users can update a comment. + To update has to be the owner or mentor.' + operationId: 'updateOneComment' + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/Comment' + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/Groups' + +components: + schemas: + Comment: + type: object + properties: + parentId: + type: string + description: ObjectId of the object that has the comment + parentType: + type: string + enum: + - 'solution' + - 'task' + - 'attendance' + - 'activity' + description: Name of the object that has the comment + creator: + type: string + description: Schacc of the creator. Default is own user + text: + type: string + isAnonim: + type: boolean + default: false + createdAt: + type: string + format: date-time + updatedAt: + type: string + format: date-time + required: + - parentId + - parentType + - creator + - text + - isAnonim diff --git a/src/resources/comment/commentModel.js b/src/resources/comment/commentModel.js index b205ed9..3fb0953 100644 --- a/src/resources/comment/commentModel.js +++ b/src/resources/comment/commentModel.js @@ -8,12 +8,13 @@ const CommentSchema = new mongoose.Schema( required: true, }, parentType: { - type: mongoose.Schema.Types.String, + type: String, required: true, enum: ['solution', 'task', 'attendance', 'activity'], }, creator: { type: String, + required: true, }, text: { type: String, diff --git a/src/resources/comment/commentRouter.js b/src/resources/comment/commentRouter.js index 03392a6..9d07c13 100644 --- a/src/resources/comment/commentRouter.js +++ b/src/resources/comment/commentRouter.js @@ -4,13 +4,13 @@ const { isLoggedIn, isMentor } = require('../../middlewares/auth') const router = Router() -// /api/item +// /api/v1/comment router .route('/') .get(isLoggedIn, isMentor, controllers.default.getMany) .post(isLoggedIn, controllers.default.createOne) -// /api/item/:id +// /api/v1/comment/id/:id router .route('/id/:id') .get(isLoggedIn, controllers.default.getOne) diff --git a/src/resources/solution/solutionControllers.js b/src/resources/solution/solutionControllers.js index 239df61..50316b9 100644 --- a/src/resources/solution/solutionControllers.js +++ b/src/resources/solution/solutionControllers.js @@ -49,7 +49,7 @@ exports.default.createOne = async (req, res) => { } if (task.solutions.indexOf(solution._id) == -1) { task.solutions.push(solution._id) - task.save() + await task.save() } let retSolution = await Solution.findById({ _id: solution._id }) -- GitLab