<commit_before>'use strict';

const Product = require('./product.model.js');
const bindAll = require('lodash.bindall');
const parseForm = require('./../../helpers/multipartyPromise/multipartyPromise.js');
const cloudinaryAPI = require('./../../helpers/cloudinaryUploaderPromise/cloudinaryUploaderPromise.js');
const path = require('path');
const config = require('./../../config/index');
const root = require('app-root-path');

module.exports = class ProductController {
    constructor() {
        this.req = null;
        this.res = null;
        this.next = null;

        bindAll(this, '_sendRes', '_uploadStatic', '_uploadFiles', '_destroyFile', '_saveProduct', '_updateProduct', '_removeProduct');
    }

    get(req, res, next) {
        this.res = res;

        Product.find()
            .then(this._sendRes)
            .catch(err => {
                console.log(err);
            });
    }

    post(req, res, next) {
        this.res = res;

        this._parseForm(req, config.imagePath)
            .then(this._uploadStatic)
            .then(this._saveProduct)
            .then(this._sendRes)
            .catch(err => {
                console.log(err);
            });
    }

    put(req, res, next) {
        this.res = res;

        this._parseForm(req, config.imagePath)
            .then(this._uploadStatic)
            .then(this._updateProduct)
            .then(this._sendRes)
            .catch(err => {
                console.log(err);
            });
    }

    delete(req, res, next) {
        this.res = res;

        Product.findOne({_id: req.params.id})
            .then(this._destroyFile)
            .then(this._removeProduct)
            .then(this._sendRes)
            .catch(err => {
                console.log(err);
            });
    }

    _sendRes(data) {
        this.res.send(data);
    }

    _saveProduct(data) {
        let product;

        data = this._prepareData(data);

        product = new Product(data);

        return product.save();
    }

    _updateProduct(data) {
        data = this._prepareData(data);

        return Product.update({_id: data._id}, data);<commit_msg>Fixed db call<commit_after>'use strict';

const Product = require('./product.model.js');
const bindAll = require('lodash.bindall');
const parseForm = require('./../../helpers/multipartyPromise/multipartyPromise.js');
const cloudinaryAPI = require('./../../helpers/cloudinaryUploaderPromise/cloudinaryUploaderPromise.js');
const path = require('path');
const config = require('./../../config/index');
const root = require('app-root-path');

module.exports = class ProductController {
    constructor() {
        this.req = null;
        this.res = null;
        this.next = null;

        bindAll(this, '_sendRes', '_uploadStatic', '_uploadFiles', '_destroyFile', '_saveProduct', '_updateProduct', '_removeProduct');
    }

    get(req, res, next) {
        this.res = res;

        Product.find()
            .then(this._sendRes);
    }

    post(req, res, next) {
        this.res = res;

        this._parseForm(req, config.imagePath)
            .then(this._uploadStatic)
            .then(this._saveProduct)
            .then(this._sendRes)
            .catch(err => {
                console.log(err);
            });
    }

    put(req, res, next) {
        this.res = res;

        this._parseForm(req, config.imagePath)
            .then(this._uploadStatic)
            .then(this._updateProduct)
            .then(this._sendRes)
            .catch(err => {
                console.log(err);
            });
    }

    delete(req, res, next) {
        this.res = res;

        Product.findOne({_id: req.params.id})
            .then(this._destroyFile)
            .then(this._removeProduct)
            .then(this._sendRes);
    }

    _sendRes(data) {
        this.res.send(data);
    }

    _saveProduct(data) {
        let product;

        data = this._prepareData(data);

        product = new Product(data);

        return product.save();
    }

    _updateProduct(data) {
        data = this._prepareData(data);

        return Product.update({_id: data._id}, data);
{'fraction_non_alphanumeric': 0.14764448761534726, 'fraction_numerical': 0.0, 'mean_word_length': 1.927505330490405, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '1208793', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1184, 'n_words': 207}