<commit_before>import Item from '../models/item';
import { error as e, httpStatus as status } from '../utils/response-utils';

export const getOne = app => (req, res) => {
    const query = Item.findOne({slug: req.params.slug});
    const promise = query.exec();

    promise.then(doc => {
        if(doc) {
            res.json({
                name: doc.name,
                category: doc.category,
                quantity: doc.quantity,
                price: doc.price,
                slug: doc.slug,
                created: doc.created,
                modified: doc.modified
            });
        } else {
            e.notFound(res);
        }
    }).catch(err => {
        app.get('logger').error(`${req.url} - ${err.toString()}`)
        e.serverErr(res);
    });
};

export const getAll = app => (req, res) => {
    const query = Item.find();
    const promise = query.exec();

    promise.then(doc => {
        const data = [];
        doc.forEach(item => {
            data.push({
                name: item.name,
                category: item.category,
                quantity: item.quantity,
                price: item.price,
                slug: item.slug,
                created: item.created,
                modified: item.modified
            });
        });

        res.json({
            items: data
        });
    }).catch(err => {
        app.get('logger').error(`${req.url} - ${err.toString()}`)
        e.serverErr(res);
    });
};

export const post = app => (req, res) => {
    const data = req.body;
    const query = Item.findOne({name: data.name});
    const promise = query.exec();

    promise.then(doc => {
        if(doc) {
            e.alreadyExists(res)(doc.name);
        } else {
            const item = new Item(data);

            try {
                item.generateSlug();
            } catch(err) {
                e.invalidInput(res, err);
                return;
            }

            item.save().then(() => {
                res.status(status.CREATED).json({
                    name: item.name,
                    category: item.category,
                    quantity: item.quantity,
                    price: item.price,
                    slug: item.slug,
                    created: item.created,
                    modified: item.modified
                });
            });
        }
    }).catch(err => {
        app.get('logger').error(`${req.url} - ${err.toString()}`)
        e.serverErr(res);
    });
};

export const patch = app => (req, res) => {
    const query = Item.findOne({slug: req.params.slug});
    const promise = query.exec();

    promise.then(doc => {
        if(doc) {
            const data = req.body;

            if(data.name) {
                const query2 = Item.findOne({name: data.name});
                const promise2 = query.exec();

                promise2.then(item => {
                    if(item &&
                        item.name.toLowerCase() !== doc.name.toLowerCase()) {
                        e.alreadyExists(res)(data.name);
                    }
                }).catch(err => {
                    app.get('logger')
                        .error(`${req.url} - ${err.toString()}`);
                    e.serverErr(res);
                });
            }

            for(let key in data) {
                doc[key] = data[key]
            }

            doc.generateSlug();
            doc.modified = Date.now();
            doc.save().then(() => {
                res.json({
                    name: doc.name,
                    category: doc.category,
                    quantity: doc.quantity,
                    price: doc.price,
                    slug: doc.slug,
                    created: doc.created,
                    modified: doc.modified
                });
            });
        } else {
            e.notFound(res);
        }
    }).catch(err => {
        app.get('logger').error(`${req.url} - ${err.toString()}`)
        e.serverErr(res);
    });
};


export const remove = app => (req, res) => {
    const query = Item.remove({slug: req.params.slug});
    const promise = query.exec();

    promise.then(doc => {
        if(doc.result.n > 0) {
            res.status(status.NOT_CONTENT).json();
        } else {
            e.notFound(res);
        }
    }).catch(err => {
        app.get('logger').error(`${req.url} - ${err.toString()}`);
        e.serverErr(res);
    });
};

export const itemIn = app => (req, res) => {
    const query = Item.findOne({slug: req.params.slug});
    const promise = query.exec();

    promise.then(doc => {
        if(doc) {
            doc.itemIn(req.body.quantity);
            doc.save().then(() => {
                res.json({
                    name: doc.name,
                    category: doc.category,
                    quantity: doc.quantity,
                    price: doc.price,
                    slug: doc.slug,
                    created: doc.created,
                    modified: doc.modified
                });
            });
        } else {
            e.notFound(res);
        }
    }).catch(err => {
        if(err === 'Invalid input') {
            e.invalidInput(res, err);
        } else {
            app.get('logger').error(`${req.url} - ${err.toString()}`)
            e.serverErr(res);
        }
    });
};

export const itemOut = app => (req, res) => {
    const query = Item.findOne({slug: req.params.slug});
    const promise = query.exec();

    promise.then(doc => {
        if(doc) {
            doc.itemOut(req.body.quantity);
            doc.save().then(() => {
                res.json({
                    name: doc.name,
                    category: doc.category,
                    quantity: doc.quantity,
                    price: doc.price,
                    slug: doc.slug,
                    created: doc.created,
                    modified: doc.modified
                });
            });
        } else {
            e.notFound(res);
        }
    }).catch(err => {
        if(err === 'Invalid input') {
            e.invalidInput(res, err);
        } else {
            app.get('logger').error(`${req.url} - ${err.toString()}`)
            e.serverErr(res);
        }
    });
};
<commit_msg>Added category and name wildcard search<commit_after>import Item from '../models/item';
import { error as e, httpStatus as status } from '../utils/response-utils';

