{"size": 999, "ext": "js", "max_stars_repo_path": "application/controllers/image/getSingleImage.js", "max_stars_repo_name": "DastanDev/images-half-work", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "application/controllers/image/getSingleImage.js", "max_issues_repo_name": "DastanDev/images-half-work", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "application/controllers/image/getSingleImage.js", "max_forks_repo_name": "DastanDev/images-half-work", "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": 27.75, "max_line_length": 160, "alphanum_fraction": 0.5305305305}
const db = require('../../helpers/db/connectDb')
const colors = require('colors')

const getSingleImage = async (req, res) => {
  try {
    const { id } = req.params
    db.query(`SELECT * FROM Image WHERE id='${id}'`, (err, result) => {
      if (err) {
        console.log(colors.red(err.message))
        res.redirect('/')
        return
      }
      db.query(
        `SELECT Comment.content, Comment.author, User.username FROM Comment JOIN User ON User.id = Comment.author WHERE image='${id} ORDER BY Comment.id DESC'`,
        (err, comments) => {
          if (err) {
            console.log(colors.red(err.message))
            res.redirect('/')
            return
          }
          // if no error
          res.render('imageDetails', {
            image: result[0],
            comments: comments.reverse(),
            user: req.user
          })
        }
      )
    })
  } catch (error) {
    res.status(400).json({ message: error.message })
  }
}

module.exports = getSingleImage
