<commit_before>module.exports = function(experimentDatasets, experiments, samples, schema) {
    const parse = require('co-body');
    const status = require('http-status');
    const _ = require('lodash');

    return {
        getDatasetsForExperiment,
        getDatasetForExperiment,
        createDatasetForExperiment,
        deleteDatasetFromExperiment,
        updateDatasetForExperiment,
        addSampleToDataset,
        updateSamplesInDataset,
        updateFilesInDataset,
        updateProcessesInDataset,
        getSamplesForDataset,
        publishDataset,
        unpublishDataset
    };

    function* getDatasetsForExperiment(next) {
        let rv = yield experimentDatasets.getDatasetsForExperiment(this.params.experiment_id);
        if (rv.error) {
            this.status = status.BAD_REQUEST;
            this.body = rv;
        } else {
            this.body = rv.val;
        }
        yield next;
    }

    function* getDatasetForExperiment(next) {
        let rv = yield experimentDatasets.getDataset(this.params.dataset_id);
        if (rv.error) {
            this.status = status.BAD_REQUEST;
            this.body = rv;
        } else {
            this.body = rv.val;
        }
        yield next;
    }

    function* createDatasetForExperiment(next) {
        let datasetArgs = yield parse(this);
        schema.prepare(schema.createDatasetSchema, datasetArgs);
        let errors = yield schema.validate(schema.createDatasetSchema, datasetArgs);
        if (errors !== null) {
            this.status = status.BAD_REQUEST;
            this.body = errors;
        } else {
            let rv = yield experimentDatasets.createDatasetForExperiment(this.params.experiment_id, this.reqctx.user.id, datasetArgs);
            if (rv.error) {
                this.status = status.BAD_REQUEST;
                this.body = rv;
            } else {
                this.body = rv.val;
            }
        }
        yield next;
    }

    function* deleteDatasetFromExperiment(next) {
        let rv = yield experimentDatasets.deleteDataset(this.params.dataset_id);
        if (rv.error) {
            this.status = status.BAD_REQUEST;
            this.body = rv;
        } else {
            this.body = rv.val;
        }
        yield next;
    }

    function* updateDatasetForExperiment(next) {
        let datasetArgs = yield parse(this);
        schema.prepare(schema.updateDatasetSchema, datasetArgs);
        let errors = yield validateUpdateDataset(datasetArgs);
        if (errors !== null) {
            this.status = status.BAD_REQUEST;
            this.body = errors;
        } else {
            let rv = yield experimentDatasets.updateDataset(this.params.dataset_id, datasetArgs);
            if (rv.error) {
                this.status = status.BAD_REQUEST;
                this.body = rv;
            } else {
                this.body = rv.val;
            }
        }
        yield next;
    }

    function* validateUpdateDataset(datasetArgs) {<commit_msg>Make sure dataset hasn't been published.<commit_after>module.exports = function(experimentDatasets, experiments, samples, schema) {
    const parse = require('co-body');
    const status = require('http-status');
    const _ = require('lodash');

    return {
        getDatasetsForExperiment,
        getDatasetForExperiment,
        createDatasetForExperiment,
        deleteDatasetFromExperiment,
        updateDatasetForExperiment,
        addSampleToDataset,
        updateSamplesInDataset,
        updateFilesInDataset,
        updateProcessesInDataset,
        getSamplesForDataset,
        publishDataset,
        unpublishDataset
    };

    function* getDatasetsForExperiment(next) {
        let rv = yield experimentDatasets.getDatasetsForExperiment(this.params.experiment_id);
        if (rv.error) {
            this.status = status.BAD_REQUEST;
            this.body = rv;
        } else {
            this.body = rv.val;
        }
        yield next;
    }

    function* getDatasetForExperiment(next) {
        let rv = yield experimentDatasets.getDataset(this.params.dataset_id);
        if (rv.error) {
            this.status = status.BAD_REQUEST;
            this.body = rv;
        } else {
            this.body = rv.val;
        }
        yield next;
    }

    function* createDatasetForExperiment(next) {
        let datasetArgs = yield parse(this);
        schema.prepare(schema.createDatasetSchema, datasetArgs);
        let errors = yield schema.validate(schema.createDatasetSchema, datasetArgs);
        if (errors !== null) {
            this.status = status.BAD_REQUEST;
            this.body = errors;
        } else {
            let rv = yield experimentDatasets.createDatasetForExperiment(this.params.experiment_id, this.reqctx.user.id, datasetArgs);
            if (rv.error) {
                this.status = status.BAD_REQUEST;
                this.body = rv;
            } else {
                this.body = rv.val;
            }
        }
        yield next;
    }

    function* deleteDatasetFromExperiment(next) {
        let dsrv = yield experimentDatasets.getDataset(this.params.dataset_id);
        if (dsrv.val.published) {
            this.status = status.BAD_REQUEST;
            this.body = {error: `You may only delete unpublished datasets`};
        } else {
            let rv = yield experimentDatasets.deleteDataset(this.params.dataset_id);
            if (rv.error) {
                this.status = status.BAD_REQUEST;
                this.body = rv;
            } else {
                this.body = rv.val;
            }
        }
        yield next;
    }

    function* updateDatasetForExperiment(next) {
        let datasetArgs = yield parse(this);
        schema.prepare(schema.updateDatasetSchema, datasetArgs);
        let errors = yield validateUpdateDataset(datasetArgs);
        if (errors !== null) {
            this.status = status.BAD_REQUEST;
            this.body = errors;
        } else {
            let rv = yield experimentDatasets.updateDataset(this.params.dataset_id, datasetArgs);
            if (rv.error) {
                this.status = status.BAD_REQUEST;
                this.body = rv;
            } else {
                this.body = rv.val;
            }
        }
        yield next;
    }

    function* validateUpdateDataset(datasetArgs) {
{'fraction_non_alphanumeric': 0.0908229347654399, 'fraction_numerical': 0.0, 'mean_word_length': 1.8768741481144935, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6267522', 'n_tokens_mistral': 1700, 'n_tokens_neox': 1731, 'n_words': 328}