|  | 
|  | 1 | +const db = require('./index'); | 
|  | 2 | +const errors = require('restify-errors'); | 
|  | 3 | + | 
|  | 4 | +function CreateOneNote(data) { | 
|  | 5 | +    return new Promise((resolve, reject) => { | 
|  | 6 | +        const qry = "INSERT INTO notes SET ?"; | 
|  | 7 | +        db.pool.query(qry, data, (err, result) => { | 
|  | 8 | +            if (err) { | 
|  | 9 | +                return reject({  | 
|  | 10 | +                    code: new errors.InternalServerError(), | 
|  | 11 | +                    message: err | 
|  | 12 | +                }); | 
|  | 13 | +            } | 
|  | 14 | + | 
|  | 15 | +            if (result.affectedRows <= 0) { | 
|  | 16 | +                return reject({  | 
|  | 17 | +                    code: new errors.InternalServerError(), | 
|  | 18 | +                    message: 'Failed to create' | 
|  | 19 | +                }); | 
|  | 20 | +            } | 
|  | 21 | + | 
|  | 22 | +            resolve({ | 
|  | 23 | +                code: 201, | 
|  | 24 | +                message: '', | 
|  | 25 | +                note: result | 
|  | 26 | +            }); | 
|  | 27 | +        }); | 
|  | 28 | +    }); | 
|  | 29 | +} | 
|  | 30 | + | 
|  | 31 | +function GetOneNoteById(id) { | 
|  | 32 | +    return new Promise((resolve, reject) => { | 
|  | 33 | +        const qry = "SELECT * FROM notes WHERE id = ?"; | 
|  | 34 | +        db.pool.query(qry, id, (err, rows) => { | 
|  | 35 | +            if (err) { | 
|  | 36 | +                return reject({  | 
|  | 37 | +                    code: new errors.InternalServerError(), | 
|  | 38 | +                    message: err | 
|  | 39 | +                }); | 
|  | 40 | +            } | 
|  | 41 | + | 
|  | 42 | +            resolve({ | 
|  | 43 | +                code: 200, | 
|  | 44 | +                message: '', | 
|  | 45 | +                note: rows[0] || {} | 
|  | 46 | +            }); | 
|  | 47 | +        }); | 
|  | 48 | +    }); | 
|  | 49 | +} | 
|  | 50 | + | 
|  | 51 | +function GetAll() { | 
|  | 52 | +    return new Promise((resolve, reject) => { | 
|  | 53 | +        const qry = `SELECT  | 
|  | 54 | +                     notes.id as noteId,  | 
|  | 55 | +                     notes.text,  | 
|  | 56 | +                     author.id as authorId,  | 
|  | 57 | +                     author.first_name as author_firstname | 
|  | 58 | +                     FROM notes  | 
|  | 59 | +                     RIGHT JOIN users as author  | 
|  | 60 | +                     ON author.id = notes.authorId  | 
|  | 61 | +                     WHERE notes.authorId IS NOT NULL  | 
|  | 62 | +                     AND notes.id IS NOT NULL`; | 
|  | 63 | +        db.pool.query(qry, {}, (err, rows) => { | 
|  | 64 | +            if (err) { | 
|  | 65 | +                return reject({  | 
|  | 66 | +                    code: new errors.InternalServerError(), | 
|  | 67 | +                    message: err | 
|  | 68 | +                }); | 
|  | 69 | +            } | 
|  | 70 | + | 
|  | 71 | +            resolve({ | 
|  | 72 | +                code: 200, | 
|  | 73 | +                message: '', | 
|  | 74 | +                note: rows | 
|  | 75 | +            }); | 
|  | 76 | +        }); | 
|  | 77 | +    }); | 
|  | 78 | +} | 
|  | 79 | + | 
|  | 80 | +function GetNotesByAuthorId(id) { | 
|  | 81 | +    return new Promise((resolve, reject) => { | 
|  | 82 | +        const qry = `SELECT  | 
|  | 83 | +                     notes.id as noteId,  | 
|  | 84 | +                     notes.text,  | 
|  | 85 | +                     author.id as authorId,  | 
|  | 86 | +                     author.first_name as author_firstname | 
|  | 87 | +                     FROM notes  | 
|  | 88 | +                     RIGHT JOIN users as author  | 
|  | 89 | +                     ON author.id = notes.authorId  | 
|  | 90 | +                     WHERE notes.authorId = ?`; | 
|  | 91 | +        db.pool.query(qry, id, (err, rows) => { | 
|  | 92 | +            if (err) { | 
|  | 93 | +                return reject({  | 
|  | 94 | +                    code: new errors.InternalServerError(), | 
|  | 95 | +                    message: err | 
|  | 96 | +                }); | 
|  | 97 | +            } | 
|  | 98 | + | 
|  | 99 | +            resolve({ | 
|  | 100 | +                code: 200, | 
|  | 101 | +                message: '', | 
|  | 102 | +                note: rows | 
|  | 103 | +            }); | 
|  | 104 | +        }); | 
|  | 105 | +    }); | 
|  | 106 | +} | 
|  | 107 | + | 
|  | 108 | +function UpdateOneNote(id, newNote) { | 
|  | 109 | +    return new Promise((resolve, reject) => { | 
|  | 110 | +        const qry = `UPDATE notes SET text = ? WHERE id = ?`; | 
|  | 111 | +        db.pool.query(qry, [newNote.text, id], (err, rows) => { | 
|  | 112 | +            if (err) { | 
|  | 113 | +                return reject({  | 
|  | 114 | +                    code: new errors.InternalServerError(), | 
|  | 115 | +                    message: err | 
|  | 116 | +                }); | 
|  | 117 | +            } | 
|  | 118 | +             | 
|  | 119 | +            if (rows.changedRows <= 0) { | 
|  | 120 | +                return reject({  | 
|  | 121 | +                    code: new errors.InternalServerError(), | 
|  | 122 | +                    message: 'Failed to update' | 
|  | 123 | +                }); | 
|  | 124 | +            } | 
|  | 125 | + | 
|  | 126 | +            resolve({ | 
|  | 127 | +                code: 200, | 
|  | 128 | +                message: '' | 
|  | 129 | +            }); | 
|  | 130 | +        }); | 
|  | 131 | +    }); | 
|  | 132 | +} | 
|  | 133 | + | 
|  | 134 | +function DeleteOneNoteById(id) { | 
|  | 135 | +    return new Promise((resolve, reject) => { | 
|  | 136 | +        const qry = `DELETE FROM notes WHERE id = ?`; | 
|  | 137 | +        db.pool.query(qry, id, (err, rows) => { | 
|  | 138 | +            if (err) { | 
|  | 139 | +                return reject({  | 
|  | 140 | +                    code: new errors.InternalServerError(), | 
|  | 141 | +                    message: err | 
|  | 142 | +                }); | 
|  | 143 | +            } | 
|  | 144 | +             | 
|  | 145 | +            if (rows.affectedRows <= 0) { | 
|  | 146 | +                return reject({  | 
|  | 147 | +                    code: new errors.InternalServerError(), | 
|  | 148 | +                    message: 'Failed to delete' | 
|  | 149 | +                }); | 
|  | 150 | +            } | 
|  | 151 | +         | 
|  | 152 | +            resolve({ | 
|  | 153 | +                code: 200, | 
|  | 154 | +                message: '', | 
|  | 155 | +            }); | 
|  | 156 | +        }); | 
|  | 157 | +    }); | 
|  | 158 | +} | 
|  | 159 | + | 
|  | 160 | +module.exports = { | 
|  | 161 | +    CreateOneNote, | 
|  | 162 | +    GetOneNoteById, | 
|  | 163 | +    GetAll, | 
|  | 164 | +    GetNotesByAuthorId, | 
|  | 165 | +    UpdateOneNote, | 
|  | 166 | +    DeleteOneNoteById | 
|  | 167 | +}; | 
0 commit comments