{"size": 1078, "ext": "js", "max_stars_repo_path": "controllers/repliesController.js", "max_stars_repo_name": "TahaSh/vue-forum-app-backend", "max_stars_repo_stars_event_min_datetime": "2019-12-11T18:00:10.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-04T19:35:05.000Z", "max_issues_repo_path": "controllers/repliesController.js", "max_issues_repo_name": "TahaSh/vue-forum-app-backend", "max_issues_repo_issues_event_min_datetime": "2019-12-11T18:25:39.000Z", "max_issues_repo_issues_event_max_datetime": "2020-07-19T01:05:19.000Z", "max_forks_repo_path": "controllers/repliesController.js", "max_forks_repo_name": "TahaSh/vue-forum-app-backend", "max_forks_repo_forks_event_min_datetime": "2019-12-11T17:57:12.000Z", "max_forks_repo_forks_event_max_datetime": "2020-10-04T00:27:16.000Z"}
{"max_stars_count": 55, "max_issues_count": 2, "max_forks_count": 9, "avg_line_length": 25.6666666667, "max_line_length": 59, "alphanum_fraction": 0.6614100186}
const mongoose = require('mongoose')
const Reply = mongoose.model('Reply')
const Topic = mongoose.model('Topic')
const boom = require('boom')

exports.createReply = async (req, res) => {
  const topic = (await Topic.findById(req.params.topicId))
  if (!topic) {
    throw boom.notFound('Topic not found')
  }
  req.body.topic = req.params.topicId
  req.body.user = req.user._id
  let reply = await (new Reply(req.body).save())
  reply = await Reply.populate(reply, 'user')
  res.json(reply)
}

exports.updateReply = async (req, res) => {
  const topic = (await Topic.findById(req.params.topicId))
  if (!topic) {
    throw boom.notFound('Topic not found')
  }
  const reply = await Reply.findOneAndUpdate(
    { _id: req.params.replyId },
    req.body,
    {
      new: true,
      runValidators: true
    }
  )
  res.json(reply)
}

exports.deleteReply = async (req, res) => {
  const topic = (await Topic.findById(req.params.topicId))
  if (!topic) {
    throw boom.notFound('Topic not found')
  }
  await Reply.findOneAndDelete({ _id: req.params.replyId })
  res.send(true)
}
