{"size": 1590, "ext": "js", "max_stars_repo_path": "controllersAdmin/adminNewsController.js", "max_stars_repo_name": "Azizxon9909/Robo-app-backend", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "controllersAdmin/adminNewsController.js", "max_issues_repo_name": "Azizxon9909/Robo-app-backend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "controllersAdmin/adminNewsController.js", "max_forks_repo_name": "Azizxon9909/Robo-app-backend", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.9090909091, "max_line_length": 67, "alphanum_fraction": 0.641509434}
const { News } = require("../model/News");
const { schema } = require("../validators/newsValidator");

exports.getNews = async (req, res) => {
  const news = await News.find().sort({ dateNews: -1 });
  res.status(200).send(news);
};
exports.getNew = async (req, res) => {
  const news = await News.findById(req.params.id);
  if (!news)
    return res
      .status(404)
      .send("Berilgan IDga teng bo'lgan yangilik topilmadi.");
  res.status(200).send(news);
};

exports.createNews = async (req, res) => {
  const { error } = schema.validate(req.body);
  if (error) return res.status(400).send(error.details[0].message);
  let news = new News({
    title: req.body.title,
    about: req.body.about,
    photo: req.file.path,
  });
  news = await news.save();

  res.status(201).send(news);
};
exports.updateNews = async (req, res) => {
  const { error } = schema.validate(req.body);
  if (error) return res.status(400).send(error.details[0].message);
  let news = await News.findByIdAndUpdate(req.params.id, {
    title: req.body.title,
    about: req.body.about,
    photo: req.file.path,
  });
  if (!news)
    return res
      .status(404)
      .send("Berilgan IDga teng bo'lgan yangilik topilmadi.");
  news = await news.save();
  res.status(200).send(news);
  res.status(500).send(err.message);
};

exports.deleteNews = async (req, res) => {
  let news = await News.findByIdAndRemove(req.params.id);
  if (!news)
    return res
      .status(404)
      .send("Berilgan IDga teng bo'lgan yangilik topilmadi.");
  res.status(200).send(news);
  res.status(500).send(err.message);
};
