<commit_before>var Stream = require('../models/streamsModel');
var User = require('../models/usersModel');
var usersController = require('./usersController');

module.exports = {
  createStream: function(req, res, next) {
    var streamName = req.body.name;
    var streamDesc = req.body.desc;
    //to make location into array
    var streamLongitude = req.body.lng;
    var streamLatitude = req.body.lat;
    var streamCreator = req.body.creator;

    User.findOne({scId: streamCreator.id}, function(err, doc) {
      if (doc) {
        console.log('creator found');
        var creatorId = doc._id;
        var newStream = new Stream({name: streamName, 
          description: streamDesc,
          heartCountNum: 0,
          listenerMaxCount: 0,
          listenerLiveCount: 0,
          timestamp: Date.now(),
          playing: true,
          latitude: streamLatitude,
          longitude: streamLongitude,
          creator: creatorId
        });
        newStream.save(function(err, doc) {
          if (err) {
            throw err;
          }
          else {
            res.status(201).send(doc);
          }
        });
      }
      else {
        usersController.createUser(streamCreator, function(doc) {
          var creatorId = doc._id;
          var newStream = new Stream({name: streamName,
            description: streamDesc,
            heartCountNum: 0,
            listenerMaxCount: 0,
            listenerLiveCount: 0,
            timestamp: Date.now(),
            playing: true,
            latitude: streamLatitude,
            longitude: streamLongitude,
            creator: creatorId
          });
          newStream.save(function(err, doc) {
            if (err) {
              throw err
            }
            else {
              res.status(201).send(doc);
            }
          });
        });
      }
    });
  },

  getStream: function(req, res, next) {
    var streamId = req.params.stream;
    Stream.findById(streamId, function(err, doc) {
      if (err) {
        throw err;
      }
      else {
        res.status(200).send(doc);
      }
    });
  },

  upHeart: function(req, res, next) {
    var streamId = req.params.stream;
    Stream.findById(streamId, function(err, stream) {
      stream.heartCountNum++;
      console.log('updated the heartCount');
      stream.save(function(err, doc) {
        if (err) {
          return err;
        }
        else {
          res.status(200).send(doc);
        }
      });
    });
  },

  addListener: function(req, res, next) {
    var streamId = req.params.stream;<commit_msg>Attach broadcaster's name to stream data.<commit_after>var Stream = require('../models/streamsModel');
var User = require('../models/usersModel');
var usersController = require('./usersController');

module.exports = {
  createStream: function(req, res, next) {
    var streamName = req.body.name;
    var streamDesc = req.body.desc;
    //to make location into array
    var streamLongitude = req.body.lng;
    var streamLatitude = req.body.lat;
    var streamCreator = req.body.creator;

    User.findOne({
      scId: streamCreator.id
    }, function(err, user) {
      if (user) {
        console.log('creator found');
        console.log(user)
        var newStream = new Stream({
          name: streamName,
          broadcaster: user.scUsername,
          description: streamDesc,
          heartCountNum: 0,
          listenerMaxCount: 0,
          listenerLiveCount: 0,
          timestamp: Date.now(),
          playing: true,
          latitude: streamLatitude,
          longitude: streamLongitude,
          creator: user._id
        });
        newStream.save(function(err, stream) {
          if (err) {
            throw err;
          }
          else {
            res.status(201).send(stream);
          }
        });
      }
      else {
        usersController.createUser(streamCreator, function(user) {
          var newStream = new Stream({
            name: streamName,
            broadcaster: user.scUsername,
            description: streamDesc,
            heartCountNum: 0,
            listenerMaxCount: 0,
            listenerLiveCount: 0,
            timestamp: Date.now(),
            playing: true,
            latitude: streamLatitude,
            longitude: streamLongitude,
            creator: user._id
          });
          newStream.save(function(err, stream) {
            if (err) {
              throw err
            }
            else {
              res.status(201).send(stream);
            }
          });
        });
      }
    });
  },

  getStream: function(req, res, next) {
    var streamId = req.params.stream;
    Stream.findById(streamId, function(err, stream) {
      if (err) {
        throw err;
      }
      else {
        res.status(200).send(stream);
      }
    });
  },

  upHeart: function(req, res, next) {
    var streamId = req.params.stream;
    Stream.findById(streamId, function(err, stream) {
      stream.heartCountNum++;
      console.log('updated the heartCount');
      stream.save(function(err, doc) {
        if (err) {
          return err;
        }
        else {
          res.status(200).send(doc);
        }
      });
    });
  },

  addListener: function(req, res, next) {
    var streamId = req.params.stream;
{'fraction_non_alphanumeric': 0.11770280515542077, 'fraction_numerical': 0.006823351023502654, 'mean_word_length': 1.7257231404958677, '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': '3946498', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1493, 'n_words': 322}