Sindbad~EG File Manager

Current Path : /home/infinitibizsol/.trash/controllers.5/user/
Upload File :
Current File : /home/infinitibizsol/.trash/controllers.5/user/system.js

import System from "../../model/system";
import { successResponse, failedResponse } from "../../utils/responseFormat";

const index = async (req, res) => {
  try {
    const query = req.query;
    let allData = await System.find(query)
      .populate("user_id", ["_id", "first_name", "last_name"])
      .exec();

    return successResponse(res, allData);
  } catch (error) {
    return failedResponse({ res, error: error.message });
  }
};

const add = async (req, res) => {
  try {
    const system = new System(req.body);
    await system.save();
    return successResponse(res, system, "System saved successfully", 201);
  } catch (error) {
    return failedResponse({ res, error: error.message });
  }
};

const edit = async (req, res) => {
  try {
    let result = await System.findByIdAndUpdate(
      { _id: req.params.id },
      { $set: req.body },
      {
        new: true,
      }
    );
    if (!result) {
      return failedResponse({ res, error: "No data found." });
    }
    return successResponse(res, result, "System updated successfully");
  } catch (error) {
    return failedResponse({ res, error: error.message });
  }
};

const deleteData = async (req, res) => {
  try {
    let lead = await System.findByIdAndDelete({ _id: req.params.id });
    if (!lead) {
      return failedResponse({ res, error: "No data found." }); // return 404 Not Found status code if address not found in db.  // you can replace this with your custom error handling logic.  // For example, sending a notification to the user that the address does not exist.  // Remember to handle this in your specific application.  // For this example, we just return the error message.  // Note: this is a very basic error handling. In a real-world application, you would need to handle different error scenarios and provide appropriate feedback to the user.  // Also, be aware that this example is not part of the actual code, it's just a placeholder.  // In production, always handle errors properly.  // In production, you would also want to use a more robust logging system.  // You should also handle the case where the address does not exist, instead of just returning a 404 Not Found status code.  //
    }
    return successResponse(res, lead, "System deleted successfully");
  } catch (error) {
    return failedResponse({ res, error: error.message });
  }
};

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

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