{"size": 1448, "ext": "js", "max_stars_repo_path": "services/roupa.service.js", "max_stars_repo_name": "brunomichelin/N2-MEAN-Estoque-Roupas", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "services/roupa.service.js", "max_issues_repo_name": "brunomichelin/N2-MEAN-Estoque-Roupas", "max_issues_repo_issues_event_min_datetime": "2020-07-18T08:57:58.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-10T15:41:05.000Z", "max_forks_repo_path": "services/roupa.service.js", "max_forks_repo_name": "brunomichelin/N2-MEAN-Estoque-Roupas", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 20.6857142857, "max_line_length": 68, "alphanum_fraction": 0.5697513812}
var config = require('config.json');
var _ = require('lodash');
var jwt = require('jsonwebtoken');
var bcrypt = require('bcryptjs');
var Q = require('q');
var mongo = require('mongoskin');
var db = mongo.db(config.connectionString, { native_parser: true });
db.bind('roupa');

var service = {};
service.createItem = createItem;
service.getallItem = getallItem;
service.delete = deleteItem;

module.exports = service;

function getallItem(user)
{
    var deferred = Q.defer();
    let query = {
        userid: user
    }
    db.roupa.find(query).toArray(function (err, doc)
    {
        if (err) deferred.reject(err.name + ': ' + err.message);
        deferred.resolve(doc);
    }
    )
    return deferred.promise;
} 



function createItem(Roupa) 
{
    var query = {
        cod_item: Roupa.cod_item
    }
    var deferred = Q.defer();
    
    db.roupa.findOneAndUpdate(
        query,
        Roupa, 
        {upsert: true},
        function (err, doc) 
        {
            if (err)
                    deferred.reject(err.name + ': ' + err.message);
            deferred.resolve();
        }
    );
    return deferred.promise;
}


 function deleteItem(_id) {
    var deferred = Q.defer();
    db.roupa.remove(
        { _id: mongo.helper.toObjectID(_id)
         },
        function (err) {
            if (err) deferred.reject(err.name + ': ' + err.message);

            deferred.resolve();
        });

    return deferred.promise;
}

