{"size": 1433, "ext": "js", "max_stars_repo_path": "services/player/server/models/index.js", "max_stars_repo_name": "hrr36-sdc-t2/scootify", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "services/player/server/models/index.js", "max_issues_repo_name": "hrr36-sdc-t2/scootify", "max_issues_repo_issues_event_min_datetime": "2019-03-24T17:49:50.000Z", "max_issues_repo_issues_event_max_datetime": "2019-03-25T15:10:57.000Z", "max_forks_repo_path": "services/player/server/models/index.js", "max_forks_repo_name": "hrr36-sdc-t2/scootify", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 5, "max_forks_count": null, "avg_line_length": 24.7068965517, "max_line_length": 75, "alphanum_fraction": 0.5840893231}
var db = require('../db');

// selects the first 30 tracks from db
let getPlaylist30 = function (callback) {
  db.dbConnection.query(
    'SELECT * FROM playlist ORDER BY id ASC LIMIT 30',
    (err, data) => {
      if (err) {
        console.log(err, 'ERROR WITH MYSQL SELECT QUERY!');
        callback(err, null);
      } else {
        console.log(data, 'this is the playlist data!');
        callback(null, data);
      }
    }
  );
};

//selects a specific track from db based on track_id
let getTrack = function (trackid, callback) {
  db.dbConnection.query(
    'SELECT * FROM playlist WHERE track_id = ? LIMIT 1 ',
    [trackid],
    (err, data) => {
      if (err) {
        console.log(err, 'ERROR WITH MYSQL SELECT QUERY!');
        callback(err, null);
      } else {
        console.log(data, 'this is the playlist data!');
        callback(null, data);
      }
    }
  );
};

//a function which toggles a song as favorited or unfavorited
let toggleFavorite = function (trackid, callback) {
  db.dbConnection.query(
    'UPDATE playlist SET favorite = (favorite + 1) % 2 WHERE track_id = ?',
    [trackid],
    (err, data) => {
      if (err) {
        console.log(err, 'ERROR WITH MYSQL UPDATE QUERY!!');
        callback(err, null);
      } else {
        console.log(data, 'toggled favorites!');
        callback(null, data);
      }
    }
  );
};

module.exports = {
  getTrack,
  getPlaylist30,
  toggleFavorite
};
