Skip to content

Commit

Permalink
Adding util for categorization (#627)
Browse files Browse the repository at this point in the history
* adding util for categorization

* refactor code
  • Loading branch information
Mav-Ivan authored Nov 14, 2023
1 parent b2612cc commit 8407875
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
4 changes: 3 additions & 1 deletion controllers/attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ const attachmentService = require('~/services/attachment')
const getMatchOptions = require('~/utils/getMatchOptions')
const getSortOptions = require('~/utils/getSortOptions')
const getRegex = require('~/utils/getRegex')
const getCategoriesOptions = require('~/utils/getCategoriesOption')

const getAttachments = async (req, res) => {
const { id: author } = req.user
const { fileName, sort, skip, limit, categories } = req.query
const categoriesOptions = getCategoriesOptions(categories)

const match = getMatchOptions({
author,
fileName: getRegex(fileName),
...(categories?.length && { category: categories })
category: categoriesOptions
})
const sortOptions = getSortOptions(sort)

Expand Down
4 changes: 3 additions & 1 deletion controllers/lesson.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const lessonService = require('~/services/lesson')
const getCategoriesOptions = require('~/utils/getCategoriesOption')
const getMatchOptions = require('~/utils/getMatchOptions')
const getRegex = require('~/utils/getRegex')
const getSortOptions = require('~/utils/getSortOptions')
Expand All @@ -23,8 +24,9 @@ const getLessonById = async (req, res) => {
const getLessons = async (req, res) => {
const { id: author } = req.user
const { title, sort, skip, limit, categories } = req.query
const categoriesOptions = getCategoriesOptions(categories)

const match = getMatchOptions({ author, title: getRegex(title), ...(categories?.length && { category: categories }) })
const match = getMatchOptions({ author, title: getRegex(title), category: categoriesOptions })
const sortOptions = getSortOptions(sort)

const lessons = await lessonService.getLessons(match, sortOptions, parseInt(skip), parseInt(limit))
Expand Down
9 changes: 6 additions & 3 deletions controllers/question.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
const questionService = require('~/services/question')
const getCategoriesOptions = require('~/utils/getCategoriesOption')
const getMatchOptions = require('~/utils/getMatchOptions')
const getRegex = require('~/utils/getRegex')
const getSortOptions = require('~/utils/getSortOptions')

const getQuestions = async (req, res) => {
const { id: author } = req.user
const { title, sort, skip, limit, categories } = req.query
const categoriesOptions = getCategoriesOptions(categories)

const match = {
const match = getMatchOptions({
author,
title: getRegex(title),
...(categories?.length && { category: categories })
}
category: categoriesOptions
})
const sortOptions = getSortOptions(sort)

const questions = await questionService.getQuestions(match, sortOptions, parseInt(skip), parseInt(limit))
Expand Down
5 changes: 4 additions & 1 deletion controllers/quiz.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const quizService = require('~/services/quiz')
const getCategoriesOptions = require('~/utils/getCategoriesOption')
const getMatchOptions = require('~/utils/getMatchOptions')
const getRegex = require('~/utils/getRegex')
const getSortOptions = require('~/utils/getSortOptions')

const getQuizzes = async (req, res) => {
const { id: author } = req.user
const { title, sort, skip, limit, categories } = req.query
const categoriesOptions = getCategoriesOptions(categories)

const match = { author, title: getRegex(title), ...(categories?.length && { category: categories }) }
const match = getMatchOptions({ author, title: getRegex(title), category: categoriesOptions })
const sortOptions = getSortOptions(sort)

const quizzes = await quizService.getQuiz(match, sortOptions, parseInt(skip), parseInt(limit))
Expand Down
8 changes: 8 additions & 0 deletions utils/getCategoriesOption.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const getCategoriesOptions = (categories) => {
if (categories) {
return categories.map((item) => (item === 'null' ? null : item))
} else {
return
}
}
module.exports = getCategoriesOptions

0 comments on commit 8407875

Please sign in to comment.