<commit_before>module.exports = function(samples, schema) {
    'use strict';
    let ec = require('./error-code');
    let parse = require('co-body');

    return {
        all: all,
        byID: byID,
        create: create,
        update: update
    };

    function *all(next) {
        yield next;
    }

    function *byID(next) {
        yield next;
    }

    // create creates a new sample. It validates the submitted
    // entry and enters default values in for optional missing
    // attributes.
    function *create(next) {
        try {
            let sample = prepareSample(yield parse(this));
            yield validateSample(sample);
            let inserted = yield samples.create(sample);
            this.status = 200;
            this.body = inserted;
            yield next;
        } catch (err) {
            let e = ec(err);
            this.status = e.status();
            this.body = e.error();
        }

        /////////////////

        // validateSample validates the sample and any properties
        // included with the sample.
        function *validateSample(sample) {
            yield schema.samples.validateAsync(sample);
            for (let i = 0; i < sample.properties.length; i++) {
                yield schema.properties.validateAsync(sample.properties[i]);
            }
        }
    }

    function *update(next) {
        'use strict';
        try {
            let fields = prepareSample(yield parse(this));
            yield validateFields(fields);
            let updated = yield samples.update(this.params.sample_id, fields);
            this.status = 200;
            this.body = updated;
            yield next;
        } catch (err) {
            let e = ec(err);
            this.status = e.status();
            this.body = e.error();
        }

        //////////////

        // validateFields will validate the individual fields sent for
        // a sample and ignore missing fields.
        function *validateFields(fields) {
            yield schema.samples.validateAsync(fields, true);
            if ('properties' in fields) {
                for (let i = 0; i < fields.properties.length; i++) {
                    yield schema.properties.validateAsync(fields.properties[i], true);
                }
            }
        }
    }

    // prepareSample strips out unknown attributes and adds
    // default values for optional attributes.
    function prepareSample(sample) {<commit_msg>Add in the project_id and user from the validated reqctx and path params.<commit_after>module.exports = function(samples, schema) {
    'use strict';
    let ec = require('./error-code');
    let parse = require('co-body');

    return {
        all: all,
        byID: byID,
        create: create,
        update: update
    };

    function *all(next) {
        yield next;
    }

    function *byID(next) {
        yield next;
    }

    // create creates a new sample. It validates the submitted
    // entry and enters default values in for optional missing
    // attributes.
    function *create(next) {
        try {
            let sample = prepareSample(yield parse(this));
            sample.project_id = this.params.project_id;
            sample.owner = this.reqctx.user.id;
            yield validateSample(sample);
            let inserted = yield samples.create(sample);
            this.status = 200;
            this.body = inserted;
            yield next;
        } catch (err) {
            let e = ec(err);
            this.status = e.status();
            this.body = e.error();
        }

        /////////////////

        // validateSample validates the sample and any properties
        // included with the sample.
        function *validateSample(sample) {
            yield schema.samples.validateAsync(sample);
            for (let i = 0; i < sample.properties.length; i++) {
                yield schema.properties.validateAsync(sample.properties[i]);
            }
        }
    }

    function *update(next) {
        'use strict';
        try {
            let fields = prepareSample(yield parse(this));
            fields.project_id = this.params.project_id;
            yield validateFields(fields);
            let updated = yield samples.update(this.params.sample_id, fields);
            this.status = 200;
            this.body = updated;
            yield next;
        } catch (err) {
            let e = ec(err);
            this.status = e.status();
            this.body = e.error();
        }

        //////////////

        // validateFields will validate the individual fields sent for
        // a sample and ignore missing fields.
        function *validateFields(fields) {
            yield schema.samples.validateAsync(fields, true);
            if ('properties' in fields) {
                for (let i = 0; i < fields.properties.length; i++) {
                    yield schema.properties.validateAsync(fields.properties[i], true);
                }
            }
        }
    }

    // prepareSample strips out unknown attributes and adds
    // default values for optional attributes.
    function prepareSample(sample) {
{'fraction_non_alphanumeric': 0.11113273647333592, 'fraction_numerical': 0.003114052160373686, 'mean_word_length': 1.6020253164556961, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '1347316', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1264, 'n_words': 372}