32 lines
808 B
JavaScript
32 lines
808 B
JavaScript
const Category = require('./categoryModel')
|
|
const AppError = require('../../globalErrorHandler/AppError')
|
|
|
|
module.exports.getAllCategories = async () => {
|
|
return Category.find().populate('parentId')
|
|
}
|
|
|
|
module.exports.createCategory = async (data) => {
|
|
const category = Category.create(data, null)
|
|
return category
|
|
}
|
|
|
|
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
|
|
}
|