const serviceModel = require("../models/service");

const validateService = async (req) => {
    const { name } = req.body;
    const exist = await serviceModel.findOne({ name: { $regex: name, $options: "i" } });
    if(exist){
        return true
    } else {
        return false
    } 
};

const addService = async (req) => {
    const newService = new serviceModel(req.body);
    const result = await newService.save();
    return result
};

const getAllServices = async (req) => {
    const services = await serviceModel.find();
    return services
};

const updateService = async (req) => {
    const { serviceId, name } = req.body;
    const service = await serviceModel.findByIdAndUpdate(serviceId,
        { $set: {name: name } },
        { new: true }
    );
    return service
};

const deleteService = async (req) => {
    const { serviceId } = req.body;
    const service = await serviceModel.findByIdAndDelete(serviceId);
    return service
};

module.exports = {
    validateService,
    addService,
    getAllServices,
    updateService,
    deleteService
};