Skip to content
Snippets Groups Projects
Commit f25aa298 authored by Rafael László's avatar Rafael László :speech_balloon:
Browse files

comment documentation

parent 96c1f46f
Branches
No related tags found
2 merge requests!20working endpoints and documentation,!18Resolve "Átfaktorálás"
Pipeline #4714 passed
const { crudControllers } = require('../../utils/crud') const { crudControllers } = require('../../utils/crud')
const { pick } = require('lodash')
const { Comment } = require('./commentModel') const { Comment } = require('./commentModel')
const { Solution } = require('../solution/solutionModel') const { Solution } = require('../solution/solutionModel')
const { Task } = require('../task/taskModel') const { Task } = require('../task/taskModel')
const { Attendance } = require('../attendance/attendanceModel') const { Attendance } = require('../attendance/attendanceModel')
const { Activity } = require('../activity/activityModel') const { Activity } = require('../activity/activityModel')
const { omit, pick } = require('lodash')
const pickedKeys = [ const pickedKeys = [
'_id', '_id',
...@@ -119,10 +120,23 @@ exports.default.getMany = async (req, res) => { ...@@ -119,10 +120,23 @@ exports.default.getMany = async (req, res) => {
exports.default.createOne = async (req, res) => { exports.default.createOne = async (req, res) => {
try { try {
if (req.user.role != 'mentor' || !req.body.creator)
req.body.creator = req.user.schacc
var comment = await Comment.create({ var comment = await Comment.create({
...req.body, ...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 comment = await comment
.populate('_creator', 'fullName nickName schacc') .populate('_creator', 'fullName nickName schacc')
.execPopulate() .execPopulate()
......
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
...@@ -8,12 +8,13 @@ const CommentSchema = new mongoose.Schema( ...@@ -8,12 +8,13 @@ const CommentSchema = new mongoose.Schema(
required: true, required: true,
}, },
parentType: { parentType: {
type: mongoose.Schema.Types.String, type: String,
required: true, required: true,
enum: ['solution', 'task', 'attendance', 'activity'], enum: ['solution', 'task', 'attendance', 'activity'],
}, },
creator: { creator: {
type: String, type: String,
required: true,
}, },
text: { text: {
type: String, type: String,
......
...@@ -4,13 +4,13 @@ const { isLoggedIn, isMentor } = require('../../middlewares/auth') ...@@ -4,13 +4,13 @@ const { isLoggedIn, isMentor } = require('../../middlewares/auth')
const router = Router() const router = Router()
// /api/item // /api/v1/comment
router router
.route('/') .route('/')
.get(isLoggedIn, isMentor, controllers.default.getMany) .get(isLoggedIn, isMentor, controllers.default.getMany)
.post(isLoggedIn, controllers.default.createOne) .post(isLoggedIn, controllers.default.createOne)
// /api/item/:id // /api/v1/comment/id/:id
router router
.route('/id/:id') .route('/id/:id')
.get(isLoggedIn, controllers.default.getOne) .get(isLoggedIn, controllers.default.getOne)
......
...@@ -49,7 +49,7 @@ exports.default.createOne = async (req, res) => { ...@@ -49,7 +49,7 @@ exports.default.createOne = async (req, res) => {
} }
if (task.solutions.indexOf(solution._id) == -1) { if (task.solutions.indexOf(solution._id) == -1) {
task.solutions.push(solution._id) task.solutions.push(solution._id)
task.save() await task.save()
} }
let retSolution = await Solution.findById({ _id: solution._id }) let retSolution = await Solution.findById({ _id: solution._id })
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment