Sindbad~EG File Manager
import mongoose from "mongoose";
import db from "../../model/index";
import { successResponse, failedResponse } from "../../utils/responseFormat";
import catchAsync from "../../utils/catchAsync";
import AppError from "../../utils/apiError";
const index = catchAsync(async (req, res, next) => {
const query = req.query;
let allData = await db.Employee.find(query)
.populate("contact_id", ["_id", "first_name"])
.exec();
return successResponse(res, allData);
});
const add = catchAsync(async (req, res, next) => {
const employee = req.body;
const result = new db.Employee(employee);
await result.save();
return successResponse(res, result, "Employee saved successfully.");
});
const edit = catchAsync(async (req, res, next) => {
let result = await db.Employee.findByIdAndUpdate(
{ _id: req.params.id },
{ $set: req.body },
{
new: true,
}
);
if (!result) {
return next(new AppError("No data found.", 404));
}
return successResponse(res, result, "Employee updated successfully");
});
const view = catchAsync(async (req, res, next) => {
let Contacts = await db.Employee.aggregate([
{
$match: {
_id: mongoose.Types.ObjectId(req.params.id),
},
},
]);
let contact = await db.Employee.findByIdAndUpdate({ _id: req.params.id });
if (!contact) {
return next(new AppError("No data found.", 404));
}
return successResponse(res, Contacts);
});
const deleteData = catchAsync(async (req, res, next) => {
const contactId = req.params.id;
// Delete the contact itself
const deletedEmployer = await db.Employee.findByIdAndDelete({
_id: contactId,
});
if (!deletedEmployer) {
return next(new AppError("No data found.", 404));
}
return successResponse(
res,
deletedEmployer,
"Employee and related data deleted successfully."
);
});
export default { index, add, edit, view, deleteData };
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists