Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…eToStudy-BackEnd into develop
  • Loading branch information
Mav-Ivan committed Sep 13, 2023
2 parents e3eaed1 + 580d275 commit 9376b32
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 1 deletion.
18 changes: 18 additions & 0 deletions controllers/question.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const questionService = require('~/services/question')
const getRegex = require('~/utils/getRegex')
const getSortOptions = require('~/utils/getSortOptions')

const getQuestions = async (req, res) => {
const { title, sort, skip, limit } = req.query

const match = { title: getRegex(title) }
const sortOptions = getSortOptions(sort)

const questions = await questionService.getQuestions(match, sortOptions, parseInt(skip), parseInt(limit))

res.status(200).json(questions)
}

module.exports = {
getQuestions
}
File renamed without changes.
74 changes: 74 additions & 0 deletions docs/questions/question.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
paths:
/questions:
get:
security:
- bearerAuth: []
tags:
- Questions
summary: Find all questions.
description: Finds and returns an array with a list of all questions.
produces:
- application/json
parameters:
- in: query
name: title
schema:
type: string
required: false
- in: query
name: sort
schema:
type: string
required: false
- in: query
name: skip
schema:
type: string
required: false
- in: query
name: limit
schema:
type: string
required: false
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/definitions/quizBody'
example:
items:
- _id: 64ca5914b57f2442403394a5
title: Is Assembly best programming language?
answers:
- text: Yes,
isCorrect: false
- text: Yes, of course,
isCorrect: true
author: '6477007a6fa4d05e1a800ce5'
createdAt: 2023-20-01T13:25:36.292Z
updatedAt: 2023-20-01T13:25:36.292Z
- _id: 64ca5932b57f2442403394a9
title: What is the chemical symbol for water?
answers:
- text: H2O,
isCorrect: false
- text: O2H
isCorrect: true
- text: H2O2
isCorrect: true
createdAt: 2023-20-01T13:25:36.292Z
updatedAt: 2023-20-01T13:25:36.292Z
author: '6477007a6fa4d05e1a800ce5'
count: 2
401:
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/Error'
example:
status: 401
code: UNAUTHORIZED
message: The requested URL requires user authorization.
2 changes: 2 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const course = require('~/routes/course')
const quiz = require('~/routes/quiz')
const attachment = require('~/routes/attachment')
const finishedQuiz = require('~/routes/finishedQuiz')
const question = require('~/routes/question')

router.use('/auth', auth)
router.use('/users', user)
Expand All @@ -39,5 +40,6 @@ router.use('/courses', course)
router.use('/quizzes', quiz)
router.use('/attachments', attachment)
router.use('/finished-quizzes', finishedQuiz)
router.use('/questions', question)

module.exports = router
11 changes: 11 additions & 0 deletions routes/question.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const router = require('express').Router()

const questionController = require('~/controllers/question')
const asyncWrapper = require('~/middlewares/asyncWrapper')
const { authMiddleware } = require('~/middlewares/auth')

router.use(authMiddleware)

router.get('/', asyncWrapper(questionController.getQuestions))

module.exports = router
5 changes: 4 additions & 1 deletion services/attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ const attachmentService = {
const [fileExtension] = attachment.fileName.split('.').reverse()
const newFileName = `${fileName}.${fileExtension}`

attachment.fileName = newFileName

await attachment.validate()

const newLink = await uploadService.updateFile(attachment.link, newFileName, ATTACHMENT)

attachment.fileName = newFileName
attachment.link = newLink
}

Expand Down
19 changes: 19 additions & 0 deletions services/question.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const Question = require('~/models/question')

const questionService = {
getQuestions: async (match, sort, skip = 0, limit = 10) => {
const items = await Question
.find(match)
.collation({ locale: 'en', strength: 1 })
.sort(sort)
.skip(skip)
.limit(limit)
.lean()
.exec()
const count = await Question.countDocuments(match)

return { items, count }
}
}

module.exports = questionService
71 changes: 71 additions & 0 deletions test/integration/controllers/question.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const { serverInit, serverCleanup, stopServer } = require('~/test/setup')
const { expectError } = require('~/test/helpers')
const { UNAUTHORIZED } = require('~/consts/errors')
const testUserAuthentication = require('~/utils/testUserAuth')
const TokenService = require('~/services/token')
const {
roles: { TUTOR }
} = require('~/consts/auth')
const Question = require('~/models/question')

const endpointUrl = '/questions/'

const testQuestionData = {
title: 'Assembly',
answers: [
{
text: 'Yes',
isCorrect: true
},
{
text: 'Yes, of course',
isCorrect: false
}
]
}


describe('Question controller', () => {
let app, server, accessToken, currentUser, testQuestion

beforeAll(async () => {
;({ app, server } = await serverInit())
})

beforeEach(async () => {
accessToken = await testUserAuthentication(app, { role: TUTOR })

currentUser = TokenService.validateAccessToken(accessToken)

testQuestion = await Question.create({ author: currentUser.id, ...testQuestionData })
})

afterEach(async () => {
await serverCleanup()
})

afterAll(async () => {
await stopServer(server)
})

describe(`GET ${endpointUrl}`, () => {
it('should return list of questions', async () => {
const questions = await app.get(endpointUrl).set('Authorization', accessToken)

expect(questions.statusCode).toBe(200)
expect(questions.count).toBe(1)
expect(questions.items).toContainObject({
_id: testQuestion._id,
createdAt: expect.any(String),
updatedAt: expect.any(String),
...testQuestionData
})
})

it('should throw UNAUTHORIZED', async () => {
const response = await app.get(endpointUrl)

expectError(401, UNAUTHORIZED, response)
})
})
})

0 comments on commit 9376b32

Please sign in to comment.