Sindbad~EG File Manager

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

import db from "../../model/index";
import { successResponse } 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.Goal.find({ user_id: req.user._id });
  return successResponse(res, allData);
});

const add = catchAsync(async (req, res, next) => {
  if (!req.body.user_id) {
    req.body.user_id = req.user._id;
  }
  const goals = new db.Goal(req.body);
  await goals.save();
  return successResponse(res, goals, "Goals saved successfully", 201);
});

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

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

const deleteData = catchAsync(async (req, res, next) => {
  const goal_id = req.params.id;
  // Delete the contact itself
  const deletedGoal = await db.Goal.findByIdAndDelete({ _id: goal_id });

  if (!deletedGoal) {
    return next(new AppError("No data found.", 404));
  }

  return successResponse(
    res,
    deletedGoal,
    "Goal and related data deleted successfully."
  );
});

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

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