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

Supplemented db with banner field for course // Not relevant #626

Closed
Closed
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
8 changes: 7 additions & 1 deletion docs/course/course-schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ definitions:
properties:
_id:
type: string
banner:
type: string
title:
type: string
description:
Expand Down Expand Up @@ -34,6 +36,8 @@ definitions:
properties:
_id:
type: string
banner:
type: string
title:
type: string
description:
Expand All @@ -60,6 +64,8 @@ definitions:
courseBody:
type: object
properties:
banner:
type: string
title:
type: string
description:
Expand All @@ -79,4 +85,4 @@ definitions:
ref: '#/components/attachments'
required:
- title
- description
- description
8 changes: 6 additions & 2 deletions docs/course/course.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ paths:
schema:
$ref: '#/definitions/course'
example:
- _id: 64045f98b131dd04d7896af6
- _id: 64045f98b131dd04d7896af6,
banner: 247829-banner.jpg
title: Advanced English
description: Advanced english course with 5 modules and separated lessons for improving speaking, listening and reading.
author: 63da8767c9ad4c9a0b0eacd3
Expand Down Expand Up @@ -80,6 +81,7 @@ paths:
schema:
$ref: '#definitions/courseBody'
example:
banner: 247829-banner.jpg
title: Advanced English
description: Advanced english course with 5 modules and separated lessons for improving speaking, listening and reading.
author: 63da8767c9ad4c9a0b0eacd3
Expand All @@ -93,6 +95,7 @@ paths:
schema:
$ref: '#definitions/course'
example:
banner: 247829-banner.jpg
title: Advanced English
description: Advanced english course with 5 modules and separated lessons for improving speaking, listening and reading.
author: 63da8767c9ad4c9a0b0eacd3
Expand Down Expand Up @@ -206,6 +209,7 @@ paths:
schema:
$ref: '#definitions/course'
example:
banner: 247829-banner.jpg
title: Advanced English
description: Advanced english course with 5 modules and separated lessons for improving speaking, listening and reading.
author: 63da8767c9ad4c9a0b0eacd3
Expand Down Expand Up @@ -281,4 +285,4 @@ paths:
example:
status: 404
code: DOCUMENT_NOT_FOUND
message: Course with the specified id was not found.
message: Course with the specified id was not found.
83 changes: 43 additions & 40 deletions models/course.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
const { Schema, model } = require('mongoose')

const { FIELD_CANNOT_BE_EMPTY, FIELD_CANNOT_BE_SHORTER, FIELD_CANNOT_BE_LONGER } = require('~/consts/errors')
const { COURSE, USER, LESSON, ATTACHMENT } = require('~/consts/models')

const courseSchema = new Schema(
{
title: {
type: String,
required: [true, FIELD_CANNOT_BE_EMPTY('title')],
minLength: [1, FIELD_CANNOT_BE_SHORTER('title', 1)],
maxLength: [100, FIELD_CANNOT_BE_LONGER('title', 100)]
},
description: {
type: String,
required: [true, FIELD_CANNOT_BE_EMPTY('description')],
minLength: [1, FIELD_CANNOT_BE_SHORTER('description', 1)],
maxLength: [1000, FIELD_CANNOT_BE_LONGER('description', 1000)]
},
author: {
type: Schema.Types.ObjectId,
ref: USER,
required: [true, FIELD_CANNOT_BE_EMPTY('author')]
},
lessons: {
type: [Schema.Types.ObjectId],
ref: LESSON
},
attachments: {
type: [Schema.Types.ObjectId],
ref: ATTACHMENT
}
},
{
versionKey: false,
timestamps: true
}
)

module.exports = model(COURSE, courseSchema)
const { Schema, model } = require('mongoose')

const { FIELD_CANNOT_BE_EMPTY, FIELD_CANNOT_BE_SHORTER, FIELD_CANNOT_BE_LONGER } = require('~/consts/errors')
const { COURSE, USER, LESSON, ATTACHMENT } = require('~/consts/models')

const courseSchema = new Schema(
{
banner: {
type: String
},
title: {
type: String,
required: [true, FIELD_CANNOT_BE_EMPTY('title')],
minLength: [1, FIELD_CANNOT_BE_SHORTER('title', 1)],
maxLength: [100, FIELD_CANNOT_BE_LONGER('title', 100)]
},
description: {
type: String,
required: [true, FIELD_CANNOT_BE_EMPTY('description')],
minLength: [1, FIELD_CANNOT_BE_SHORTER('description', 1)],
maxLength: [1000, FIELD_CANNOT_BE_LONGER('description', 1000)]
},
author: {
type: Schema.Types.ObjectId,
ref: USER,
required: [true, FIELD_CANNOT_BE_EMPTY('author')]
},
lessons: {
type: [Schema.Types.ObjectId],
ref: LESSON
},
attachments: {
type: [Schema.Types.ObjectId],
ref: ATTACHMENT
}
},
{
versionKey: false,
timestamps: true
}
)

module.exports = model(COURSE, courseSchema)
155 changes: 78 additions & 77 deletions services/course.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,78 @@
const Course = require('~/models/course')

const { DOCUMENT_NOT_FOUND } = require('~/consts/errors')
const { createError, createForbiddenError } = require('~/utils/errorsHelper')

const courseService = {
getCourses: async ({ author, skip, limit }) => {
const items = await Course.find({ author }).skip(skip).limit(limit).sort({ updatedAt: -1 }).lean().exec()
const count = await Course.countDocuments({ author })

return { items, count }
},

getCourseById: async (id) => {
return await Course.findById(id).lean().exec()
},

createCourse: async (author, data) => {
const { title, description, lessons, attachments } = data

return await Course.create({
title,
description,
author,
lessons,
attachments
})
},

updateCourse: async (userId, data) => {
const { id, title, description, attachments, rewriteAttachments = false } = data

const course = await Course.findById(id).exec()

if (!course) {
throw createError(DOCUMENT_NOT_FOUND(Course.modelName))
}

const courseAuthor = course.author.toString()

if (userId !== courseAuthor) {
throw createForbiddenError()
}

if (attachments) {
course.attachments = rewriteAttachments ? attachments : course.attachments.concat(attachments)
}

const updateData = { title, description }

for (const key in updateData) {
const value = updateData[key]
if (value) course[key] = value
}

await course.validate()
await course.save()
},

deleteCourse: async (id, currentUser) => {
const course = await Course.findById(id).exec()

if (!course) {
throw createError(DOCUMENT_NOT_FOUND(Course.modelName))
}

const author = course.author.toString()

if (author !== currentUser) {
throw createForbiddenError()
}

await Course.findByIdAndRemove(id).exec()
}
}

module.exports = courseService
const Course = require('~/models/course')

const { DOCUMENT_NOT_FOUND } = require('~/consts/errors')
const { createError, createForbiddenError } = require('~/utils/errorsHelper')

const courseService = {
getCourses: async ({ author, skip, limit }) => {
const items = await Course.find({ author }).skip(skip).limit(limit).sort({ updatedAt: -1 }).lean().exec()
const count = await Course.countDocuments({ author })

return { items, count }
},

getCourseById: async (id) => {
return await Course.findById(id).lean().exec()
},

createCourse: async (author, data) => {
const { banner, title, description, lessons, attachments } = data

return await Course.create({
banner,
title,
description,
author,
lessons,
attachments
})
},

updateCourse: async (userId, data) => {
const { id, banner, title, description, attachments, rewriteAttachments = false } = data

const course = await Course.findById(id).exec()

if (!course) {
throw createError(DOCUMENT_NOT_FOUND(Course.modelName))
}

const courseAuthor = course.author.toString()

if (userId !== courseAuthor) {
throw createForbiddenError()
}

if (attachments) {
course.attachments = rewriteAttachments ? attachments : course.attachments.concat(attachments)
}

const updateData = { banner, title, description }

for (const key in updateData) {
const value = updateData[key]
if (value) course[key] = value
}

await course.validate()
await course.save()
},

deleteCourse: async (id, currentUser) => {
const course = await Course.findById(id).exec()

if (!course) {
throw createError(DOCUMENT_NOT_FOUND(Course.modelName))
}

const author = course.author.toString()

if (author !== currentUser) {
throw createForbiddenError()
}

await Course.findByIdAndRemove(id).exec()
}
}

module.exports = courseService
Loading
Loading