text: const db = require('../db-connection');
  
  const controllers = {
    getAll: (req, res) => {
  
      const sql = `SELECT * FROM albums`;
  
      db.all(sql, (err, rows) => {
        if (err) {
          res.status(400).json({ "error": err.message });
          return;
        }
        res.json(rows)
      });
    },
    getOne: (req, res) => {
      
      const id = req.body.id;
  
      const sql = `SELECT * FROM albums where AlbumId = '${id}'`;
  
      db.all(sql, (err, rows) => {
        if (err) {
          res.status(400).json({ "error": err.message });
          return;
        }
  
        res.json(rows)
      });
    },
    create: (req, res) => {
      
      const title = req.body.title;
      const ArtistId = req.body.ArtistId;
      
          
      const sql = `INSERT INTO Albums (Title , ArtistId)
  VALUES("${title}" , "${ArtistId}" )`;
  
  
      db.all(sql, (err, rows) => {
        if (err) {
          res.status(400).json({ "error": err.message });
          return;
        }
        //res.send(rows)
        res.redirect('/')
      });
      
    },
    update: (req, res) => {
      // read row data from body
       const title = req.body.title;
      const ArtistId = req.body.ArtistId;
      const id = parseInt(req.body.id);
      
          
      const sql = `UPDATE Albums
  SET Title = "${title}",
      ArtistId = "${ArtistId}"
  WHERE
      AlbumId = ${id}`;
  
  
      db.all(sql, (err, rows) => {
        if (err) {
          res.status(400).json({ "error": err.message });
          return;
        }
        //res.send(`your changes have been saved`)
        res.redirect('/')
      });
    },
    delete: (req, res) => {
      const id = req.body.id;
      console.log(id);
      
  
      const sql = `DELETE FROM Albums WHERE AlbumId = "${id}"`;
  
      db.all(sql, (err, rows) => {
        if (err) {
          res.status(400).json({ "error": err.message });
          return;
        }
  
       // res.json(`it's deleted`)
        res.redirect('/')
      });
     }
  }
  
  module.exports = controllers;
  