{"size": 1108, "ext": "js", "max_stars_repo_path": "vakuutuspalvelin/databaseServices/insurancedbservice.js", "max_stars_repo_name": "Varjokorento/vakuutusportaali", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "vakuutuspalvelin/databaseServices/insurancedbservice.js", "max_issues_repo_name": "Varjokorento/vakuutusportaali", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "vakuutuspalvelin/databaseServices/insurancedbservice.js", "max_forks_repo_name": "Varjokorento/vakuutusportaali", "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": 24.0869565217, "max_line_length": 93, "alphanum_fraction": 0.6209386282}
const Insurance = require('../models/insurance');

function findOneById(id) {
    return Insurance.findById({_id: id}, (err, insurance) => {
        return insurance;
    });
}

function findAllByUser(userId) {
    let insurances = Insurance.find({userid: userId});
    if (insurances) return insurances;
    else return ["Cannot find user with ID:", userId];
}

function findAll() {
    return Insurance.find({}, (err, insurances) => {
        if (err) throw err;
        else return insurances;
    });
}

function addOne(data) {
    return Insurance.create(data)
        .then(createdInsurance => {
            return createdInsurance;
        }).catch(error => {
            return error;
    });
}

function updateOneById(data) {
    return Insurance.findByIdAndUpdate({_id: data._id}, data, (err, insurance) => {
        console.log("DATA", data);
        return data;
    });
}

function deleteOneById(id){
    Insurance.deleteOne({_id: id}, (err, result) => {
        console.log("poistettu");
    });
}

module.exports = {findOneById, findAllByUser, findAll, addOne, updateOneById, deleteOneById};

