{"size": 1251, "ext": "js", "max_stars_repo_path": "server/controllers/tops.js", "max_stars_repo_name": "Baleksas/GAMESPOT", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/controllers/tops.js", "max_issues_repo_name": "Baleksas/GAMESPOT", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/controllers/tops.js", "max_forks_repo_name": "Baleksas/GAMESPOT", "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": 30.512195122, "max_line_length": 68, "alphanum_fraction": 0.6482813749}
import mongoose from "mongoose";
import TopMessage from "../models/topMessage.js";
export const getTops = async (req, res) => {
  try {
    const topMessages = await TopMessage.find();
    res.status(200).json(topMessages);
  } catch (error) {
    res.status(404).json({ message: error.message });
  }
};
export const createTop = async (req, res) => {
  const { player, maxScore, game } = req.body;
  const newTop = new TopMessage({ player, maxScore, game });
  try {
    await newTop.save();
    res.status(201).json(newTop);
  } catch (error) {
    res.status(409).json({ message: error.message });
  }
};
export const updateTop = async (req, res) => {
  const { id } = req.params;
  const { game, player, maxScore } = req.body;

  if (!mongoose.Types.ObjectId.isValid(id))
    return res.status(404).send(`No post with: ${id}`);

  const updatedTop = { game, player, maxScore, _id: id };
  await TopMessage.findByIdAndUpdate(id, updatedTop, { new: true });
  res.json(updatedTop);
};
export const deleteTop = async (req, res) => {
  const { id } = req.params;

  if (!mongoose.Types.ObjectId.isValid(id))
    return res.status(404).send(`No post with: ${id}`);

  await TopMessage.findByIdAndDelete(id);
  res.json({ message: "Top deleted." });
};
