'use strict';

var _ = require('lodash');
var B = require('bluebird');

var Tasks = require('../lib/dbModels/Tasks');

/**
 * Function returns map to work with {@link Tasks} table
 * @returns {{get: Function, post: Function, put: Function, delete: Function}}
 * @constructor
 */
var TasksModel = function TasksModelConstructor() {
    return {
        /**
         * Find tasks and execute callback
         * @param {Number} [id] ID of the task
         * @param {Function} cb Callback to execute after we found tasks
         * @returns {*}
         */
        get: function (id, cb) {
            var found;

            if (_.isUndefined(cb) && _.isFunction(id)) {
                cb = id;
                id = null;
            }

            if (_.isNull(id)) {
                found = Tasks.all();
            }
            else {
                found = Tasks.findOne({ where: { id: id } });
            }

            return found
                .then(function (results) {
                    cb(null, results);
                })
                .catch(function (err) {
                    cb(err);
                });
        },
        post: function (task, cb) {
            var promises = [];
            var fulfilledPromise;

            if (_.isArray(task)) {
                _.forEach(user, function(v) {
                    promises.push(Tasks.create(v));
                });
                fulfilledPromise = B.all(promises);
            }
            else if (_.isPlainObject(task)) {
                fulfilledPromise = Tasks.create(task);
            }

            return fulfilledPromise
                .then(function (result) {
                    cb(null, result);
                })
                .catch(function (err) {
                    cb(err);
                });
        },
        put: function (id, task, cb) {
            return Tasks.update(task, { where: { id: id } })
                .then(function () {
                    console.log(arguments);
                });
        },
        delete: function (id, cb) {
            return Tasks.destroy({ where: { id: id } })
                .then(function () {
                    console.log(arguments);
                });
        }
    }
};


module.exports = TasksModel();

@@ -1930,32 +1930,40 @@
 .then(function (
+affected
 ) %7B%0A            
@@ -1971,36 +1971,139 @@
        c
-onsole.log(arguments
+b(null, %7B 'affected': affected%5B0%5D %7D);%0A                %7D)%0A                .catch(function (err) %7B%0A                    cb(err
 );%0A     
@@ -2252,16 +2252,24 @@
 nction (
+affected
 ) %7B%0A    
@@ -2289,28 +2289,128 @@
    c
-onsole.log(arguments
+b(null, %7B 'affected': affected %7D);%0A                %7D)%0A                .catch(function (err) %7B%0A                    cb(err
 );%0A 

add put and delete fns to model