export const getOne = app => (req, res) => {
    const query = Item.findOne({slug: req.params.slug});
    const promise = query.exec();

    promise.then(doc => {
        if(doc) {
            res.json({
                name: doc.name,
                category: doc.category,
                quantity: doc.quantity,
                price: doc.price,
                slug: doc.slug,
                created: doc.created,
                modified: doc.modified
            });
        } else {
            e.notFound(res);
        }
    }).catch(err => {
        app.get('logger').error(`${req.url} - ${err.toString()}`)
        e.serverErr(res);
    });
};

export const getAll = app => (req, res) => {
    let query = null;
    if (req.query.s && req.query.c) {
        query = Item.find().and([
            {name: new RegExp(res.query.s, 'i')},
            {category: new RegExp(res.query.c, 'i')}
        ]);
    } else if(req.query.s) {
        query = Item.find({name: new RegExp(req.query.s, 'i')});
    } else if(req.query.c) {
        query = Item.find({category: new RegExp(req.query.c, 'i')});
    } else {
        query = Item.find();
    }

    const promise = query.exec();

    promise.then(doc => {
        const data = [];
        doc.forEach(item => {
            data.push({
                name: item.name,
                category: item.category,
                quantity: item.quantity,
                price: item.price,
                slug: item.slug,
                created: item.created,
                modified: item.modified
            });
        });

        res.json({
            items: data
        });
    }).catch(err => {
        app.get('logger').error(`${req.url} - ${err.toString()}`)
        e.serverErr(res);
    });
};

export const post = app => (req, res) => {
    const data = req.body;
    const query = Item.findOne({name: data.name});
    const promise = query.exec();

    promise.then(doc => {
        if(doc) {
            e.alreadyExists(res)(doc.name);
        } else {
            const item = new Item(data);

            try {
                item.generateSlug();
            } catch(err) {
                e.invalidInput(res, err);
                return;
            }

            item.save().then(() => {
                res.status(status.CREATED).json({
                    name: item.name,
                    category: item.category,
                    quantity: item.quantity,
                    price: item.price,
                    slug: item.slug,
                    created: item.created,
                    modified: item.modified
                });
            });
        }
    }).catch(err => {
        app.get('logger').error(`${req.url} - ${err.toString()}`)
        e.serverErr(res);
    });
};

export const patch = app => (req, res) => {
    const query = Item.findOne({slug: req.params.slug});
    const promise = query.exec();

    promise.then(doc => {
        if(doc) {
            const data = req.body;

            if(data.name) {
                const query2 = Item.findOne({name: data.name});
                const promise2 = query.exec();

                promise2.then(item => {
                    if(item &&
                        item.name.toLowerCase() !== doc.name.toLowerCase()) {
                        e.alreadyExists(res)(data.name);
                    }
                }).catch(err => {
                    app.get('logger')
                        .error(`${req.url} - ${err.toString()}`);
                    e.serverErr(res);
                });
            }

            for(let key in data) {
                doc[key] = data[key]
            }

            doc.generateSlug();
            doc.modified = Date.now();
            doc.save().then(() => {
                res.json({
                    name: doc.name,
                    category: doc.category,
                    quantity: doc.quantity,
                    price: doc.price,
                    slug: doc.slug,
                    created: doc.created,
                    modified: doc.modified
                });
            });
        } else {
            e.notFound(res);
        }
    }).catch(err => {
        app.get('logger').error(`${req.url} - ${err.toString()}`)
        e.serverErr(res);
    });
};


export const remove = app => (req, res) => {
    const query = Item.remove({slug: req.params.slug});
    const promise = query.exec();

    promise.then(doc => {
        if(doc.result.n > 0) {
            res.status(status.NOT_CONTENT).json();
        } else {
            e.notFound(res);
        }
    }).catch(err => {
        app.get('logger').error(`${req.url} - ${err.toString()}`);
        e.serverErr(res);
    });
};

export const itemIn = app => (req, res) => {
    const query = Item.findOne({slug: req.params.slug});
    const promise = query.exec();

    promise.then(doc => {
        if(doc) {
            doc.itemIn(req.body.quantity);
            doc.save().then(() => {
                res.json({
                    name: doc.name,
                    category: doc.category,
                    quantity: doc.quantity,
                    price: doc.price,
                    slug: doc.slug,
                    created: doc.created,
                    modified: doc.modified
                });
            });
        } else {
            e.notFound(res);
        }
    }).catch(err => {
        if(err === 'Invalid input') {
            e.invalidInput(res, err);
        } else {
            app.get('logger').error(`${req.url} - ${err.toString()}`)
            e.serverErr(res);
        }
    });
};

export const itemOut = app => (req, res) => {
    const query = Item.findOne({slug: req.params.slug});
    const promise = query.exec();

    promise.then(doc => {
        if(doc) {
            doc.itemOut(req.body.quantity);
            doc.save().then(() => {
                res.json({
                    name: doc.name,
                    category: doc.category,
                    quantity: doc.quantity,
                    price: doc.price,
                    slug: doc.slug,
                    created: doc.created,
                    modified: doc.modified
                });
            });
        } else {
            e.notFound(res);
        }
    }).catch(err => {
        if(err === 'Invalid input') {
            e.invalidInput(res, err);
        } else {
            app.get('logger').error(`${req.url} - ${err.toString()}`)
            e.serverErr(res);
        }
    });
};

{'fraction_non_alphanumeric': 0.15458974557265487, 'fraction_numerical': 0.0006186683164488439, 'mean_word_length': 1.215521672091828, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 75, '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': '6524336', 'n_tokens_mistral': 3704, 'n_tokens_neox': 3580, 'n_words': 665}