Sindbad~EG File Manager

Current Path : /home/infinitibizsol/.trash/controllers.8/common/
Upload File :
Current File : /home/infinitibizsol/.trash/controllers.8/common/notes.js

import db from "../../model/index";
import AppError from "../../utils/apiError";
import { successResponse } from "../../utils/responseFormat";
import catchAsync from "../../utils/catchAsync";

const index = catchAsync(async (req, res, next) => {
  const query = req.query;
  let allData = await db.Note.find()
    .populate("created_by", ["_id", "first_name"])
    .exec();

  const result = allData.filter((item) => item.created_by !== null);

  return successResponse(res, result);
});

const add = catchAsync(async (req, res, next) => {
  const notes = new db.Note(req.body);
  await notes.save();
  return successResponse(res, notes, " Note saved successfully");
});

const view = catchAsync(async (req, res, next) => {
  let notes = await db.Note.findOne({ _id: req.params.id });
  if (!notes) {
    return next(new AppError("No data found.", 404));
  }
  return successResponse(res, notes);
});

const edit = catchAsync(async (req, res, next) => {
  let result = await db.Note.findByIdAndUpdate(
    { _id: req.params.id },
    { $set: req.body },
    {
      new: true,
    }
  );
  if (!result) {
    return next(new AppError("No data found.", 404));
  }
  return successResponse(res, result, "Note updated successfully");
});

const deleteData = catchAsync(async (req, res, next) => {
  let lead = await db.Note.deleteOne({ _id: req.params.id });
  if (!lead) {
    return next(new AppError("No data found.", 404));
  }
  return successResponse(res, lead, "Note deleted successfully");
});

export default { index, add, view, edit, deleteData };

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists