<commit_before>'use strict';

const database = require('./database.js');
const ObjectID = require('mongodb').ObjectID;

/**
 * Retrieves an item from the database using ID.
 */

function getItemByID(id) {

    return new Promise((resolve, reject) => {
        database.getDB().then(db => {
            //console.log(`Successfully connected to the db. Looking for id: ${id}`);

            let result = db.items.findOne({
                'itemID': id
            });

            return result;
        }).then(dbResult => {
            //console.log(`Function getItemByID -> dbResult ${JSON.stringify(dbResult, null, 4)}`);
            resolve(dbResult);
        }).catch(error => {
            console.log(error);
            resolve(null);
        });
    });
}

function addItem(item) {

    let gw2DB = null;

    return new Promise((resolve, reject) => {
        database.getDB().then(db => {

                gw2DB = db;

                var cursor = db.items.find({
                    'itemID': item.itemID
                }).count();

                return cursor;

            })
            .then(count => {
                if (count > 0) {
                    //console.log(`Function addItem -> ${item.name} already exists in the database. Count: ${count}`);
                    return false;
                } else {
                    item._id = new ObjectID();
                    return gw2DB.items.insertOne(item);
                }
            })
            .then(result => {
                resolve(item);
            })
            .catch(error => {
                console.log(error);
                resolve(null);
            });
    });
}

function addRecipes(id, recipes) {
    return new Promise((resolve, reject) => {

        //console.log(`Adding recipe data for id: ${id}`);

        let gw2DB = null;

        database.getDB().then(db => {

                gw2DB = db;

                let cursor = db.items.find({
                    'itemID': id,
                    'recipes': {
                        $exists: true,
                        $ne: []
                    }
                });

                return cursor;

            }).then(check => {
                if (check) {
                    return gw2DB.items.updateOne({
                        'itemID': id
                    }, {
                        $set: {
                            'recipes': recipes
                        }
                    });
                } else {
                    return false;
                }

            })
            .then(result => {
                if (result) {
                    resolve(recipes);
                } else {
                    resolve(`Recipe data already exists for item ${id}`);
                }
            })
            .catch(error => {
                console.log(error);
                resolve(null);
            });

    });
}

exports.getItemByID = getItemByID;<commit_msg>Added db function to search by item name<commit_after>'use strict';

const database = require('./database.js');
const ObjectID = require('mongodb').ObjectID;

/**
 * Retrieves an item from the database using ID.
 */

function getItemByID(id) {

    return new Promise((resolve, reject) => {
        database.getDB().then(db => {
            //console.log(`Successfully connected to the db. Looking for id: ${id}`);

            let result = db.items.findOne({
                'itemID': id
            });

            return result;
        }).then(dbResult => {
            resolve(dbResult);
        }).catch(error => {
            console.log(error);
            resolve(null);
        });
    });
}

function getItemByName(searchText) {

    return new Promise((resolve, reject) => {
        database.getDB().then(db => {

            var cursor = db.items.find({
                $text: {
                    $search: searchText
                }
            }, {
                name: 1,
                img: 1,
                itemID: 1,
                _id: 0
            }).toArray();

            return cursor;

        }).then(dbResult => {
            resolve(dbResult);
        }).catch(error => {
            console.log(error);
            resolve(null);
        });
    });
}

function addItem(item) {

    let gw2DB = null;

    return new Promise((resolve, reject) => {
        database.getDB().then(db => {

                gw2DB = db;

                var cursor = db.items.find({
                    'itemID': item.itemID
                }).count();

                return cursor;

            })
            .then(count => {
                if (count > 0) {
                    //console.log(`Function addItem -> ${item.name} already exists in the database. Count: ${count}`);
                    return false;
                } else {
                    item._id = new ObjectID();
                    return gw2DB.items.insertOne(item);
                }
            })
            .then(result => {
                resolve(item);
            })
            .catch(error => {
                console.log(error);
                resolve(null);
            });
    });
}

function addRecipes(id, recipes) {
    return new Promise((resolve, reject) => {

        //console.log(`Adding recipe data for id: ${id}`);

        let gw2DB = null;

        database.getDB().then(db => {

                gw2DB = db;

                let cursor = db.items.find({
                    'itemID': id,
                    'recipes': {
                        $exists: true,
                        $ne: []
                    }
                });

                return cursor;

            }).then(check => {
                if (check) {
                    return gw2DB.items.updateOne({
                        'itemID': id
                    }, {
                        $set: {
                            'recipes': recipes
                        }
                    });
                } else {
                    return false;
                }

            })
            .then(result => {
                if (result) {
                    resolve(recipes);
                } else {
                    resolve(`Recipe data already exists for item ${id}`);
                }
            })
            .catch(error => {
                console.log(error);
                resolve(null);
            });

    });
}

exports.getItemByID = getItemByID;
exports.getItemByName = getItemByName;
{'fraction_non_alphanumeric': 0.12579199505486013, 'fraction_numerical': 0.0029361767887498068, 'mean_word_length': 1.0155714730613516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 40, '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': '7113074', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1587, 'n_words': 345}