HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux vmi1674223.contaboserver.net 5.4.0-182-generic #202-Ubuntu SMP Fri Apr 26 12:29:36 UTC 2024 x86_64
User: root (0)
PHP: 7.4.3-4ubuntu2.22
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //home/evaluation-leave/controllers/emailContoller.js
const Service = require("../models/service");

module.exports = {
  services: async (req, res) => {
    try {
      const result = await Service.findSerrvices();
      if (!result) {
        return res.status(404).json({ message: "Sorry no Services setup." });
      }

      console.log(`Service, successfully pulled on ${new Date()} `);

      return res.status(200).json(result);
    } catch (error) {
      console.error("Error occurred:", error);
      res.status(500).json({ error: true, message: "Internal Server Error" });
    }
  },

  serviceById: async (req, res) => {
    const id = req.params.id;
    try {
      const result = await Service.findServiceById(id);
      if (!result) {
        return res
          .status(404)
          .json({ message: "Sorry thet service does not exist." });
      }

      console.log(
        `Service with id ${id}, successfully pulled on ${new Date()} ` 
      );

      return res.status(200).json(result);
    } catch (error) {
      console.error("Error occurred:", error);
      res.status(500).json({ error: true, message: "Internal Server Error" });
    }
  },

 createService: async (req, res) => {
    const {
      name,
      detailed_name,
      tag,
      cost,
      description,
      image_url
         } = req.body;

    try {
      const serv = await Service.findServiceByName(name);
      if (serv) {
        return res.status(400).json({
          message:
            "Sorry there is an existing servce with that name.Use a different name",
        });
      }
      
      const newService = {
       name,
      detailed_name,
      tag,
      cost,
      description,
      image_url

      };

      const result = await Service.createService(newService);

      console.log(`successfully Created an anew servive on ${new Date()}`);
      return res.status(201).json(result);
    } catch (error) {
      console.error("Error occurred:", error);
      res.status(500).json({ error: true, message: "Internal Server Error" });
    }
  },

 updateAvailableService: async (req, res) => {
    const {
      id,
      name,
      detailed_name,
      tag,
      cost,
      description,
      image_url
         } = req.body;

    try {
      const serv = await Service.findServiceById(id);
      if (!serv) {
        return res.status(400).json({
          message:
            "Sorry there seems to be a problem. Try again latter",
        });
      }
      
      const updatedService = {
      name,
      detailed_name,
      tag,
      cost,
      description,
      image_url,
      updated_at: new Date(),
      };

      const result = await Service.updateService(updatedService);

      console.log(`successfully Updated service ${updatedService.name} at ${new Date()}`);
      return res.status(201).json(result);
    } catch (error) {
      console.error("Error occurred:", error);
      res.status(500).json({ error: true, message: "Internal Server Error" });
    }
  },

};