Files
mbmanager-api/components/category/categoryService.js
T
2026-08-19 05:50:14 +03:30

50 lines
1.3 KiB
JavaScript

const Category = require('./categoryModel')
const AppError = require('../../globalErrorHandler/AppError')
module.exports.getAllCategories = async (query = {}) => {
const mongoQuery = {}
const search = (query.search || query.query || '').trim()
if (search) {
mongoQuery.$or = [
{ nativeName: { $regex: search, $options: 'i' } },
{ englishName: { $regex: search, $options: 'i' } },
]
}
return Category.find(mongoQuery).populate('features')
}
module.exports.createCategory = async (data) => {
return Category.create(data, null)
}
module.exports.getCategory = async (categoryId) => {
const category = await Category.findById(categoryId)
if (!category) {
throw new AppError('NoCategoryFound', 404, {})
}
return category
}
module.exports.updateCategory = async (categoryId, data) => {
const category = await Category.findOneAndUpdate({ _id: categoryId }, data, { new: true })
if (!category) {
throw new AppError('NoCategoryFound', 404, {})
}
return category
}
module.exports.deleteCategory = async (categoryId) => {
const category = await Category.findByIdAndDelete(categoryId)
if (!category) {
throw new AppError('NoCategoryFound', 404, {})
}
return null
}