text: const express = require('express');
  const router = express.Router();
  const db = require('../../db/connection');
  const inputCheck = require('../../utils/inputCheck');
  
  // Get all candidates
  router.get('/candidate', (req, res) => {
      const sql = `SELECT candidates.*, parties.name
                   AS party_name
                   FROM candidates
                   LEFT JOIN parties
                   ON candidates.party_id = parties.id`;
  
      db.query(sql, (err, row) => {
          if (err) {
              res.status(500).json({ error: err.message });
              return;
          }
          res.json({ 
              message: 'success',
              data: row
          });
      });
  });
  
  // Creates one candidate
  router.post('/candidates', ({ body }, res) => {
      const errors = inputCheck(
          body, 
          'first_name', 
          'last_name', 
          'industry_connected'
      );
      if (errors) {
          res.status(400).json({ error: errors });
          return;
      }
      const sql = `INSERT INTO candidates (id, first_name, last_name, industry_connected)
          VALUES (?, ?, ?)`;
      const params = [body.first_name, body.last_name, body.industry_connected];
  
      db.query(sql, params, (err, result) => {
          if (err) {
              res.status(400).json({ error: err.message });
              return;
          }
          res.json({
              message: 'success',
              data: body,
              changes: result.affectedRows
          });
      });
  });
  
  // Updates/changes a candidate's party
  router.put('/candidates/:id', (req, res) => {
      const errors = inputCheck(req.body, 'party_id');
      if (errors) {
          res.status(400).json({ error: errors });
          return;
      };
  
      const sql = `UPDATE candidates SET party_id = ?
          WHERE id = ?`;
      const params = [req.body.party_id, req.params.id];
      
      db.query(sql, params, (err, result) => {
          if (err) {
              res.status(400).json({ error: err.message });
              // check if a record was found
          } else if (!result.affectedRows) {
              res.json({
                  message: 'Candidate not found.'
              });
          } else {
              res.json({
                  message: 'success',
                  data: req.body,
                  changes: result.affectedRows
              });
          }
      });
  });
  
  // Deletes one candidate
  router.delete('/candidates/:id', (req, res) => {
      const sql = `DELETE FROM candidates WHERE id = ?`;
      const params = [req.params.id];
  
      db.query(sql, params, (err, result) => {
          if (err) {
              res.statusMessage(400).sjon({ error: res.message });
          } else if (!result.affectedRows) {
              res.json({ 
                  message: 'Candidate not found.'
              });
          } else {
              res.json({ 
                  message: 'deleted',
                  changes: result.affectedRows,
                  id: req.params.id
              });
          }
      });
  });
  
  module.exports = router;