<commit_before>import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check'

import { Bands, Songs, Audios } from '../../../imports/collections';
import { bandPermission } from './checkers';

Meteor.methods({
  'song.insert'(params) {

    check(params.bandId, bandPermission);

    let band = Bands.collection.findOne({ _id: params.bandId });

    let songId = Songs.collection.insert({
      bandId: params.bandId,
      audioIds: [],
      createdAt: new Date()
    });

    band.songIds.push(songId);

    Bands.collection.update({
      _id: band._id
    },{
      $set: {
        songIds: band.songIds
      }
    });

    return songId;
  },

  'song.update'(id, newParams) {

    check(id, String);

    check(newParams, {
      title: Match.Maybe(String),
      text: Match.Maybe(String),
      audioIds: Match.Maybe([String]),
      'metronome.bpm': Match.Maybe(Number),
      'metronome.bpb': Match.Maybe(Number)
    });

    let song = Songs.collection.findOne({ _id: id });

    check(song, Object);

    check(song.bandId, bandPermission);

    // Check that new audio/s belong to this song
    if (newParams.audioIds) {
      newParams.audioIds
      .filter(e => song.audioIds.indexOf(e) < 0)
      .forEach(audioId => {
        let audio = Audios.collection.findOne(audioId);

        if (audio.songId !== id) {

          throw new Meteor.Error('song-id-mismatch',
          'Trying to assing an audio to a different song');
        }
      });
    }

    Songs.collection.update({ _id: id }, {
      $set: newParams
    })
  },

  'song.remove'(id) {
    check(id, String)

    let song = Songs.collection.findOne({ _id: id });

    check(song, Object);

    check(song.bandId, bandPermission);

    Songs.collection.remove(id);
  }<commit_msg>Remove related song audios along with the song<commit_after>import { Meteor } from 'meteor/meteor';
import { check } from 'meteor/check'

import { Bands, Songs, Audios } from '../../../imports/collections';
import { bandPermission } from './checkers';

Meteor.methods({
  'song.insert'(params) {

    check(params.bandId, bandPermission);

    let band = Bands.collection.findOne({ _id: params.bandId });

    let songId = Songs.collection.insert({
      bandId: params.bandId,
      audioIds: [],
      createdAt: new Date()
    });

    band.songIds.push(songId);

    Bands.collection.update({
      _id: band._id
    },{
      $set: {
        songIds: band.songIds
      }
    });

    return songId;
  },

  'song.update'(id, newParams) {

    check(id, String);

    check(newParams, {
      title: Match.Maybe(String),
      text: Match.Maybe(String),
      audioIds: Match.Maybe([String]),
      'metronome.bpm': Match.Maybe(Number),
      'metronome.bpb': Match.Maybe(Number)
    });

    let song = Songs.collection.findOne({ _id: id });

    check(song, Object);

    check(song.bandId, bandPermission);

    // Check that new audio/s belong to this song
    if (newParams.audioIds) {
      newParams.audioIds
      .filter(e => song.audioIds.indexOf(e) < 0)
      .forEach(audioId => {
        let audio = Audios.collection.findOne(audioId);

        if (audio.songId !== id) {

          throw new Meteor.Error('song-id-mismatch',
          'Trying to assing an audio to a different song');
        }
      });
    }

    Songs.collection.update({ _id: id }, {
      $set: newParams
    })
  },

  'song.remove'(id) {
    check(id, String)

    let song = Songs.collection.findOne({ _id: id });

    check(song, Object);

    check(song.bandId, bandPermission);

    Audios.collection.remove({ songId: id});

    Songs.collection.remove(id);
  }
{'fraction_non_alphanumeric': 0.1517734396480616, 'fraction_numerical': 0.0005499037668408029, 'mean_word_length': 2.511583011583012, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '6413993', 'n_tokens_mistral': 1248, 'n_tokens_neox': 1165, 'n_words': 250}