<commit_before>define([
    'app',
    'config'
], function(app, config) {
    'use strict';

    function CrudManager($q, Restangular) {
        this.$q = $q;
        this.Restangular = Restangular;
    }

    /**
     * Get one entity
     *
     * @param {String} entityName  name of the entity
     * @param {Number} entityId       id of the entity
     *
     * @returns {promise} (list of fields (with their values if set) & the entity name, label & id-
     */
    CrudManager.prototype.getOne = function(entityName, entityId) {
        if (!(entityName in config.entities)) {
            return this.$q.reject('Entity ' + entityName + ' not found.');
        }

        var entityConfig = config.entities[entityName];
        this.Restangular.setBaseUrl(config.global.baseApiUrl);
        this.Restangular.setFullResponse(true);  // To get also the headers

        // Get element data
        return this.Restangular
            .one(entityName, entityId)
            .get()
            .then(function(response) {

                var fields = entityConfig.fields,
                    entity = response.data;

                angular.forEach(fields, function(field, index) {
                    if(typeof(field.edition) === "undefined") {
                        return;
                    }

                    if (typeof(entity[field.name]) !== "undefined") {
                        fields[index].value = entity[field.name];
                    }
                });

                return {
                    fields: fields,
                    entityLabel: entityConfig.label,
                    entityName: entityName,
                    entityId : entityId
                };
            });
    };


    /**
     * Get the edition fields of an entity:
     *
     * @param {String} entityName        name of the entity
     * @param {String|Array} filterType  optional filter on the edition type (can be `read-only` or `editable`)
     *
     * @returns {promise} (list of fields & the entity name, label & id)
     */
    CrudManager.prototype.getEditionFields = function(entityName, filterType) {
        var filters = [];

        if (typeof(filterType) !== 'undefined') {
            if (typeof(filterType) === 'string') {
                filters.push(filterType);
            } else if (filterType.length) {
                filters = filterType;
            }
        }


        if (!(entityName in config.entities)) {
            return this.$q.reject('Entity ' + entityName + ' not found.');
        }

        var entityConfig = config.entities[entityName],
            fields = this.filterEditionFields(entityConfig.fields, filters);

        return {
            fields: fields,
            entityLabel: entityConfig.label,
            entityName: entityName
        };
    };


    /**
     * Filter a list of field to the edition fields
     *
     * @param {Object} fields  list of fields
     * @param {Array} filters  filter on the edition type
     *
     * @returns {Object} (list of the filtered fields)
     */
    CrudManager.prototype.filterEditionFields = function(fields, filters) {
        var filteredFields = {};

        angular.forEach(fields, function(field){
            // the field is not an edition field - do nothing
            if (typeof(field.edition) === 'undefined') {
                return;
            }

            // if we don't specify a restriction, get all the edition fields
            if (!filters.length) {
                return this[field.name] = field;
            }

            // restriction to specified types fields
            if (filters.indexOf(field.edition) !== -1) {
                return this[field.name] = field;
            }

        }, filteredFields);

        return filteredFields;
    };

    /**
     * Create a new entity
     * Post the data to the API to create the new object
     *
     * @param {String}  entityName  the name of the entity
     * @param {Object}  entity           the entity's object
     *
     * @returns {promise}  the new object
     */
    CrudManager.prototype.createOne = function (entityName, entity) {
        if (!(entityName in config.entities)) {
            return this.$q.reject('Entity ' + entityName + ' not found.');
        }

        this.Restangular.setBaseUrl(config.global.baseApiUrl);
        
        // Get element data
        return this.Restangular
            .restangularizeElement(null, entity, entityName)
            .post();
    };

    /**
     * Update an entity
     * Put the data to the API to create the new object<commit_msg>Fix error after double creation<commit_after>define([
    'app',
    'config'
], function(app, config) {
    'use strict';

    function CrudManager($q, Restangular) {
        this.$q = $q;
        this.Restangular = Restangular;
    }

    /**
     * Get one entity
     *
     * @param {String} entityName  name of the entity
     * @param {Number} entityId       id of the entity
     *
     * @returns {promise} (list of fields (with their values if set) & the entity name, label & id-
     */
    CrudManager.prototype.getOne = function(entityName, entityId) {
        if (!(entityName in config.entities)) {
            return this.$q.reject('Entity ' + entityName + ' not found.');
        }

        var entityConfig = config.entities[entityName];
        this.Restangular.setBaseUrl(config.global.baseApiUrl);
        this.Restangular.setFullResponse(true);  // To get also the headers

        // Get element data
        return this.Restangular
            .one(entityName, entityId)
            .get()
            .then(function(response) {

                var fields = entityConfig.fields,
                    entity = response.data;

                angular.forEach(fields, function(field, index) {
                    if(typeof(field.edition) === "undefined") {
                        return;
                    }

                    if (typeof(entity[field.name]) !== "undefined") {
                        fields[index].value = entity[field.name];
                    }
                });

                return {
                    fields: fields,
                    entityLabel: entityConfig.label,
                    entityName: entityName,
                    entityId : entityId
                };
            });
    };


    /**
     * Get the edition fields of an entity:
     *
     * @param {String} entityName        name of the entity
     * @param {String|Array} filterType  optional filter on the edition type (can be `read-only` or `editable`)
     *
     * @returns {promise} (list of fields & the entity name, label & id)
     */
    CrudManager.prototype.getEditionFields = function(entityName, filterType) {
        var filters = [];

        if (typeof(filterType) !== 'undefined') {
            if (typeof(filterType) === 'string') {
                filters.push(filterType);
            } else if (filterType.length) {
                filters = filterType;
            }
        }


        if (!(entityName in config.entities)) {
            return this.$q.reject('Entity ' + entityName + ' not found.');
        }

        var entityConfig = config.entities[entityName],
            fields = this.filterEditionFields(entityConfig.fields, filters);

        return {
            fields: fields,
            entityLabel: entityConfig.label,
            entityName: entityName
        };
    };


    /**
     * Filter a list of field to the edition fields
     *
     * @param {Object} fields  list of fields
     * @param {Array} filters  filter on the edition type
     *
     * @returns {Object} (list of the filtered fields)
     */
    CrudManager.prototype.filterEditionFields = function(fields, filters) {
        var filteredFields = {};

        angular.forEach(fields, function(field){
            // the field is not an edition field - do nothing
            if (typeof(field.edition) === 'undefined') {
                return;
            }

            // if we don't specify a restriction, get all the edition fields
            if (!filters.length) {
                return this[field.name] = field;
            }

            // restriction to specified types fields
            if (filters.indexOf(field.edition) !== -1) {
                return this[field.name] = field;
            }

        }, filteredFields);

        return filteredFields;
    };

    /**
     * Create a new entity
     * Post the data to the API to create the new object
     *
     * @param {String}  entityName  the name of the entity
     * @param {Object}  entity           the entity's object
     *
     * @returns {promise}  the new object
     */
    CrudManager.prototype.createOne = function (entityName, entity) {
        if (!(entityName in config.entities)) {
            return this.$q.reject('Entity ' + entityName + ' not found.');
        }

        this.Restangular.setBaseUrl(config.global.baseApiUrl);
        this.Restangular.setFullResponse(false);
        
        // Get element data
        return this.Restangular
            .restangularizeElement(null, entity, entityName)
            .post();
    };

    /**
     * Update an entity
     * Put the data to the API to create the new object
{'fraction_non_alphanumeric': 0.10498432262947345, 'fraction_numerical': 0.00021623959346956428, 'mean_word_length': 1.6580459770114941, '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': '1884262', 'n_tokens_mistral': 2430, 'n_tokens_neox': 2349, 'n_words': 664}