All files / repositories/learning_object index.js

100% Statements 19/19
100% Branches 0/0
100% Functions 5/5
100% Lines 19/19

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 4122x   22x 4x       4x 4x     22x 3x 3x     22x 3x 3x     22x 1x   1x 1x     22x 1x 1x     22x              
const db = require("../../database/connection");
 
const insert = (learningObject) => {
    const query = "INSERT INTO learning_object(urn, timestamp, data) VALUES ($1, $2, $3) " +
    "ON CONFLICT ON CONSTRAINT learning_object_pk " +
    "DO UPDATE SET timestamp = $2, data = $3 " +
    "RETURNING *"; // returns passed learningObject with it's id set to corresponding id in database
    const params = [learningObject.urn, learningObject.timestamp, learningObject.data];
    return db.query(query, params);
};
 
const findByURN = (urn) => {
    const query = "SELECT * FROM learning_object WHERE urn=$1";
    return db.query(query, [urn]);
};
 
const findAll = () => {
    const query = "SELECT * FROM learning_object";
    return db.query(query);
};
 
const update = (learningObject) => {
    const query = "UPDATE learning_object SET timestamp = $2, data = $3 WHERE urn = $1" +
        "RETURNING *"; // returns passed learningObject with it's id set to corresponding id in database
    const params = [learningObject.urn, learningObject.timestamp, learningObject.data];
    return db.query(query, params);
};
 
const removeByURN = (urn) => {
    const query = "DELETE FROM learning_object WHERE urn=$1 RETURNING *";
    return db.query(query, [urn]);
};
 
module.exports = {
    insert,
    findByURN,
    findAll,
    update,
    removeByURN,
};