Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added category field to lesson, attachment, quiz models #578

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/attachments/attachment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ paths:
fileName: EnglishB2Level
link: 'https://english-b2-test.com'
size: 230
category: [3da8767c9ad4c9a0b0eacd3]
createdAt: 2023-02-14T23:44:21.334Z
updatedAt: 2023-02-14T23:44:21.334Z
401:
Expand Down
15 changes: 15 additions & 0 deletions docs/attachments/attachments-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ definitions:
type: string
size:
type: number
category:
type: array
ref: '#/components/category'
createdAt:
type: string
format: date-time
Expand All @@ -23,6 +26,15 @@ definitions:
properties:
fileName:
type: string
link:
type: string
description:
type: string
size:
type: number
category:
type: array
ref: '#/components/category'
attachment:
type: object
properties:
Expand All @@ -34,6 +46,9 @@ definitions:
type: string
size:
type: number
category:
type: array
ref: '#/components/category'
createdAt:
type: string
format: date-time
Expand Down
5 changes: 4 additions & 1 deletion docs/lesson/lesson-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ definitions:
attachments:
type: array
$ref: '#/components/attachments'
category:
type: string
ref: '#/components/category'
required:
- title
- description
- description
13 changes: 8 additions & 5 deletions docs/lesson/lesson.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ paths:
description: With this lesson you will learn all about colors in English.
content: <h1>Title</h1>
author: 63da8767c9ad4c9a0b0eacd3
attachments: [ 63ed1cd25e9d781cdb6a6b15 ]
attachments: [63ed1cd25e9d781cdb6a6b15]
category: [3da8767c9ad4c9a0b0eacd3]
_id: 63ec1cd51e9d781cdb6f4b14
createdAt: 2023-02-14T23:44:21.334Z
updatedAt: 2023-02-14T23:44:21.334Z
Expand Down Expand Up @@ -86,7 +87,8 @@ paths:
description: With this lesson you will learn all about colors in English.
content: <h1>Title</h1>
author: 63da8767c9ad4c9a0b0eacd3
attachments: [ 63ed1cd25e9d781cdb6a6b15 ]
attachments: [63ed1cd25e9d781cdb6a6b15]
category: [3da8767c9ad4c9a0b0eacd3]
responses:
201:
description: Created
Expand All @@ -99,7 +101,8 @@ paths:
description: With this lesson you will learn all about colors in English.
content: <h1>Title</h1>
author: 63da8767c9ad4c9a0b0eacd3
attachments: [ 63ed1cd25e9d781cdb6a6b15 ]
attachments: [63ed1cd25e9d781cdb6a6b15]
category: [3da8767c9ad4c9a0b0eacd3]
_id: 63ec1cd51e9d781cdb6f4b14
createdAt: 2023-02-14T23:44:21.334Z
updatedAt: 2023-02-14T23:44:21.334Z
Expand Down Expand Up @@ -263,7 +266,7 @@ paths:
message: Lesson with the specified id was not found.
delete:
security:
- bearerAuth: [ ]
- bearerAuth: []
tags:
- Lessons
summary: Delete lesson by ID
Expand Down Expand Up @@ -318,4 +321,4 @@ paths:
example:
status: 404
code: DOCUMENT_NOT_FOUND
message: Lesson with the specified id was not found.
message: Lesson with the specified id was not found.
82 changes: 44 additions & 38 deletions docs/quiz/quiz-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@ definitions:
title:
type: string
items:
type: array
items:
type: object
properties:
question:
type: string
answers:
type: array
items:
type: object
text: string
isCorrect: boolean
type: array
items:
type: object
properties:
question:
type: string
answers:
type: array
items:
type: object
text: string
isCorrect: boolean
author:
type: string
ref: '#/components/user'
category:
type: string
ref: '#/components/category'
createdAt:
type: string
format: date-time
Expand All @@ -36,18 +39,18 @@ definitions:
title:
type: string
items:
type: array
items:
type: object
properties:
question:
type: string
answers:
type: array
items:
type: object
text: string
isCorrect: boolean
type: array
items:
type: object
properties:
question:
type: string
answers:
type: array
items:
type: object
text: string
isCorrect: boolean
quiz:
type: object
properties:
Expand All @@ -56,24 +59,27 @@ definitions:
title:
type: string
items:
type: array
items:
type: object
properties:
question:
type: string
answers:
type: array
items:
type: object
text: string
isCorrect: boolean
type: array
items:
type: object
properties:
question:
type: string
answers:
type: array
items:
type: object
text: string
isCorrect: boolean
author:
type: string
ref: '#/components/user',
category:
type: string
ref: '#/components/user'
ref: '#/components/category'
createdAt:
type: string
format: date-time
updatedAt:
type: string
format: date-time
format: date-time
5 changes: 4 additions & 1 deletion docs/quiz/quiz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ paths:
- text: Yes, of course,
isCorrect: true
author: '6477007a6fa4d05e1a800ce5'
category: '63bed9ef260f18d04ab15da'
- _id: 64ca5932b57f2442403394a9
title: Chemistry
items:
Expand All @@ -61,6 +62,7 @@ paths:
- text: H2O2
isCorrect: true
author: '6477007a6fa4d05e1a800ce5'
category: '63bed9ef260f18d04ab15da'
count: 2
401:
description: Unauthorized
Expand Down Expand Up @@ -178,7 +180,8 @@ paths:
isCorrect: false
- text: Yes, of course,
isCorrect: true
author: '6477007a6fa4d05e1a800ce5'
author: '6477007a6fa4d05e1a800ce5',
category: '63bed9ef260f18d04ab15da'
401:
description: Unauthorized
content:
Expand Down
6 changes: 5 additions & 1 deletion models/attachment.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { ATTACHMENT, USER } = require('~/consts/models')
const { ATTACHMENT, USER, CATEGORY } = require('~/consts/models')
const { Schema, model } = require('mongoose')
const { FIELD_CANNOT_BE_EMPTY, FIELD_CANNOT_BE_SHORTER, FIELD_CANNOT_BE_LONGER } = require('~/consts/errors')

Expand All @@ -24,6 +24,10 @@ const attachmentSchema = new Schema(
size: {
type: Number,
required: [true, FIELD_CANNOT_BE_EMPTY('size')]
},
category: {
type: [Schema.Types.ObjectId],
ref: CATEGORY
}
},
{
Expand Down
6 changes: 5 additions & 1 deletion models/lesson.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { Schema, model } = require('mongoose')
const { FIELD_CANNOT_BE_EMPTY, FIELD_CANNOT_BE_SHORTER, FIELD_CANNOT_BE_LONGER } = require('~/consts/errors')
const { USER, LESSON, ATTACHMENT } = require('~/consts/models')
const { USER, LESSON, ATTACHMENT, CATEGORY } = require('~/consts/models')
const lessonSchema = new Schema(
{
author: {
Expand Down Expand Up @@ -31,6 +31,10 @@ const lessonSchema = new Schema(
attachments: {
type: [Schema.Types.ObjectId],
ref: ATTACHMENT
},
category: {
type: [Schema.Types.ObjectId],
ref: CATEGORY
}
},
{
Expand Down
6 changes: 5 additions & 1 deletion models/quiz.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { Schema, model } = require('mongoose')
const { QUIZ, USER } = require('~/consts/models')
const { QUIZ, USER, CATEGORY } = require('~/consts/models')
const { FIELD_CANNOT_BE_EMPTY, FIELD_CANNOT_BE_LONGER, FIELD_CANNOT_BE_SHORTER } = require('~/consts/errors')

const quizSchema = new Schema(
Expand Down Expand Up @@ -41,6 +41,10 @@ const quizSchema = new Schema(
type: Schema.Types.ObjectId,
ref: USER,
required: [true, FIELD_CANNOT_BE_EMPTY('author')]
},
category: {
type: [Schema.Types.ObjectId],
ref: CATEGORY
}
},
{ timestamps: true, versionKey: false }
Expand Down
4 changes: 2 additions & 2 deletions services/lesson.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const { DOCUMENT_NOT_FOUND } = require('~/consts/errors')

const lessonService = {
createLesson: async (author, data) => {
const { title, description, attachments, content } = data
const { title, description, attachments, content, category } = data

return await Lesson.create({ author, title, description, attachments, content })
return await Lesson.create({ author, title, description, attachments, content, category })
},

getLessons: async (match, sort, skip, limit) => {
Expand Down
3 changes: 2 additions & 1 deletion services/quiz.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ const quizService = {
},

createQuiz: async (author, data) => {
const { title, items } = data
const { title, category, items } = data

return await Quiz.create({
title,
author,
category,
items
})
},
Expand Down
3 changes: 3 additions & 0 deletions test/integration/controllers/lesson.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const nonExistingLessonId = '64a51e41de4debbccf0b39b0'
const testLesson = {
title: 'title',
description: 'description',
category: ['63bed9ef260f18d04ab15da']
content: '<h1>Title</h1>',
attachments: ['65bed8ef260f18d04ab22da3', '65bed9ef260f19d05ab25bc6']
}
Expand Down Expand Up @@ -73,6 +74,7 @@ describe('Lesson controller', () => {
it('should create a lesson', async () => {
expect(testLessonResponse.statusCode).toBe(201)
expect(testLessonResponse.body).toMatchObject({
category: testLesson.category,
title: testLesson.title,
description: testLesson.description,
content: testLesson.content,
Expand Down Expand Up @@ -196,6 +198,7 @@ describe('Lesson controller', () => {
expect(response.body).toMatchObject({
_id: expect.any(String),
author: expect.any(String),
category: testLesson.category,
title: testLesson.title,
description: testLesson.description,
content: testLesson.content,
Expand Down
4 changes: 4 additions & 0 deletions test/integration/controllers/quiz.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const endpointUrl = '/quizzes/'

const testQuizData = {
title: 'Assembly',
category: ['63bed9ef260f18d04ab15da'],
items: [
{
question: 'Is it the best programming language?',
Expand Down Expand Up @@ -77,6 +78,7 @@ describe('Quiz controller', () => {
createdAt: expect.any(String),
updatedAt: expect.any(String),
author: currentUser.id,
category: testQuizData.category,
...testQuizData
})
})
Expand Down Expand Up @@ -110,6 +112,7 @@ describe('Quiz controller', () => {
author: currentUser.id,
createdAt: expect.any(String),
updatedAt: expect.any(String),
category: testQuizData.category,
...testQuizData
}
],
Expand Down Expand Up @@ -138,6 +141,7 @@ describe('Quiz controller', () => {
expect(response.body).toMatchObject({
_id: expect.any(String),
author: currentUser.id,
category: testQuizData.category,
createdAt: expect.any(String),
updatedAt: expect.any(String),
...testQuizData
Expand Down
Loading