All files / repositories/competency index.js

100% Statements 28/28
100% Branches 0/0
100% Functions 8/8
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 5711x   11x 43x   43x 43x     11x 5x 5x     11x 1x 1x     11x 1x 1x     11x 1x 1x     11x 1x   1x 1x     11x 1x 1x     11x 1x 1x     11x                    
const db = require("../../database/connection");
 
const insert = (competency) => {
    const query = "INSERT INTO competency(title, category_id) VALUES ($1, $2) " +
        "RETURNING *"; // returns passed competency with it's id set to corresponding id in database
    const params = [competency.title, competency.categoryId];
    return db.query(query, params);
};
 
const findById = (id) => {
    const query = "SELECT * FROM competency WHERE id=$1";
    return db.query(query, [id]);
};
 
const findAll = () => {
    const query = "SELECT * FROM competency";
    return db.query(query);
};
 
const findAllByParent = (categoryId) => {
    const query = "SELECT * FROM competency WHERE category_id=$1";
    return db.query(query, [categoryId]);
};
 
const findAllByParentJoint = (categoryId) => {
    const query = "SELECT * FROM competency LEFT JOIN category ON id(category) = category_id WHERE category_id=$1";
    return db.query(query, [categoryId]);
};
 
const update = (competency) => {
    const query = "UPDATE competency SET title = $2 WHERE id = $1" +
        "RETURNING *"; // returns passed competency with it's id set to corresponding id in database
    const params = [competency.id, competency.title];
    return db.query(query, params);
};
 
const removeById = (id) => {
    const query = "DELETE FROM competency WHERE id=$1 RETURNING *";
    return db.query(query, [id]);
};
 
const findByKeyword = (keyword) => {
    const query = "SELECT * FROM competency WHERE UPPER(title) LIKE UPPER($1)";
    return db.query(query, [`%${keyword}%`]);
};
 
module.exports = {
    insert,
    findById,
    findAll,
    findAllByParent,
    findAllByParentJoint,
    findByKeyword,
    update,
    removeById,
};