{"size": 1296, "ext": "js", "max_stars_repo_path": "api/auth/auth-model.js", "max_stars_repo_name": "BuildWeekAnywhereFitness/Back-End", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "api/auth/auth-model.js", "max_issues_repo_name": "BuildWeekAnywhereFitness/Back-End", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "api/auth/auth-model.js", "max_forks_repo_name": "BuildWeekAnywhereFitness/Back-End", "max_forks_repo_forks_event_min_datetime": "2021-03-01T06:34:14.000Z", "max_forks_repo_forks_event_max_datetime": "2021-03-01T06:34:14.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 25.4117647059, "max_line_length": 136, "alphanum_fraction": 0.5625}
const db = require('../../data/db-config')

module.exports = {
    find,
    findByName,
    addInstructor,
    addClient,
    login
}

function find(id, role) {
    if(role === 1) {
        return db("instructors").where("id", id).then(res =>{
            return res[0]
        })
    }else {
        return db("clients").where("id", id).then(res => {
            return res[0]
        })
    }
}

function findByName(username, role) {
    if(role === 1) {
        return db.select("password as password", "username", "id", "role").from("instructors").where("username", username).then(res => {
            return res [0]
        })
    }else {
        return db.select("password as password", "username", "id", "role").from("clients").where("username", username).then(res =>{
            return res[0]
        })
    }
}

function addInstructor(user) {
    return db("instructors")
    .insert({ "username": user.username, "password": user.password , "role": user.role})
    .then(res => {
        return findByName(user.username, user.role)
    })
}

function addClient(user) {
    return db("clients")
    .insert({ "username": user.username, "password": user.password, "role": user.role})
    .then(res => {
        return findByName(user.username, user.role)
    })
}

function login(user){}