# All files / models issueTypeModel.js

0% Statements 0/26
100% Branches 0/0
0% Functions 0/4
0% Lines 0/26

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
    57
    58

     const { getDb } = require('../dbPool');

    // CREATE
    const create = async (type, callback) => {
      try {
        const db = await getDb();
        const [result] = await db.query(
          `INSERT INTO IssueTypes (category, subcategory) VALUES (?, ?)`,
          [type.category, type.subcategory]
        );
        callback(null, result.insertId);
      } catch (err) {
        callback(err);
      }
    };

    // READ ALL
    const getAll = async (callback) => {
      try {
        const db = await getDb();
        const [rows] = await db.query(`SELECT * FROM IssueTypes ORDER BY category, subcategory`);
        callback(null, rows);
      } catch (err) {
        callback(err);
      }
    };

    // UPDATE
    const update = async (id, type, callback) => {
      try {
        const db = await getDb();
        const [result] = await db.query(
          `UPDATE IssueTypes SET category = ?, subcategory = ? WHERE id = ?`,
          [type.category, type.subcategory, id]
        );
        callback(null, result.affectedRows);
      } catch (err) {
        callback(err);
      }
    };

    // DELETE
    const remove = async (id, callback) => {
      try {
        const db = await getDb();
        const [result] = await db.query(`DELETE FROM IssueTypes WHERE id = ?`, [id]);
        callback(null, result.affectedRows);
      } catch (err) {
        callback(err);
      }
    };

    module.exports = {
      create,
      getAll,
      update,
      remove
    };