{"size": 3319, "ext": "js", "max_stars_repo_path": "db/nasihat_collection.js", "max_stars_repo_name": "RobbiNespu/nasihati", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "db/nasihat_collection.js", "max_issues_repo_name": "RobbiNespu/nasihati", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "db/nasihat_collection.js", "max_forks_repo_name": "RobbiNespu/nasihati", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.7328767123, "max_line_length": 75, "alphanum_fraction": 0.4823742091}
var MongoClient = require('mongodb').MongoClient
// var ObjectId = require('mongodb').ObjectId

// Connection URL
var url = 'mongodb://localhost:27017/nasihat'
var collectionName = 'nasihats'

// Read data
module.exports = {

  /**
   * Get a nasihat by id
   */
  getNasihatById (id) {
    return new Promise((resolve, reject) => {
      connectToDB(url)
        .then((db) => {
          db.collection(collectionName)
            .find({id: id})
            .limit(1)
            .toArray(function (err, quote) {
              if (err) reject(err)

              resolve(quote[0])
              db.close()
            })
        })
        .catch(err => console.log(err))
    })
  },

  /**
   * Update nasihat by id
   */
  updateNasihatById (id, updateData) {
    return new Promise((resolve, reject) => {
      connectToDB(url)
        .then(db => {
          db.collection(collectionName)
            .updateOne({id: id},
              updateData,
              (err, result) => {
                // TODO pass a full result not result.result
                if (err) reject(err)

                resolve(result)
                db.close()
              })
        })
        .catch(err => console.log(err))
    })
  },

  /**
   * Create a new nasihat
   * TODO sanitize insertData @important
   * TODO automatic add increment id field to insertData @important @urgent
   */
  createNasihat (insertData) {
    return new Promise((resolve, reject) => {
      connectToDB(url)
        .then(db => {
          db.collection(collectionName)
            .insert(insertData,
              (err, result) => {
                if (err) reject(err)

                resolve(result)
              })
        })
        .catch(err => console.log(err))
    })
  },

  /**
   * Delete a nasihat
   * @params id if not Number use mongodb _id: ObjectId
   */
  deleteNasihat (id, callback) {
    return new Promise((resolve, reject) => {
      connectToDB(url)
        .then(db => {
          db.collection(collectionName)
            .remove({id: id}, (err, result) => {
              if (err) reject(err)

              resolve(result)
              db.close()
            })
        })
    })
  },

  /**
   * get next nasihat for given id
   */
  getNextNasihatForId (id) {
    return new Promise((resolve, reject) => {
      connectToDB(url)
        .then(db => {
          db.collection(collectionName)
            .find({id: {$gt: id}})
            .limit(1)
            .toArray((err, result) => {
              if (err) reject(err)

              resolve(result[0])
              db.close()
            })
        })
        .catch(err => console.log(err))
    })
  },

  /**
   * get previous nasihat for given id
   */
  getPrevNasihatForId (id) {
    return new Promise((resolve, reject) => {
      connectToDB(url)
        .then(db => {
          db.collection(collectionName)
            .find({id: {$lt: id}})
            .sort({id: -1})
            .limit(1)
            .toArray((err, result) => {
              if (err) reject(err)

              resolve(result)
              db.close()
            })
        })
    })
  }
}

function connectToDB (url) {
  return new Promise((resolve, reject) => {
    MongoClient.connect(url, function (err, db) {
      if (err) reject(err)

      resolve(db)
    })
  })
}
