lib/qpm_media.js
function insertRating(rating, config, conn) {


  // validate
  if ( (!rating.uri || rating.uri === '') && (!rating.cacheURI || rating.cacheURI === '') ) {
    return 'You must enter a valid uri'
  }
  if (!rating.reviewer || rating.reviewer === '') {
    return 'You must enter a valid reviewer'
  }

  // defaults
  config = config || require('../config/config.js')


  // main
  return new Promise((resolve, reject) => {

    if (!conn) {
      var conn = wc_db.getConnection(config.db)
    }

    var sql
    if (rating.uri) {
      sql = 'INSERT into Rating values (NULL, :uri, :rating, NULL, :reviewer, NULL, NOW())'
    } else {
      sql = 'INSERT into Rating SELECT NULL, a.uri, :rating, NULL, :reviewer, NULL, NOW() from Media a where a.cacheURI = :cacheURI '
    }
    debug('insertRating', sql, rating)

    conn.query(sql, { replacements: { "uri" : rating.uri, "cacheURI" : rating.cacheURI, "reviewer" : rating.reviewer, "rating" : rating.rating ? rating.rating : null } }).then(function(ret){
      return resolve({"ret" : ret, "conn" : conn})
    }).catch(function(err) {
      return reject({"err" : err, "conn" : conn})
    })

  })

}
function insertRating(rating, config, conn) {


  // validate
  if ( (!rating.uri || rating.uri === '') && (!rating.cacheURI || rating.cacheURI === '') ) {
    return 'You must enter a valid uri'
  }
  if (!rating.reviewer || rating.reviewer === '') {
    return 'You must enter a valid reviewer'
  }

  // defaults
  config = config || require('../config/config.js')


  // main
  return new Promise((resolve, reject) => {

    if (!conn) {
      var conn = wc_db.getConnection(config.db)
    }

    var sql
    if (rating.uri) {
      sql = 'INSERT into Rating values (NULL, :uri, :rating, NULL, :reviewer, NULL, NOW())'
    } else {
      sql = 'INSERT into Rating SELECT NULL, a.uri, :rating, NULL, :reviewer, NULL, NOW() from Media a where a.cacheURI = :cacheURI '
    }
    debug('insertRating', sql, rating)

    conn.query(sql, { replacements: { "uri" : rating.uri, "cacheURI" : rating.cacheURI, "reviewer" : rating.reviewer, "rating" : rating.rating ? rating.rating : null } }).then(function(ret){
      return resolve({"ret" : ret, "conn" : conn})
    }).catch(function(err) {
      return reject({"err" : err, "conn" : conn})
    })

  })

}
function insertRating ( rating , config , conn ) { // validate if ( ( ! rating . uri || rating . uri === '' ) && ( ! rating . cacheURI || rating . cacheURI === '' ) ) { return 'You must enter a valid uri' } if ( ! rating . reviewer || rating . reviewer === '' ) { return 'You must enter a valid reviewer' } // defaults config = config || require ( '../config/config.js' ) // main return new Promise ( ( resolve , reject ) => { if ( ! conn ) { var conn = wc_db . getConnection ( config . db ) } var sql if ( rating . uri ) { sql = 'INSERT into Rating values (NULL, :uri, :rating, NULL, :reviewer, NULL, NOW())' } else { sql = 'INSERT into Rating SELECT NULL, a.uri, :rating, NULL, :reviewer, NULL, NOW() from Media a where a.cacheURI = :cacheURI ' } debug ( 'insertRating' , sql , rating ) conn . query ( sql , { replacements : { "uri" : rating . uri , "cacheURI" : rating . cacheURI , "reviewer" : rating . reviewer , "rating" : rating . rating ? rating . rating : null } } ) . then ( function ( ret ) { return resolve ( { "ret" : ret , "conn" : conn } ) } ) . catch ( function ( err ) { return reject ( { "err" : err , "conn" : conn } ) } ) } ) }
Inserts rating to the database
@param {Object} rating  The rating to add.
@param {Object} config  The optional config.
@param {Object} conn    The optional db connection.
@return {Object} Promise with success or fail.
Inserts rating to the database