-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of https://github.com/ita-social-projects/Spac…
…eToStudy-BackEnd into develop
- Loading branch information
Showing
8 changed files
with
199 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |