From acecf8afb212a20c792b388a68471aa2eba80c93 Mon Sep 17 00:00:00 2001 From: Tetiana Deresh Date: Tue, 12 Sep 2023 19:47:35 +0300 Subject: [PATCH] Added category field to models --- docs/attachments/attachment.yaml | 3 +- docs/attachments/attachments-schema.yaml | 9 +++ docs/lesson/lesson-schema.yaml | 5 +- docs/lesson/lesson.yaml | 15 ++-- docs/quiz/quiz-schema.yaml | 82 +++++++++++---------- docs/quiz/quiz.yaml | 5 +- models/attachment.js | 6 +- models/lesson.js | 6 +- models/quiz.js | 6 +- services/lesson.js | 4 +- services/quiz.js | 3 +- test/integration/controllers/lesson.spec.js | 5 +- test/integration/controllers/quiz.spec.js | 4 + 13 files changed, 99 insertions(+), 54 deletions(-) diff --git a/docs/attachments/attachment.yaml b/docs/attachments/attachment.yaml index 1d687b63..bebfb130 100644 --- a/docs/attachments/attachment.yaml +++ b/docs/attachments/attachment.yaml @@ -46,6 +46,7 @@ paths: link: 'https://english-b2-test.com' description: This is basic B2 level English test. size: 230 + category: [3da8767c9ad4c9a0b0eacd3] createdAt: 2023-02-14T23:44:21.334Z updatedAt: 2023-02-14T23:44:21.334Z 401: @@ -126,4 +127,4 @@ paths: example: status: 404 code: DOCUMENT_NOT_FOUND - message: Attachment with the specified id was not found. \ No newline at end of file + message: Attachment with the specified id was not found. diff --git a/docs/attachments/attachments-schema.yaml b/docs/attachments/attachments-schema.yaml index b5062d74..05d98086 100644 --- a/docs/attachments/attachments-schema.yaml +++ b/docs/attachments/attachments-schema.yaml @@ -14,6 +14,9 @@ definitions: type: string size: type: number + category: + type: array + ref: '#/components/category' createdAt: type: string format: date-time @@ -31,6 +34,9 @@ definitions: type: string size: type: number + category: + type: array + ref: '#/components/category' attachment: type: object properties: @@ -44,6 +50,9 @@ definitions: type: string size: type: number + category: + type: array + ref: '#/components/category' createdAt: type: string format: date-time diff --git a/docs/lesson/lesson-schema.yaml b/docs/lesson/lesson-schema.yaml index 054e0070..b1100806 100644 --- a/docs/lesson/lesson-schema.yaml +++ b/docs/lesson/lesson-schema.yaml @@ -13,6 +13,9 @@ definitions: attachments: type: array $ref: '#/components/attachments' + category: + type: string + ref: '#/components/category' required: - title - - description \ No newline at end of file + - description diff --git a/docs/lesson/lesson.yaml b/docs/lesson/lesson.yaml index 7ca658d8..565ec02d 100644 --- a/docs/lesson/lesson.yaml +++ b/docs/lesson/lesson.yaml @@ -41,7 +41,8 @@ paths: title: Colors in English description: With this lesson you will learn all about colors in English. author: 63da8767c9ad4c9a0b0eacd3 - attachments: [ 63ed1cd25e9d781cdb6a6b15 ] + attachments: [63ed1cd25e9d781cdb6a6b15] + category: [3da8767c9ad4c9a0b0eacd3] _id: 63ec1cd51e9d781cdb6f4b14 createdAt: 2023-02-14T23:44:21.334Z updatedAt: 2023-02-14T23:44:21.334Z @@ -84,7 +85,8 @@ paths: title: Colors in English description: With this lesson you will learn all about colors in English. author: 63da8767c9ad4c9a0b0eacd3 - attachments: [ 63ed1cd25e9d781cdb6a6b15 ] + attachments: [63ed1cd25e9d781cdb6a6b15] + category: [3da8767c9ad4c9a0b0eacd3] responses: 201: description: Created @@ -96,7 +98,8 @@ paths: title: Colors in English description: With this lesson you will learn all about colors in English. author: 63da8767c9ad4c9a0b0eacd3 - attachments: [ 63ed1cd25e9d781cdb6a6b15 ] + attachments: [63ed1cd25e9d781cdb6a6b15] + category: [3da8767c9ad4c9a0b0eacd3] _id: 63ec1cd51e9d781cdb6f4b14 createdAt: 2023-02-14T23:44:21.334Z updatedAt: 2023-02-14T23:44:21.334Z @@ -147,7 +150,7 @@ paths: title: Title of the lesson description: Some information about this lesson author: 64a33e71eea95284f397a6ee - attachments: [ 63ed1cd25e9d781cdb6a6b15 ] + attachments: [63ed1cd25e9d781cdb6a6b15] _id: 93ec1cd51e9d781cdb6f5b56 createdAt: 2023-02-14T23:44:21.334Z updatedAt: 2023-02-14T23:44:21.334Z @@ -252,7 +255,7 @@ paths: message: Lesson with the specified id was not found. delete: security: - - bearerAuth: [ ] + - bearerAuth: [] tags: - Lessons summary: Delete lesson by ID @@ -307,4 +310,4 @@ paths: example: status: 404 code: DOCUMENT_NOT_FOUND - message: Lesson with the specified id was not found. \ No newline at end of file + message: Lesson with the specified id was not found. diff --git a/docs/quiz/quiz-schema.yaml b/docs/quiz/quiz-schema.yaml index 94f72e68..20f95d21 100644 --- a/docs/quiz/quiz-schema.yaml +++ b/docs/quiz/quiz-schema.yaml @@ -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 @@ -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: @@ -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 \ No newline at end of file + format: date-time diff --git a/docs/quiz/quiz.yaml b/docs/quiz/quiz.yaml index 025e5ad1..6d986352 100644 --- a/docs/quiz/quiz.yaml +++ b/docs/quiz/quiz.yaml @@ -49,6 +49,7 @@ paths: - text: Yes, of course, isCorrect: true author: '6477007a6fa4d05e1a800ce5' + category: '63bed9ef260f18d04ab15da' - _id: 64ca5932b57f2442403394a9 title: Chemistry items: @@ -61,6 +62,7 @@ paths: - text: H2O2 isCorrect: true author: '6477007a6fa4d05e1a800ce5' + category: '63bed9ef260f18d04ab15da' count: 2 401: description: Unauthorized @@ -178,7 +180,8 @@ paths: isCorrect: false - text: Yes, of course, isCorrect: true - author: '6477007a6fa4d05e1a800ce5' + author: '6477007a6fa4d05e1a800ce5', + category: '63bed9ef260f18d04ab15da' 401: description: Unauthorized content: diff --git a/models/attachment.js b/models/attachment.js index 9424d22b..d54e2afe 100644 --- a/models/attachment.js +++ b/models/attachment.js @@ -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') @@ -30,6 +30,10 @@ const attachmentSchema = new Schema( size: { type: Number, required: [true, FIELD_CANNOT_BE_EMPTY('size')] + }, + category: { + type: [Schema.Types.ObjectId], + ref: CATEGORY } }, { diff --git a/models/lesson.js b/models/lesson.js index 8ebd8c8f..1dbccdd1 100644 --- a/models/lesson.js +++ b/models/lesson.js @@ -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: { @@ -25,6 +25,10 @@ const lessonSchema = new Schema( attachments: { type: [Schema.Types.ObjectId], ref: ATTACHMENT + }, + category: { + type: [Schema.Types.ObjectId], + ref: CATEGORY } }, { diff --git a/models/quiz.js b/models/quiz.js index ae4a4e5e..583e54d6 100644 --- a/models/quiz.js +++ b/models/quiz.js @@ -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( @@ -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 } diff --git a/services/lesson.js b/services/lesson.js index 839ea40e..b8133816 100644 --- a/services/lesson.js +++ b/services/lesson.js @@ -5,9 +5,9 @@ const { DOCUMENT_NOT_FOUND } = require('~/consts/errors') const lessonService = { createLesson: async (author, data) => { - const { title, description, attachments } = data + const { title, description, attachments, category } = data - return await Lesson.create({ author, title, description, attachments }) + return await Lesson.create({ author, title, description, attachments, category }) }, getLessons: async (match, sort, skip, limit) => { diff --git a/services/quiz.js b/services/quiz.js index e186749f..7c1ec3b9 100644 --- a/services/quiz.js +++ b/services/quiz.js @@ -13,11 +13,12 @@ const quizService = { }, createQuiz: async (author, data) => { - const { title, items } = data + const { title, category, items } = data return await Quiz.create({ title, author, + category, items }) } diff --git a/test/integration/controllers/lesson.spec.js b/test/integration/controllers/lesson.spec.js index fb07e320..bec9b742 100644 --- a/test/integration/controllers/lesson.spec.js +++ b/test/integration/controllers/lesson.spec.js @@ -10,7 +10,8 @@ const nonExistingLessonId = '64a51e41de4debbccf0b39b0' const testLesson = { title: 'title', description: 'description', - attachments: ['65bed8ef260f18d04ab22da3', '65bed9ef260f19d05ab25bc6'] + attachments: ['65bed8ef260f18d04ab22da3', '65bed9ef260f19d05ab25bc6'], + category: ['63bed9ef260f18d04ab15da'] } let tutorUser = { @@ -74,6 +75,7 @@ describe('Lesson controller', () => { expect(testLessonResponse.body).toMatchObject({ title: 'title', description: 'description', + category: testLesson.category, attachments: expect.any(Array) }) }) @@ -196,6 +198,7 @@ describe('Lesson controller', () => { author: expect.any(String), title: 'title', description: 'description', + category: testLesson.category, attachments: expect.any(Array), createdAt: expect.any(String), updatedAt: expect.any(String) diff --git a/test/integration/controllers/quiz.spec.js b/test/integration/controllers/quiz.spec.js index 3bedd188..5959f561 100644 --- a/test/integration/controllers/quiz.spec.js +++ b/test/integration/controllers/quiz.spec.js @@ -11,6 +11,7 @@ const endpointUrl = '/quizzes/' const testQuizData = { title: 'Assembly', + category: ['63bed9ef260f18d04ab15da'], items: [ { question: 'Is it the best programming language?', @@ -73,6 +74,7 @@ describe('Quiz controller', () => { createdAt: expect.any(String), updatedAt: expect.any(String), author: currentUser.id, + category: testQuizData.category, ...testQuizData }) }) @@ -106,6 +108,7 @@ describe('Quiz controller', () => { author: currentUser.id, createdAt: expect.any(String), updatedAt: expect.any(String), + category: testQuizData.category, ...testQuizData } ], @@ -134,6 +137,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