<commit_before>var db = require('../db/database');
var Group = db.Group;
var Playlist = db.Playlist;
var Song = db.Song;
var PlaylistSongs = db.PlaylistSongs;

var addSong = function (songId, playlistId) {
  return new Promise(function (resolve, reject) {
    Song.findById(songId)
    .then(function (song) {
      Playlist.findById(playlistId)
      .then(function (playlist) {
        playlist.addSong(song)
        .then(function (response) {
          resolve(response[0][0]); // return the playlistSongs row
        });
      })
      .catch(function(error) {
        reject(error);
      });
    })
    .catch(function (error) {
      reject(error);
    });
  });
};

var createPlaylist = function (groupId, title, description) {
  return Playlist.create({
    title: title,
    description: description,
    groupId: groupId
  },
  {include: {
    model: Group}
  });
};

var deletePlaylist = function (playlistId) {
  return Playlist.findOne({where: {id: playlistId}});
};

var getSongs = function (playlistId) {
  return new Promise(function (resolve, reject) {
    Playlist.findById(playlistId)
    .then(function(playlist) {
      if (playlist) {
        resolve(playlist.getSongs());
      } else {
        resolve([]);
      }
    });
  });
};

var removeSong = function (songId, playlistId) {
  return PlaylistSongs.destroy({ where: {songId: songId, playlistId: playlistId}});
};

module.exports = {
  addSong: addSong,
  createPlaylist: createPlaylist,
  deletePlaylist: deletePlaylist,
  getSongs: getSongs,
  removeSong: removeSong
};<commit_msg>(feat) adding a song to a playlist puts it at a position at the end<commit_after>var db = require('../db/database');
var Group = db.Group;
var Playlist = db.Playlist;
var Song = db.Song;
var PlaylistSongs = db.PlaylistSongs;

var addSong = function(songId, playlistId) {
  return new Promise(function(resolve, reject) {
    Song.findById(songId)
    .then(function(song) {
      PlaylistSongs.count({ where: { playlistId: playlistId } })
      .then(function(count) {
        PlaylistSongs.create({
          songId: songId,
          playlistId: playlistId,
          listPosition: count
        })
        .then(function(response) {
          console.log(JSON.stringify(response));
          resolve(response); // return the playlistSongs row
        });
      })
      .catch(function(error) {
        reject(error);
      });
    })
    .catch(function(error) {
      reject(error);
    });
  });
};

var createPlaylist = function(groupId, title, description) {
  return Playlist.create({
    title: title,
    description: description,
    groupId: groupId
  }, {
    include: {
      model: Group
    }
  });
};

var deletePlaylist = function(playlistId) {
  return Playlist.findOne({ where: { id: playlistId } });
};

var getSongs = function(playlistId) {
  return new Promise(function(resolve, reject) {
    Playlist.findById(playlistId)
      .then(function(playlist) {
        if (playlist) {
          resolve(playlist.getSongs());
        } else {
          resolve([]);
        }
      });
  });
};

var removeSong = function(songId, playlistId) {
  return PlaylistSongs.destroy({ where: { songId: songId, playlistId: playlistId } });
};

module.exports = {
  addSong: addSong,
  createPlaylist: createPlaylist,
  deletePlaylist: deletePlaylist,
  getSongs: getSongs,
  removeSong: removeSong
};
{'fraction_non_alphanumeric': 0.13523131672597866, 'fraction_numerical': 0.0005931198102016608, 'mean_word_length': 2.972909305064782, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4914263', 'n_tokens_mistral': 1104, 'n_tokens_neox': 1056, 'n_words': 230}