Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
3.2 kB
5
Indexable
Never
const formPrestasiModel = require("../models/formprestasimodel.js");

// get all data prestasi
exports.showDataPrestasi = (req, res) => {
  try {
    formPrestasiModel.getDataPrestasi((err, results) => {
      if (err) {
        res.send(err);
      } else {
        res.json(results);
      }
    });
  } catch (error) {
    res.status(500).send(error);
  }
};

exports.showDataFotoSiswa = (req, res) => {
  try {
    formPrestasiModel.getDataFotoSiswa((err, results) => {
      if (err) {
        res.status(400).send(err);
      } else {
        res.json(results);
      }
    });
  } catch (error) {
    res.status(error).send(error);
  }
};

exports.showDataKK = (req, res) => {
  try {
    formPrestasiModel.getDataKK((err, results) => {
      if (err) {
        res.status(400).send(err);
      } else {
        res.json(results);
      }
    });
  } catch (error) {
    res.status(500).send(error);
  }
};

exports.showDataNISN = (req, res) => {
  try {
    formPrestasiModel.getDataNISN((err, results) => {
      if (err) {
        res.status(400).send(err);
      } else {
        res.json(results);
      }
    });
  } catch (error) {
    res.status(500).send(error);
  }
};

// get single data id
exports.showIdPrestasi = (req, res) => {
  try {
    formPrestasiModel.getDataIdPrestasi(req.params.id, (err, results) => {
      if (err) {
        res.status(400).send(err);
      } else {
        res.json(results);
      }
    });
  } catch (error) {
    res.status(500).send(error);
  }
};

exports.showEmailPrestasi = (req, res) => {
  try {
    formPrestasiModel.getDataEmailPrestasi(req.user.user_email, (err, results) => {
      if (err) {
        res.status(400).send(err);
      } else {
        res.json(results);
      }
    });
  } catch (error) {
    res.status(500).send(error);
  }
};

const filePath = (obj) => {
  return `/${obj.fieldname}/${obj.filename}`;
};

exports.createDataPrestasi = (req, res) => {
  try {
    const formValue = req.body;
    const filesValue = req.files;
    const unggah_nisn = filePath(filesValue["unggah_nisn"][0]);

    const unggah_kk = filePath(filesValue["unggah_kk"][0]);
    const foto_siswa = filePath(filesValue["foto_siswa"][0]);

    const dataPrestasi = { ...formValue, unggah_nisn, unggah_kk, foto_siswa };
    formPrestasiModel.insertDataPrestasi(dataPrestasi, (err, results) => {
      if (err) {
        res.status(400).send(err);
      } else {
        res.json(results);
      }
    });
  } catch (error) {
    res.status(500).send(error);
  }
};

exports.updateForm = (req, res) => {
  try {
    const data = req.body;
    const email = req.params.user_email;
    formPrestasiModel.updateFormByEmail(data, email, (err, results) => {
      if (err) {
        res.status(400).send(err);
      } else {
        res.json(results);
      }
    });
  } catch (error) {
    res.status(500).send(error);
  }
};

exports.deleteForm = (req, res) => {
  const id = req.params.data_id;
  formPrestasiModel.deleteFormById(id, (err, results) => {
    if (err) {
      res.send(err);
    } else {
      res.json({ ...results });
    }
  });
};
Leave a Comment