const categoryModel = require("../models/categoryModel");

const addCategory = async (req, session) => {
    const newCategory = new categoryModel(req.body);
    const result = await newCategory.save({ session });
    return result;  
};

const getCategory = async (req) => {
    const { categoryId } = req.query;
    const category = await categoryModel.findById({_id: categoryId});
    return category;
};

const getAllCategoriesByAdmin = async (req) => {
    const { adminId } = req.query;
    const result = await categoryModel.find({adminId: adminId});
    return result 
};

const getAllCategories = async (req) => {
    const result = await categoryModel.find();
    return result;
};

const deleteCategory = async (req) => {
    const { categoryId } = req.query;
    const result = await categoryModel.findByIdAndDelete({_id: categoryId});
    return result
};

module.exports = {
    addCategory,
    getCategory,
    getAllCategoriesByAdmin,
    getAllCategories,
    deleteCategory
};