<commit_before>var mvc = require('./lib/mvc');
var renderer = require('./lib/renderer');

function CityDataProvider() {
    var cities = {};
    var nextCityId = 1;

    this.findAll = function() {
        var cityList = [];
        for(var i in cities) {
            cityList.push(cities[i]);
        }

        return cityList;
    }

    this.findById = function(cityId) {
        return cities[cityId];
    }

    this.save = function(city) {
        city.id = nextCityId++;
        cities[city.id] = city;
    }

    this.update = function(city) {
        // Nothing to be done as the service works in-memory.
    }

    this.remove = function(city) {
        delete cities[city.id];
    }
}

var routes = {};
routes['get:/cities'] = function() {
    this.render('index', this.cityService.findAll());
}

routes['get:/cities/{id}'] = function(args) {
    var city = this.cityService.findById(args.id);

    if(city) {
        this.render('show', city);
    } else {
        this.renderError(404);
    }
}

routes['post:/cities'] = function() {
   var city = {
       name: this.params.name,
       population: this.params.population
   };

   this.cityService.save(city);
   this.status = 201;
   this.render('show', city);
}

routes['put:/cities/{id}'] = function(args) {
    var city = this.cityService.findById(args.id);
    if(city) {
        city.name = this.params.name;
        city.population = this.params.population;
        this.cityService.update(city);

        this.status = 204;
        this.render();
    } else {
        this.renderError(404);
    }
}

routes['delete:/cities/{id}'] = function(args) {
    var city = this.cityService.findById(args.id);
    if(city) {
        this.cityService.remove(city);

        this.status = 204;
        this.render();
    } else {
        this.renderError(404);
    }
}

renderer.configure({
    defaultViewExtn: 'xml'
});

mvc.addToContext({cityService: new CityDataProvider()});
mvc.serve(8080, routes);
<commit_msg>Updated REST example to better emphasize DI.<commit_after>var mvc = require('./lib/mvc');
var renderer = require('./lib/renderer');

function InMemoryCityProvider() {
    var cities = {};
    var nextCityId = 1;

    this.findAll = function() {
        var cityList = [];
        for(var i in cities) {
            cityList.push(cities[i]);
        }

        return cityList;
    }

    this.findById = function(cityId) {
        return cities[cityId];
    }

    this.save = function(city) {
        city.id = nextCityId++;
        cities[city.id] = city;
    }

    this.update = function(city) {
        // Nothing to be done in an in-memory provider.
    }

    this.remove = function(city) {
        delete cities[city.id];
    }
}

var routes = {};
routes['get:/cities'] = function() {
    this.render('index', this.cityProvider.findAll());
}

routes['get:/cities/{id}'] = function(args) {
    var city = this.cityProvider.findById(args.id);

    if(city) {
        this.render('show', city);
    } else {
        this.renderError(404);
    }
}

routes['post:/cities'] = function() {
   var city = {
       name: this.params.name,
       population: this.params.population
   };

   this.cityProvider.save(city);
   this.status = 201;
   this.render('show', city);
}

routes['put:/cities/{id}'] = function(args) {
    var city = this.cityProvider.findById(args.id);
    if(city) {
        city.name = this.params.name;
        city.population = this.params.population;
        this.cityProvider.update(city);

        this.status = 204;
        this.render();
    } else {
        this.renderError(404);
    }
}

routes['delete:/cities/{id}'] = function(args) {
    var city = this.cityProvider.findById(args.id);
    if(city) {
        this.cityProvider.remove(city);

        this.status = 204;
        this.render();
    } else {
        this.renderError(404);
    }
}

renderer.configure({
    defaultViewExtn: 'xml'
});

mvc.addToContext({cityProvider: new InMemoryCityProvider()});
mvc.serve(8080, routes);

{'fraction_non_alphanumeric': 0.15878969742435609, 'fraction_numerical': 0.011502875718929733, 'mean_word_length': 2.4100596760443307, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '3183543', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1301, 'n_words': 233}