{"size": 2081, "ext": "js", "max_stars_repo_path": "src/controllers/dog.js", "max_stars_repo_name": "skywalkerKFG/restFul-API-Hapi", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/controllers/dog.js", "max_issues_repo_name": "skywalkerKFG/restFul-API-Hapi", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/controllers/dog.js", "max_forks_repo_name": "skywalkerKFG/restFul-API-Hapi", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 19.0917431193, "max_line_length": 66, "alphanum_fraction": 0.4310427679}
var Dog = require('../models/dog.js');

/**
 * A list of Dogs
 */
exports.list = (req, h) => {
    return Dog.find({}).exec().then((dog) => {
        return {
            dogs: dog
        };
    }).catch((err) => {
        return { 
            err: err
        };
    });
}


/**
 * To a Dog by ID
 */
exports.get = (req, h) => {
    return Dog.findById(req.params.id).exec().then((dog) => {
        if(!dog) return {
            message: 'Dog not Found'
        };
        return {
            dog: dog
        };
    }).catch((err) => {
        return { 
            err: err 
        }
    });
}


/**
 * POST a Dog
 */
exports.create = (req, h) => {
    const dogCreate = {
        name: req.payload.name,
        breed: req.payload.breed,
        age:  req.payload.age,
        image: req.payload.image
    };

    return Dog.create(dogData).then((dog) => {
        return {
            message: "Dog has been created successfully", dog: dog
        };
    }).catch((err) => {
        return {
            err: err
        };
    });
}


/**
 * Put|Update a Dog by ID
 */
exports.update = (req, h) => {
    return Dog.findById(req.params.id).exec().then((dog) => {
        if (!dog) return {
            err: 'Dog not found'
        };

        dog.name = req.payload.name;
        dog.breed = req.payload.breed;
        dog.age = req.payload.age;
        dog.image = req.payload.image;

        dog.save(dogData);
    }).then((data) => {
        return {
            message: "Dog data has been updated successfully"
        };
    }).catch((err) => {
        return {
            err: err
        };
    });
}

/**
 * Delete Dog by ID
 */
exports.remove = (req, h) => {
    return Dog.findById(req.params.id).exec(function (err, dog) {
        if (err) return {
            dberror: err
        };
        if (!dog) return {
            message: 'Dog not found'
        };

        dog.remove(function (err) {
            if (err) return {
                dberror: err
            };

            return {
                success: true
            };
        });
    });
}