<commit_before>var express = require('express');
var monk = require('monk');
var bodyParser = require('body-parser');

var dbAPI = require('api/db.js');
var common = require('api/routing_common.js');

var Item = require('api/models/Item.js');
var InventoryRequest = require('api/models/InventoryRequest.js');
var User = require('api/models/User.js');

var router = express.Router();
router.use(bodyParser.json());

router.get('/requests', common.asyncMiddleware(
    async (req, res) => {
        var requestList = await dbAPI.requests.find({}, {});
        var summaries = await Promise.all(requestList.map(
            (doc) => {
                var request = new InventoryRequest(doc._id);
                return request.summary();
            }
        ));

        res.status(200).json(summaries);
    }
));

router.post('/requests', common.asyncMiddleware(
    async (req, res) => {
        await common.checkRequestParameters(req, 'item', 'requester', 'count', 'status', 'eta');

        var invRQ = new InventoryRequest();

        await Promise.all([
            invRQ.item(req.body.item),
            invRQ.requester(req.body.requester),
            invRQ.count(req.body.count),
            invRQ.status(req.body.status),
            invRQ.eta(req.body.eta)
        ]);

        await invRQ.save();
        res.status(201).json(await invRQ.summary());
    }
));

router.use('/requests/:qid', common.asyncMiddleware(
    async (req, res, next) => {
        var targetRQ = new InventoryRequest(req.params.qid);
        if(!(await targetRQ.exists())) throw new Error("Request does not exist.");

        req.invRQ = targetRQ;
        next();
    }<commit_msg>Update Inventory Requests API to use APIClientError classes<commit_after>var express = require('express');
var monk = require('monk');
var bodyParser = require('body-parser');

var dbAPI = require('api/db.js');
var common = require('api/routing_common.js');

var Item = require('api/models/Item.js');
var InventoryRequest = require('api/models/InventoryRequest.js');
var User = require('api/models/User.js');

var router = express.Router();
router.use(bodyParser.json());

router.get('/requests', common.asyncMiddleware(
    async (req, res) => {
        var requestList = await dbAPI.requests.find({}, {});
        var summaries = await Promise.all(requestList.map(
            (doc) => {
                var request = new InventoryRequest(doc._id);
                return request.summary();
            }
        ));

        res.status(200).json(summaries);
    }
));

router.post('/requests', common.asyncMiddleware(
    async (req, res) => {
        await common.checkRequestParameters(req, 'item', 'requester', 'count', 'status', 'eta');

        var invRQ = new InventoryRequest();

        await Promise.all([
            invRQ.item(req.body.item),
            invRQ.requester(req.body.requester),
            invRQ.count(req.body.count),
            invRQ.status(req.body.status),
            invRQ.eta(req.body.eta)
        ]);

        await invRQ.save();
        res.status(201).json(await invRQ.summary());
    }
));

router.use('/requests/:qid', common.asyncMiddleware(
    async (req, res, next) => {
        var targetRQ = new InventoryRequest(req.params.qid);
        if(!(await targetRQ.exists())) throw new common.APIClientError(404, "Request does not exist.");

        req.invRQ = targetRQ;
        next();
    }
{'fraction_non_alphanumeric': 0.15945866431303324, 'fraction_numerical': 0.00441306266548985, 'mean_word_length': 2.719912472647702, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '2597751', 'n_tokens_mistral': 1045, 'n_tokens_neox': 1008, 'n_words': 202}