{"size": 1034, "ext": "js", "max_stars_repo_path": "controllers/image.controller.js", "max_stars_repo_name": "mizzerx/G2-Web-Project", "max_stars_repo_stars_event_min_datetime": "2021-04-08T06:54:49.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-16T10:00:27.000Z", "max_issues_repo_path": "controllers/image.controller.js", "max_issues_repo_name": "mizzerx/G2-Web-Project", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "controllers/image.controller.js", "max_forks_repo_name": "mizzerx/G2-Web-Project", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.85, "max_line_length": 60, "alphanum_fraction": 0.6025145068}
const User = require('../models/user.model');
const Image = require('../models/image.model');
const { uploadImagesFile } = require('./upload.controller');

const uploadImages = async (req, res, next) => {
  const { _id } = req.session.passport.user;
  const images = await uploadImagesFile(req.files);

  if (images && images.length > 0) {
    images.map(async (img) => {
      const image = await new Image(img).save();
      console.log(img);
      await User.findOneAndUpdate(
        { _id },
        { $push: { uploadedImages: image._id } },
        { new: true }
      );
    });
  }

  return res.redirect('/student/profile');
};

const deleteImages = async (req, res, next) => {
  const { id } = req.query;
  const { _id } = req.session.passport.user;

  await User.findOneAndUpdate(
    { _id },
    { $pull: { uploadedImages: { $in: id } } },
    { new: true }
  );

  await Image.findOneAndRemove({ _id: id }, { new: true });

  return res.redirect('/student/profile');
};

module.exports = { uploadImages, deleteImages };
