'use strict';
/* global require */

var Country = require('mongoose').model('Country');

exports.getCountries = function (req, res, next) {
    var query = Country.find(req.query);

    query.exec(function (err, countries) {
        if (err) {
            return next(err);
        }
        if (!countries) {
            return next(new Error('No countries found'));
        }
        res.send(countries);
    });
};


exports.getCountriesByID = function (req, res, next) {
    var query = Country.findOne({country_ID: req.params.country_ID});

    query.exec(function (err, country) {
        if (err) {
            return next(err);
        }
        if (!country) {
            return next(new Error('No country found'));
        }
        res.send(country);
    });
};

'use strict';

var Country = require('mongoose').model('Country');

var getCountrySet = function(methodName, criteria, errorMessage, res, next) {
    Country[methodName](criteria).exec(function (err, countrySet) {
        if (err) {
            return next(err);
        }
        if (!countrySet) {
            return next(new Error(errorMessage));
        }
        res.send(countrySet);
    });
};

exports.getCountries = function (req, res, next) {
    getCountrySet('find', req.query, 'No countries found', res, next);
};

exports.getCountriesByID = function (req, res, next) {
    getCountrySet('findOne', {country_ID: req.params.country_ID}, 'No country found', res, next);
};

refactor the back-end `countries` module